㈠ QT 簡單資料庫操作
看你的create語句中time varchar(20)),..這邊多了一個 ) ,可能導致建的表中實際只有兩個欄位:id和time。
然後你又插入了4個欄位,所以參數個數錯誤。。
㈡ 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<QSqlQuery>
#include<QObject>
#include<QVariantList>
#include<QDebug>
#include<QSqlError>
#include<QTextCodec>
#include<QObject>
staticboolcreateConnection()
{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<<x1<<x2<<x3<<x4;
query.addBindValue(ages);
QVariantListnames;
names<<QObject::tr("小王")<<QObject::tr("小明")<<QObject::tr("小張")<<QObject::tr("小新");//如果要提交空串,用QVariant(QVariant::String)代替名字
query.addBindValue(names);
if(!query.execBatch())//進行批處理,如果出錯就輸出錯誤
qDebug()<<query.lastError();
returntrue;
}
#endif//DATABASE_H
然後用QSqlTableModel實現資料庫數據顯示
㈣ 在Qt數據開發中,用來建立資料庫連接的類是什麼,用來提供SQL執行方法的類是什
using System; using System.Data.SqlClient; namespace _09_01 { class Class_09_01 { public static void Main(String[] args) { // 連接字元串,讀者可以根據自己情況設置不同的值 string strConn = "Initial Catalog=Northwind;Data Source=(local);User ID=sa;Password=sa"; // SqlConnection 對象 SqlConnection conn = new SqlConnection(); conn.ConnectionString = strConn; try { // 打開資料庫連接 conn.Open(); Console.WriteLine("成功連接到資料庫!"); Console.WriteLine("數據源:{0}", conn.DataSource); Console.WriteLine("資料庫名:{0}", conn.Database); Console.WriteLine("客戶端名:{0}", conn.WorkstationId); // 在用完了資料庫連接之後記得關閉! conn.Close(); } catch (Exception e) { Console.WriteLine("無法連接到資料庫!報告異常:"); Console.WriteLine(e.Message); } } } }
㈤ QT資料庫
網上下載「精通QT4編程.pdf」或「QT4從入門到精通.pdf」,裡面有較詳細的說明
㈥ qt資料庫應用編程實驗,實現了哪些功能
大致只要知道以上例子就OK了。一般而言會將db變數作為全局變數或者數據成員,當需要訪問的時候就獲取一個query就可以了。
另外,要實現所提到的功能,要好好看看QString、QStringList,這兩玩意主要是臨時存放數據、分割數據等作用,另外,要看QFile、QDir、QFileInfo、QTextStream,這四個主要用來讀取文本數據。看看QVector或者QList等來存放讀出來的數據。
另外,要實現這玩意,SQL的語句不要求精通,但是基本的Create、Insert、Select、Delete等操作要准確無誤。否則出錯了QT是看不出來的。QT不會檢測SQL的准確性的。
再另外,在QT的編程中,C++和類都很重要,所以還是得看看。
㈦ Qt資料庫編程如何刷新數據
在對資料庫執行打開操作的時候,是會產生一個臨時文件,實際上是把原來資料庫裡面的數據提取到臨時文件進行操作,關閉資料庫的時候再把臨時文件里的數據傳回去,消除臨時文件。如果上一次沒有關閉資料庫的話,以後實際上是在操作一個空的資料庫文件。
所以資料庫的操作應該是每次操作前打開,操作後關閉,這樣是正確的