失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 在linux下Qt使用sqlite QT中SQLite使用【实例】

在linux下Qt使用sqlite QT中SQLite使用【实例】

时间:2019-04-14 10:58:38

相关推荐

在linux下Qt使用sqlite QT中SQLite使用【实例】

今天分享一个Qt下,使用SQLite的一个案例。QT中SQLite使用【实例】

#include

//#include

#include

#include

#include

#include

#include

#include

#include

int main(int argc, char *argv[])

{

QCoreApplication a(argc, argv);

QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");

database.setDatabaseName("CashSystem.db");

if(database.open())

{

qDebug()<

QSqlQuery sql_query;

QString create_sql = "create table member (id int primary key, name varchar(30), address varchar(30))"; //创建数据表

QString insert_sql = "insert into member values(?,?,?)"; //插入数据

QString select_all_sql = "select * from member";

sql_query.prepare(create_sql); //创建表

if(!sql_query.exec()) //查看创建表是否成功

{

qDebug()<<:tr create failed>

qDebug()<

}

else

{

qDebug()<< "Table Created" ;

//插入数据

sql_query.prepare(insert_sql);

QVariantList GroupIDs;

GroupIDs.append(0);

GroupIDs.append(1);

GroupIDs.append(2);

QVariantList GroupNames;

GroupNames.append("aa");

GroupNames.append("bb");

GroupNames.append("cc");

QVariantList GroupAddress;

GroupAddress.append("beijing");

GroupAddress.append("shanghai");

GroupAddress.append("guangzhou");

sql_query.addBindValue(GroupIDs);

sql_query.addBindValue(GroupNames);

sql_query.addBindValue(GroupAddress);

if(!sql_query.execBatch())

{

qDebug()<

}

else

{

qDebug()<

}

//查询所有记录

sql_query.prepare(select_all_sql);

if(!sql_query.exec())

{

qDebug()<

}

else

{

while(sql_query.next())

{

int id = sql_query.value(0).toInt();

QString name = sql_query.value(1).toString();

QString address = sql_query.value(2).toString();

qDebug()<

}

}

}

}

database.close();

QFile::remove("CashSystem.db");

return a.exec();

}

如果觉得《在linux下Qt使用sqlite QT中SQLite使用【实例】》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。