加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.cpp 2.88 KB
一键复制 编辑 原始数据 按行查看 历史
chenjingkui 提交于 2016-07-28 18:57 . 完整版本
#include <QCoreApplication>
#include <QtSql>
#include <QDebug>
#include <QVector>
int main(int argc, char *argv[])
{
QString connectionName("playlist_connection");
QCoreApplication a(argc, argv);
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", connectionName);
db.setDatabaseName("media.db");
if(!db.open()) {
qDebug() << db.lastError();
return -1;
}
QSqlQuery query(db);
query.exec("CREATE TABLE AudioFiles ("
"path VARCHAR(255) NOT NULL,"
"modify DATETIME,"
"title VARCHAR(255),"
"artist VARCHAR(255),"
"album VARCHAR(255),"
"genre VARCHAR(255),"
"PRIMARY KEY (path)"
");");
#if 0
const QVector<QString> v({":path", ":modify", ":title", ":artist", ":album", ":genre"});
query.prepare(QString("INSERT INTO AudioFiles VALUES(%1, %2, %3, %4, %5, %6);").
arg(v[0]). arg(v[1]). arg(v[2]). arg(v[3]). arg(v[4]). arg(v[5]));
query.bindValue(v[0], "C:/Users/semisky/music/1.mp3");
query.bindValue(v[1], "2000-11-07 12:00:00");
query.bindValue(v[2], "Pretty Girl");
query.bindValue(v[3], "jay chou");
query.bindValue(v[4], "jay");
query.bindValue(v[5], "unknows");
if(!query.exec()) {
qDebug() << query.lastError();
}
#else
//Insert Row
//if one error, other querys not perform.
query.prepare("INSERT INTO AudioFiles VALUES(?, ?, ?, ?, ?, ?);");
QVariantList path, modify, title, artist, album, genre;
path << "C:/Users/semisky/music/1.mp3" << "C:/Users/semisky/music/2.mp3";
query.addBindValue(path);
modify << "2000-11-07 12:00:00" << "2000-11-07 12:00:01";
query.addBindValue(modify);
title << "Pretty Girl" << "Pretty Boy";
query.addBindValue(title);
artist << "jay chou" << "jay chou";
query.addBindValue(artist);
album << "jay" << "jay";
query.addBindValue(album);
genre << QVariant(QVariant::String) << "popluar";
query.addBindValue(genre);
if(!query.execBatch()) {
qDebug() << query.lastError();
}
#endif
//Select Row
query.exec("SELECT * FROM AudioFiles WHERE path LIKE '%mp3'");
while(query.next()) {
#if 0
qDebug() << query.record();
#else
qDebug() << query.value(0).toString() <<
query.value("modify").toString() <<
query.value(2).toString() <<
query.value(3).toString() <<
query.value(4).toString() <<
query.value(5).toString();
#endif
}
db.close();
}
QSqlDatabase::removeDatabase(connectionName);
return a.exec();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化