当前位置:首页 » 服务存储 » qt存储树形数据
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

qt存储树形数据

发布时间: 2023-07-18 01:43:54

Ⅰ qt怎样创建数据库以及数据库的操作

QT创建和插入的操作代码如下:
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::adser( 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;
}