-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.cpp
102 lines (91 loc) · 2.94 KB
/
database.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "database.h"
#include <QDebug>
#define DATABASE_CREATE_SQL "CREATE DATABASE \
`KylinPlayer` DEFAULT CHARACTER SET utf8;"
#define TABLE_CREATE_SQL \
"CREATE TABLE FavoriteSong "\
"(id INTEGER PRIMARY KEY, song_id NUMERIC, song_name TEXT, album_name TEXT, "\
"singer_name TEXT, song_url TEXT, artcover_url TEXT, lrc_url TEXT,UNIQUE(song_id));"
#define INSERT_SONG_SQL "REPLACE INTO FavoriteSong \
(song_id, song_name, album_name, singer_name, \
song_url, artcover_url, lrc_url) \
VALUES (?, ?, ?, ?, ?, ?, ?)"
#define SELECT_SONG_SQL "SELECT song_id, song_name, album_name, \
singer_name,song_url,artcover_url,lrc_url \
FROM FavoriteSong;"
Database::Database(QObject *parent) :
QObject(parent)
{
}
bool Database::initDataBase()
{
qDebug() << QSqlDatabase::drivers();
this->database = QSqlDatabase::addDatabase("QSQLITE");
if(!this->database.isValid())
{
return false;
}
qDebug() << "isValid";
QString dbPath = QDir::homePath() + "/.KylinPlayer.db3";
dbPath = QDir::toNativeSeparators(dbPath);
this->database.setDatabaseName(dbPath);//database name
// this->database.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1");//use SSL
if(this->database.open())
{
qDebug()<<"open "<<this->database.lastError().driverText();
}else
{
qDebug()<<"open failed";
return false;
}
qDebug()<< this->database.databaseName();
qDebug()<< this->database.tables();
QSqlQuery query(this->database);
#if 0
query.exec(DATABASE_CREATE_SQL);
query.exec("use KylinPlayer;");
#endif
bool ret = query.exec(TABLE_CREATE_SQL);
return ret;
}
bool Database::addFavoriteSong(KMediaInfo songInfo)
{
QSqlQuery query(this->database);
query.prepare(INSERT_SONG_SQL);
query.addBindValue(songInfo.id);
query.addBindValue(songInfo.songName);
query.addBindValue(songInfo.albumName);
query.addBindValue(songInfo.artistName);
query.addBindValue(songInfo.mp3Url);
query.addBindValue(songInfo.songPicUrl);
query.addBindValue(songInfo.lrcUrl);
bool ret = query.exec();
qDebug() << "qurey exec:" << ret ;
query.finish();
return ret;
}
QList<KMediaInfo> *Database::getFavoriteSong()
{
QSqlQuery query(this->database);
QList<KMediaInfo> *mediaInfoList = new QList<KMediaInfo>();
bool ret = query.exec(SELECT_SONG_SQL);
if(!ret)
{
return NULL;
}
while(query.next())
{
KMediaInfo info;
info.id = query.value(0).toInt();
info.songId = QString("%1").arg(info.id);
info.songName = query.value(1).toString();
info.albumName = query.value(2).toString();
info.artistName = query.value(3).toString();
info.mp3Url = query.value(4).toString();
info.songPicUrl = query.value(5).toString();
info.lrcUrl = query.value(6).toString();
mediaInfoList->append(info);
}
query.finish();
return mediaInfoList;
}