qt
可以实现连接各种
数据库,这里介绍qt自带的一种数据库(Qsqlite)#include#include#include#include#include#include#includestaticboolcreateConnection(){QSqlDatabasedb=QSqlDatabase::addDatabase("QSQLITE")db.setDatabaseName("mytest.db")if(!db.open())returnfalseQSqlQueryquery//query.exec(QObject::tr("createtablestudent(idintprimarykey,namevchar)"))//query.exec(QObject::tr("insertintostudentvalues(0,'刘')"))////query.exec(QObject::tr("insertintostudentvalues(1,'刚')"))//query.exec(QObject::tr("insertintostudentvalues(2,'红')"))//query.prepare("insertintostudentvalues(?,?)")//-------------------------------------------------------//通过下面这段代码可以实现向数据库插入变量//--------------------------------------------------------QVariantListagesintx1,x2,x3,x4x1=12x2=13x3=14x4=15agesbool database::createDatabase()
{
QSqlQuery query // 此处请查询 query的相关 *** 作
qDebug() << "Start to create table..."
//create table: User
query.exec("CREATE TABLE [User] ( [userId] VARCHAR(40) NOT NULL, [username] VARCHAR(40) NOT NULL, [email] VARCHAR(40), [password] VARCHAR(40), [city] VARCHAR(20), PRIMARY KEY([userId]) )")
//create table: Connect
query.exec("CREATE TABLE [Connect] ( [LeftUser] VARCHAR(40) NOT NULL, [RightUser] VARCHAR(40) NOT NULL, [relation] INTEGER DEFAULT '0' NULL, PRIMARY KEY ([LeftUser], [RightUser]))")
if (query.lastError().isValid())
{
qDebug() << query.lastError()
return false
}
else
{
qDebug() << "Create database successfully."
}
return true
}
评论列表(0条)