QT+SQLite3 基础

QT+SQLite3 基础,第1张

概述环境:DebianGNU/Linux8.1 QT版本:Qt Creator 3.2.1 Based on Qt 5.3.2(GCC 4.9.2, 32bit) SQLite可视化软件:sqlitebrowser .pro文件加上 QT += sql 头文件 #include<QtSql> (1)建数据库、建表、插入数据、读取数据 QSqlDatabase db = QSqlDatabase

环境:DebianGNU/linux8.1

QT版本:Qt Creator 3.2.1 Based on Qt 5.3.2(GCC 4.9.2,32bit)

sqlite可视化软件:sqlitebrowser


.pro文件加上

QT += sql

头文件
#include<Qtsql>

(1)建数据库、建表、插入数据、读取数据
    QsqlDatabase db = QsqlDatabase::addDatabase("QsqlITE");    db.setDatabasename("DB.db");    if(!db.open())    {        qDeBUG() << "database open Failed!";        return ;    }    Qsqlquery query;    query.exec("create table student(ID varchar,name varchar)");    query.exec(QObject::tr("insert into student values(1,'Rachel')"));    query.exec(QObject::tr("insert into student values(2,'Phoebe')"));    query.exec(QObject::tr("insert into student values(3,'Monica')"));    query.exec(QObject::tr("insert into student values(4,'Chandler')"));    query.exec(QObject::tr("insert into student values(5,'张三')"));    query.exec(QObject::tr("insert into student values(6,'李四')"));    query.exec(QObject::tr("insert into student values(7,'王五')"));    query.exec(QObject::tr("insert into student values(8,'赵四')"));    query.exec(QObject::tr("insert into student values(9,'赵五')"));    query.exec(QObject::tr("insert into student values(10,'赵六')"));    query.exec("select ID,name from student where ID>=1");    while(query.next())    {        int ele0=query.value(0).toInt();        QString ele1 = query.value(1).toString();        qDeBUG() << ele0 << " " << ele1;    }

(2)建数据库、建表(主键自增)、插入数据、读取数据

@H_404_22@ QsqlDatabase db = QsqlDatabase::addDatabase("QsqlITE"); db.setDatabasename("DB.db"); if(!db.open()) { qDeBUG() << "database open Failed!"; return ; } Qsqlquery query; query.exec("create table student(ID INTEGER PRIMARY KEY autoINCREMENT,name TEXT NOT NulL)"); query.exec(QObject::tr("insert into student values(NulL,'Rachel')")); query.exec(QObject::tr("insert into student values(NulL,'Phoebe')")); query.exec(QObject::tr("insert into student values(NulL,'Monica')")); query.exec(QObject::tr("insert into student values(NulL,'Chandler')")); query.exec(QObject::tr("insert into student values(NulL,'张三')")); query.exec(QObject::tr("insert into student values(NulL,'李四')")); query.exec(QObject::tr("insert into student values(NulL,'王五')")); query.exec(QObject::tr("insert into student values(NulL,'赵四')")); query.exec(QObject::tr("insert into student values(NulL,'赵五')")); query.exec(QObject::tr("insert into student values(NulL,name from student where ID>=1"); while(query.next()) { int ele0=query.value(0).toInt(); QString ele1 = query.value(1).toString(); qDeBUG() << ele0 << " " << ele1; } 总结

以上是内存溢出为你收集整理的QT+SQLite3 基础全部内容,希望文章能够帮你解决QT+SQLite3 基础所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1166498.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存