qt如何调用sqlite数据库
3个回答
展开全部
//自己设置连接名,不使用默认连接名
if (SdbSqlite.contains("Conn2sqlite"))
{
SdbSqlite = QSqlDatabase::database("Conn2sqlite");
}
else
{
SdbSqlite = QSqlDatabase::addDatabase(
"QSQLITE","Conn2sqlite");
}
SdbSqlite.setDatabaseName("FiveChess.database"); //数据库名称
这是连接
if (SdbSqlite.open())
{
int RltRowNumber = 0; //查询出来的结果列数
QString StrSql1 =
QString("select name,grade,integration,round,victory,defeat,rank"
" from member where account = '%0' and password = '%1';")
.arg(StrAcn,StrPsdOrNew);
QSqlQuery query1(SdbSqlite);
query1.exec(StrSql1);
while (query1.next())
{
if (SdbSqlite.driver()->hasFeature(QSqlDriver::QuerySize))
{
//速度快
RltRowNumber = query1.size();
}
else
{
//速度很慢
//实际执行的是这个
query1.last();
RltRowNumber = query1.at() + 1;
}
}
SdbSqlite.close();
}
这是查询
展开全部
代码:mian.cpp
#include <QtCore/QCoreApplication>
#include <QtSql>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("/tmp/my.db");
if (!db.open())
{
qDebug()<<"open database failed ---"<<db.lastError().text()<<"/n";
return -1;
}
QSqlQuery query;
bool ok = query.exec("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY AUTOINCREMENT,"
"name VARCHAR(20) NOT NULL,"
"age INTEGER NULL)");
if (ok)
{
qDebug()<<"ceate table partition success/n";
}
else
{
qDebug()<<"ceate table partition failed/n";
}
for (int i = 0; i< 3; ++i)
{
query.prepare("INSERT INTO people (id, name, age) VALUES (:id, :name, :age)");
query.bindValue(":name", QString("smith_%1").arg(i+1));
query.bindValue(":age", 20+i*5);
query.exec();
}
// QSqlQuery query;
query.exec("SELECT id, name, age FROM people");
while (query.next())
{
qDebug()<<"people("<<query.value(0).toInt()<<") name:"<<query.value(1).toString()<<" age:"<<query.value(2).toInt();
}
return a.exec();
}
sql.pro:
QT += core sql
QT -= gui
TARGET = sql
CONFIG += console
CONFIG -= app_bundle
LIBS += -lsqlite3
TEMPLATE = app
SOURCES += main.cpp
编译运行,输出:
ceate table partition success
people( 1 ) name: "smith_1" age: 20
people( 2 ) name: "smith_2" age: 25
people( 3 ) name: "smith_3" age: 30
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-03-25
展开全部
有接口啊。看sqlite.h
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |