原来保存数据使用的是sqlite的内存数据库,后来需要将配置信息等也保存到数据库,程序配置后关闭打开要能显示正常显示保存的配置信息
1.将配置信息写入到普通的配置文件(ini等),第一个版本这样实现,缺点读写文件有点慢
2.程序中采用两种数据库,内存数据库和文件数据库,将敏感信息存放到内存数据库,将用户配置信息等存放到文件数据库,进行长期保存
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "memory");
db.setDatabaseName(":memory:");
if (!db.open()) {
LOG_ALL_ERROR(QStringLiteral("failed open sqlite."));
return bret;
}
QString strdbpath("");
strdbpath = QCoreApplication::applicationDirPath();
strdbpath += "/db";
QDir dir("");
dir.mkpath(strdbpath);
strdbpath += "/mysqlite.db";
QSqlDatabase dbset = QSqlDatabase::addDatabase("QSQLITE", "file");
dbset.setDatabaseName(strdbpath);
if (!dbset.open()) {
LOG_ALL_ERROR(QStringLiteral("failed open mysqlite.db"));
return bret;
}
QSqlQuery query(QSqlDatabase::database("memory", true));
bret = query.exec("create table backlist(no varchar(100), "
"name varchar(200),color varchar(100), "
"size varchar(100), retail double, "
"discount double, unit double, "
"attrid varchar(33), salenum int, "
"backmax int, backnum int, totalprice double, "
"serial varchar(100), orderid varchar(33), "
"date varchar(100))");
QSqlQuery setquery(QSqlDatabase::database("file", true));
bret = setquery.exec("create table printset(btitle INTEGER,bshop INTEGER,bbarcode INTEGER, "
"bsellid INTEGER,bcashdesk INTEGER,bcashier INTEGER, "
"bdate INTEGER,bmemid INTEGER,bscore INTEGER, "
"bpayable INTEGER,bprivilege INTEGER,btotal INTEGER, "
"bgoodname INTEGER,bgoodcount INTEGER,bunit INTEGER, "
"bsum INTEGER,bdiscount INTEGER,bprice INTEGER, "
"bcash INTEGER,bbank INTEGER,bchange INTEGER, "
"btel INTEGER,bend INTEGER)");
1.http://blog.csdn.net/abeldeng/article/details/24767245这篇文章讲述了创建没说使用
2.qt助手
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/zhang_ruiqiang/article/details/46697677