Qt5连接数据库常见问题及解决

建立并打开数据库

QSqlDatabase database;
database = QSqlDatabase::addDatabase("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里的单引号转义不是用反斜杠’/‘而是用单引号,就是在单引号前再加一个单引号(’’)

Qt连接Sqlite使用事务批量插入数据

方法一,批处理方式:

 dbT1.transaction();	 	//开始启动事务
   inQry.prepare("insert into datatable values (?,?,?)");
   while(!ReadDat.atEnd())
   {
       strTextData = ReadDat.readLine();
       listIndex << datIndex;
       tmStamp << tmStampCnt;
       dtData << strTextData;
       datIndex++;
       DatWin->append(strTextData);
   }
   //绑定数据,顺序应与表结构字段数据一致
   inQry.addBindValue(listIndex);	//绑定数据
   inQry.addBindValue(tmStamp);		//绑定数据
   inQry.addBindValue(dtData);		//绑定数据
   inQry.execBatch();		//进行批处理操作
   dbT1.commit();		//提交事务,此时打开数据库文件执行SQL语句
  
   //不加下面三句,重复操作上面语句时,数据库不会有新的添加
   //因为作为主键的listIndex内有原数据,此时数据库不会保存主键值重复的内容
   listIndex.clear();
   tmStamp.clear();
   dtData.clear();

方法二,占位符方式:

   dbT1.transaction();
   while(!ReadDat.atEnd())
   {
       strTextData = ReadDat.readLine();
       inQry.prepare("insert into datatable(tIndex, tTimStamp, tDistance)"
                         "values(:tIndex, :tTimStamp, :tDistance)");
        inQry.bindValue(0,datIndex);
        inQry.bindValue(1,tmStampCnt);
        inQry.bindValue(2,strTextData);
        inQry.exec();
        datIndex++;
        DatWin->append(strTextData);
   }
   dbT1.commit();

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)

发布了6 篇原创文章 · 获赞 0 · 访问量 37

猜你喜欢

转载自blog.csdn.net/hellozhuzhuye/article/details/102652854