应用程序很多情况下需要操作数据库。QT支持多种数据库,但是很多情况需要安装DLL驱动。这就有点麻烦,想当初想用MYSQL的结果就
是因为驱动很难装,然后就使用了SQLITE。如果对数据库的要求不是很高的话,Sqlite应该可以满足需求了。下面就简单介绍下在QT中的
-
需要包含如下的数据库相关头文件:
-
#include <QSqlDatabase>
-
#include <QSqlQuery>
-
下面是创建数据库的操作,写成 createConnection()
-
bool database::createconnection()
-
{
-
bool IsExist = QFile::exists("Myqq.dat");
-
db = QSqlDatabase::addDatabase("QSQLITE"); // 注意不要拼写错误
-
db.setDatabaseName("Myqq.dat");
-
if (!db.open())
-
{
-
QMessageBox::critical(0, "Cannot open database",
-
"Unable to establish a database connection./n"
-
"This example needs SQLite support. Please read "
-
"the Qt SQL driver documentation for information how "
-
"to build it./n/n"
-
"Click Cancel to exit.", QMessageBox::Cancel);
-
return false;
-
}
-
if (!IsExist)
-
{
-
if (!createDatabase())
-
return false;
-
}
-
return true;
-
}
-
下面是创建数据库的操作,写成 createConnection()
-
bool database::createconnection()
-
{
-
bool IsExist = QFile::exists("Myqq.dat");
-
db = QSqlDatabase::addDatabase("QSQLITE"); // 注意不要拼写错误
-
db.setDatabaseName("Myqq.dat");
-
if (!db.open())
-
{
-
QMessageBox::critical(0, "Cannot open database",
-
"Unable to establish a database connection./n"
-
"This example needs SQLite support. Please read "
-
"the Qt SQL driver documentation for information how "
-
"to build it./n/n"
-
"Click Cancel to exit.", QMessageBox::Cancel);
-
return false;
-
}
-
if (!IsExist)
-
{
-
if (!createDatabase())
-
return false;
-
}
-
return true;
-
}
-
下面是创建数据库 createDatabase()。创建了两张表,最需要注意的就是:不要把数据库操作写错了,比如单词写错了。因为没有提示,所以还是蛮容易写错的。最后出错了,还不容易找到错误。
-
bool database::createDatabase()
-
{
-
QSqlQuery query;
-
qDebug() << "Start to create table..."; // qDebut()方便在 debug 的时候查看
-
//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;
-
}
下面就简单的贴下自己写的 创建和插入的操作方法。会写数据库的童鞋知道语法了 就知道怎么写了
-
bool 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;
-
}
插入操作
-
bool database::adduser( User user )
-
{
-
if (!db.isOpen())
-
{
-
createconnection();
-
}
-
QSqlQuery query;
-
qDebug() << "start to insert data";
-
query.exec("INSERT INTO [User] ( userId, username, email, password, city) VALUES(?,?,?,?,?)");
-
QVariantList userId;
-
userId << user.getUserId();
-
query.addBindValue(userId);
-
QVariantList username;
-
username << user.getUserName();
-
query.addBindValue(username);
-
QVariantList email;
-
email << user.getEmail();
-
query.addBindValue(email);
-
QVariantList password;
-
password << user.getPassword();
-
query.addBindValue(password);
-
QVariantList city;
-
city << user.getCity();
-
query.addBindValue(city);
-
try
-
{
-
if (!query.execBatch())
-
{
-
qDebug() << query.lastQuery();
-
qDebug() << query.lastError();
-
return NULL;
-
}
-
}
-
catch(...)
-
{
-
QMessageBox::critical(0, "Add New Node error!",
-
"Unable to add a new Node!/n/n"
-
"Click Cancel to exit.", QMessageBox::Cancel);
-
}
-
if( !UpdateConnectTable(user.getUserId(),user.getUserId(),2))
-
{
-
QMessageBox::critical(0,"","Update table Connect error");
-
return NULL;
-
}
-
return true;
-
}
阅读(2414) | 评论(0) | 转发(0) |