qt表格資料庫
A. Qt 與資料庫連接後輸出的表格是空白的
if ([WXWCommonUtils currentOSVersion] > IOS7) {
self. = NO;
}
self. = NO;
看這個UIViewController的這個屬性你就明白了,此屬性默認為YES,這樣UIViewController下如果只有一個UIScollView或者其子類,那麼會自動留出空白,讓scollview滾動經過各種bar下面時能隱約看到內容。但是每個UIViewController只能有唯一一個UIScollView或者其子類,如果超過一個,需要將此屬性設置為NO,自己去控制留白以及坐標問題。
B. 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;
}
C. QT查詢mysql資料庫中表格是否存在怎麼操作
1、sql語句判斷資料庫表是否存在:
sql:select * from user_all_tables where table_name='tableName'
如果結果為空則表示不存在,如何結果不為空則表示存在;
2、java如何判斷資料庫表是否存在
可以利用上面的sql,執行獲取結果,相應的java代碼如下:
String helperName= delegator.getGroupHelperName("com.asiainfo");
SQLProcessor sqlProcessor= new SQLProcessor(helperName);
String sql = "select * from user_all_tables where table_name='"+table+"'";
ResultSet rsTables =sqlProcessor.executeQuery(sql);
if(rsTables.next()){
Debug.logWarning("table:"+table+" exists", mole);
}else{
Debug.logWarning("table:"+table+" does not exist", mole);
}
D. 急求各位大神,我在QT中用sqlite資料庫,創建了一個表格,然後進行查如何查詢,可是為什麼查詢結果是木有
你 model->setFilter("room_id=1"); 過濾了
E. QT查詢mysql資料庫中表格是否存在怎麼操作
//QSqlDataBase裡面可以查詢所有表的名字,然後進行匹配即可
QSqlDatabasedb=QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("acidalia");
db.setDatabaseName("customdb");
db.setUserName("mojito");
db.setPassword("J0a1m8");
boolok=db.open();
//打開資料庫
QSringListstrTables=db.tables();
if(strTables.contains("Yourtable"){
qDebug()<<"Yes";
}
else{
qDebug()<<"no";
}
F. QT中table view怎麼顯示sqlite資料庫的內容
在QT的widget中用tableview顯示sqlite資料庫表中的內容。
假設有資料庫文件test.db,有表table(id integer, name nvarchar(20),age integer),且有數條數據。
首先用QTcreator創建一個基於Widget類的窗口,再拖一個tableview到widget中,保存,然後按照如下方法進行:
1.在widget.h中增添頭文件:QtSql/qsql.h、QtSql/QsqlDatabase、QtSql/QsqlQuery、QtSql/QsqlQueryModel
2.在.pro工程文件中添加:QT+=sql
3.在widget.cpp中widget的構造函數中添加如下代碼:
QsqDatabase db = QsqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("test.db");
if(!db.open())
{
//錯誤處理
}
static QSqlQueryModel *model = new QSqlQueryModel(ui->tableview);
model->setQuery(QString("select * from table"));
model->setHeaderData(0,Qt::Horizontal,QObject::tr("編號"));
model->setHeaderData(1,Qt::Horizontal,QObject::tr("姓名"));
model->setHeaderData(2,Qt::Horizontal,QObject::tr("年齡"));
ui->tableview->setModel(model);
db->close();
這樣之後,table表裡的內容就會顯示到tableview中了。
G. qt中怎樣將表格中數據導出為excel文件
如果你在做一個報表類的程序,可能將內容導出為Excel文件是一項必須的功能。之前使用MFC的時候我就寫過一個類,用於將grid中的數據導出為Excel文件。在使用了QtSql模塊後,我很容易的將這個類改寫應用在Qt程序中。類的名字叫「ExportExcelObject」。使用起來很簡單:
[cpp] view plain
// 1. declare an object
// – fileName Excel 文件路徑
// – sheetName Excel 工作表(sheet)名稱
// – tableView 需要導出的QTableView指針
ExportExcelObject obj(fileName, sheetName, tableView);
// 2. define fields (columns) to the Excel sheet file
// – 第1個參數是QTableView的列
// – 第2個參數是對應該列的Excel sheet中的列名
// – 第3個參數是該列的類型,可以使用char(x) (x最大255),int,datetime, 等
obj.addField(1, tr("name"), "char(60)");
obj.addField(2, tr("ID"), "int");
obj.addField(3, tr("time"), " datetime ");
// 3. 該類有特定的SIGNAL用於連接一個progress控制項,可以顯示導出進度
connect(&obj, SIGNAL(exportedRowCount(int)), progressBar, SLOT(setValue(int)));
// 4. do the work
int retVal = obj.export2Excel();
if(retVal > 0)
{//done
}
else
{//something wrong
}
那麼這個類是怎樣實現的呢?
1. 將Excel文件當成是一個資料庫
使用MS的ODBC或ADO都可以將Excel文件當做一個資料庫,那麼我們只需要使用下面這個DSN連接串去創建並連接至該Excel文件:
[cpp] view plain
QString dsn = QString("DRIVER={Microsoft Excel Driver (*.xls)};DSN=''; FIRSTROWHASNAMES=1;;CREATE_DB=/"%1/";DBQ=%2").
arg(excelFilePath).arg(excelFilePath);
2. 將Excel的工作表(sheet)當成是一個資料庫表
可以使用SQL語句「CREATE TABLE」 去創建一個工作表。
3. 向表中插入數據
使用SQL的「INSERT」語句插入數據。
4. Unicode支持
是的,列名和數據都支持Unicode。
H. 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實現資料庫數據顯示
I. QT 簡單資料庫操作
看你的create語句中time varchar(20)),..這邊多了一個 ) ,可能導致建的表中實際只有兩個欄位:id和time。
然後你又插入了4個欄位,所以參數個數錯誤。。