QT += core gui sql
引入头文件
#include
#include
#include
建立并打开数据库
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); //添加数据库名称,这里写QMYSQL
db.setHostName("127.0.0.1"); //设置数据库主机名
db.setDatabaseName("Mydb"); //设置数据库名
db.setUserName("root"); //设置数据库用户名
db.setPassword("123456"); //设置数据库密码
if (!db.open())
{
qDebug() << "Error: Failed to connect database." << db.lastError();
}
else
{
qDebug() << "Succeed to connect database." ;
}
创建表
QSqlQuery sql_query;
if(!sql_query.exec("create table allfilesdata( \
fileid INTEGER primary key AUTOINCREMENT, \
filename text UNIQUE, \
filepath text NOT NULL, \
filesize INTEGER, \
fileattributes INTEGER NOT NULL, \
creat_dwlowtime INTEGER NOT NULL, \
creat_dwhightime INTEGER NOT NULL, \
lastaccess_dwlowtime INTEGER NOT NULL, \
lastaccess_dwhightime INTEGER NOT NULL, \
lastwrite_dwlowtime INTEGER NOT NULL, \
lastwrite_dwhightime INTEGER NOT NULL)"))
{
qDebug() << "Error: Fail to create table."<< sql_query.lastError();
}
else
{
qDebug() << "Table created!";
}
插入数据
QSqlQuery sql_query;
sql_query.prepare("INSERT INTO allfilesdata(filename,filepath,filesize,fileattributes,creat_dwlowtime,creat_dwhightime,lastaccess_dwlowtime,lastaccess_dwhightime,lastwrite_dwlowtime,lastwrite_dwhightime) VALUES(:filename,:filepath,:filesize,:fileattributes,:creat_dwlowtime,:creat_dwhightime,:lastaccess_dwlowtime,:lastaccess_dwhightime,:lastwrite_dwlowtime,:lastwrite_dwhightime)");
sql_query.bindValue(":filename",QString::fromStdWString(fd.cFileName));
sql_query.bindValue(":filepath",QString::fromStdWString(folder));
sql_query.bindValue(":filesize",(int)(fd.nFileSizeHigh*(MAXDWORD + 1) + fd.nFileSizeLow));
sql_query.bindValue(":fileattributes",(int)fd.dwFileAttributes);
sql_query.bindValue(":creat_dwlowtime",(int)fd.ftCreationTime.dwLowDateTime);
sql_query.bindValue(":creat_dwhightime",(int)fd.ftCreationTime.dwHighDateTime);
sql_query.bindValue(":lastaccess_dwlowtime",(int)fd.ftLastAccessTime.dwLowDateTime);
sql_query.bindValue(":lastaccess_dwhightime",(int)fd.ftLastAccessTime.dwHighDateTime);
sql_query.bindValue(":lastwrite_dwlowtime",(int)fd.ftLastWriteTime.dwLowDateTime);
sql_query.bindValue(":lastwrite_dwhightime",(int)fd.ftLastWriteTime.dwHighDateTime);
if(!sql_query.exec())
{
qDebug() << sql_query.lastError();
}
else
{
qDebug() << "inserted data successfully!";
}
占位符
QSqlQuery sql_query;
QString searchstr="select * from allfilesdata where filename LIKE :linetext";
sql_query.prepare(searchstr);
sql_query.bindValue(":linetext",QString("%%1%").arg(linetext));
注意,LIKE子句中使用到的百分号(%)不是在SQL字符串中出现的,而是在绑定占位符的值的时候出现的,而且LIKE子句在SQL字符串中不能使用单引号(’),因为占位符的类型就是字符串,所以就不需要在LIKE子句中再使用单引号(’)了。
Qt5连接mysql数据库时报错QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
QSqlQuery::prepare: database not open
报错原因1:缺少libmysql.dll
解决办法:将 MySQL目录下\MySQL Server 5.5\lib\libmysql.dll复制到项目编译目录
报错原因2:服务器未开启mysql
解决办法:systemctr start mysqld.service (centos7)
报错原因3:服务器未关闭防火墙
解决办法:systemctr stop firewalld.service (centos7)