Qt5连接sqlite数据库

首先要在.pro文件中加入sql

QT       += core gui sql

引入头文件

#include 
#include 
#include 

建立并打开数据库

QSqlDatabase database;
database = QSqlDatabase::addDatabase("QSQLITE");	//添加数据库名称,这里写QSQLITE
database.setDatabaseName("MyDataBase.db");			//这里写数据库文件名
if (!database.open())
{
	qDebug() << "Error: Failed to connect database." << database.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子句中再使用单引号(’)了

转义字符

sqlite里的单引号转义不是用反斜杠’/‘而是用单引号,就是在单引号前再加一个单引号(’’)

你可能感兴趣的:(Qt5,数据库)