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

qt資料庫開發

發布時間: 2023-03-01 05:24:47

❶ qt資料庫應用編程實驗,實現了哪些功能

大致只要知道以上例子就OK了。一般而言會將db變數作為全局變數或者數據成員,當需要訪問的時候就獲取一個query就可以了。
另外,要實現所提到的功能,要好好看看QString、QStringList,這兩玩意主要是臨時存放數據、分割數據等作用,另外,要看QFile、QDir、QFileInfo、QTextStream,這四個主要用來讀取文本數據。看看QVector或者QList等來存放讀出來的數據。
另外,要實現這玩意,sql的語句不要求精通,但是基本的Create、Insert、Select、Delete等操作要准確無誤。否則出錯了QT是看不出來的。QT不會檢測SQL的准確性的。
再另外,在QT的編程中,C++和類都很重要,所以還是得看看。

❷ qt怎麼開發資料庫程序

不知道QTable是什麼,不過可以用分頁的辦法來處理數據,也就是說最好用可以支持SQL查詢的組件,在裡面語句裡面用分頁top 100,「沒有客戶端可以讓你如此豪放的處理數據」--李維語

❸ 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

❹ qt的資料庫實現是怎樣的

完整的代碼給你了:

data.h
#define DB_SALES_DRIVER "QMYSQL3"
#define DB_SALES_DBNAME "CAAS"
#define DB_SALES_USER "root"
#define DB_SALES_PASSWD ""
#define DB_SALES_HOST ""

main.cpp

#include <qapplication.h>
#include <qsqldatabase.h>
#include <qsqlquery.h>
#include <qsqlcursor.h>
#include <qtextedit.h>
#include <qstring.h>
#include <qtextcodec.h>
#include <qvbox.h>
#include "data.h"
#include <stdio.h>
#include <qpushbutton.h>

class MyQVBox : public QVBox
{
public:
MyQVBox( QWidget *parent=0, const char *name=0 );
//public slots:
// refValue(QSrting lzw);
};

MyQVBox::MyQVBox( QWidget *parent, const char *name )
: QVBox( parent, name )
{
QString Lzw("\n");
QSqlDatabase *defaultDB = QSqlDatabase::addDatabase(DB_SALES_DRIVER);
if ( defaultDB )
{
defaultDB->setDatabaseName( DB_SALES_DBNAME );
defaultDB->setUserName( DB_SALES_USER );
defaultDB->setPassword( DB_SALES_PASSWD );
defaultDB->setHostName( DB_SALES_HOST );

if ( defaultDB->open() )
{
//插入數據
QSqlQuery query("INSERT INTO test(ID,Name,Age) VALUES(1155, 'Ginger', 125);" );

//提取數據
QSqlCursor cur( "test" ); // 指定表/視圖名稱
cur.select(); // 我們將檢索每一條記錄
while ( cur.next() )
{
qDebug( cur.value( "ID" ).toString() + ": " +
cur.value( "Name" ).toString() + " " +
cur.value( "Age" ).toString() );
Lzw.append(cur.value( "ID" ).toString() + ": " +
cur.value( "Name" ).toString() + " " +
cur.value( "Age" ).toString()+"\n");
}
//qDebug(Lzw);
printf("aaaaaaa");
}
}

QString ustr = (QTextCodec::codecForLocale())->toUnicode(Lzw);
QString sstr = (QTextCodec::codecForLocale())->toUnicode("刷新");
QTextEdit *myEdit = new QTextEdit(this,0);
myEdit->setText(ustr);
QPushButton *quit = new QPushButton(sstr, this, "quit" );
connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
}

int main( int argc, char *argv[] )
{
QApplication app( argc, argv );
MyQVBox *mybox = new MyQVBox(0,0);
//mybox->setText(ustr);
app.setMainWidget(mybox);
mybox->show();
return app.exec();
}

❺ Qt的資料庫

query對象未與db綁定 -- 在哪執行啊?

❻ QT開發的 mysql資料庫

試試在addDatabase時指定不同的ConnectionName

❼ 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實現資料庫數據顯示