當前位置:首頁 » 服務存儲 » 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;
}