當前位置:首頁 » 數據倉庫 » qt資料庫添加
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

qt資料庫添加

發布時間: 2022-12-07 12:01:28

Ⅰ qt5.9怎麼向mysql資料庫中插入數據

最基本實現:
按照lindEdit裡面輸入的內容查找資料庫並顯示出來。
最高實現目標:
用了個combobox裡面有幾個屬性都是資料庫裡面有的,先選擇相應的屬性,再輸入對應的內容到linEdit,按照lindEdit裡面輸入的內容查找資料庫並顯示出來。

Ⅱ 用qt如何連接資料庫(簡答題)

連接mysql 資料庫
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); // 使用mysql資料庫驅動
db.setHostName("localhost");
db.setDatabaseName("exampledb"); // 資料庫名稱
db.setUserName("sa"); // 用戶名
db.setPassword("1"); // 密碼
bool ok = db.open(); // 嘗試連接資料庫
if(ok)
{
QSqlQuery myquery;
if(myquery.exec("select * from employeedb"))
{
int num = 0;
if(db.driver()->hasFeature(QSqlDriver::QuerySize))
{
num = myquery.size(); // 如果支持結果影響的行數,那麼直接記錄下來
}
else
{
myquery.last(); //否則定位到結果最後
num = myquery.at() + 1;
}
//這里添加資料庫的查詢結果處理操作
}
else // 如果查詢失敗
{
QSqlError error = myquery.lastError();
}
}
else // 打開資料庫失敗
{
}

Ⅲ QT 簡單資料庫操作

看你的create語句中time varchar(20)),..這邊多了一個 ) ,可能導致建的表中實際只有兩個欄位:id和time。

然後你又插入了4個欄位,所以參數個數錯誤。。

Ⅳ QT中怎樣連接MYsql資料庫,遠程連接資料庫等

1: windows 下登陸mysql 命令行,(1)進入cmd (2) cd mysql 安裝路徑/mysqlserver5.6/bin
(3) 使用命令mysql -u root -p 然後根據提示輸入密碼 進入命令行

select user(); //顯示當前用戶

2: 在同一台電腦上利用Qt 訪問資料庫
(1)顯示當前電腦上安裝的資料庫驅動
QStringList drivers = QSqlDatabase::drivers();
foreach(QString driver, drivers)
qDebug() <<"/t" << driver;
(2)QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");/對 QMYSQL進行操作,本函數
有第二個參數 連接名
db.setHostName("localhost");//或127.0.0.1 本主機
db.setPort(3306);
db.setDatabaseName("example"); //對資料庫example進行操作
db.setUserName("wangxuetao"); //wangxuetao是一個對example資料庫有操作許可權的賬戶
db.setPassword("3791948");

於是mysql中增加一個賬戶可使用
Grant all previliges on *.* to 『wangxuetao』@』localhost』 identified by
『3791948』 with grant option;
Flush privileges; //更新

(3)db.open() 函數可由於檢測資料庫是否連接成功
cout<DBConnection();
2. m_sqlquery = new QSqlQuery("",m_sqldb);//statement 1:connect db with sql
query
3. if(result == R_OK)
4. {
5. result = m_sqlquery->exec("INSERT INTO children(fname,age) VALUES('A
nn2',13)");
6. if(!result)
7. qDebug()<<" [OK] "<<"EXEC successed";
8. m_sqlquery->exec("SELECT * FROM children c LIMIT 0,1000");
9. while(m_sqlquery->next())
10. {
11. qDebug()<value(0).toString()<value(1).
toString();
12. }
13. }

Ⅳ 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;
}

Ⅵ qt怎樣創建資料庫以及資料庫的操作

qt可以實現連接各種資料庫,這里介紹qt自帶的一種資料庫(Qsqlite)#include#include#include#include#include#include#(){QSqlDatabasedb=QSqlDatabase::addDatabase("QSQLITE");db.setDatabaseName("mytest.db");if(!db.open())returnfalse;QSqlQueryquery;//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(?,?)");//-------------------------------------------------------//通過下面這段代碼可以實現向資料庫插入變數//--------------------------------------------------------QVariantListages;intx1,x2,x3,x4;x1=12;x2=13;x3=14;x4=15;ages