当前位置:网站首页>Some cold knowledge about QT database development
Some cold knowledge about QT database development
2022-06-26 19:09:00 【feiyangqingyun】
One 、 Knowledge points
- Qt That is, it supports direct communication with the database in the form of a library , Also support ODBC The form of data source communicates with various databases , So it covers all the situations .
- Qt The database program is packaged and released , All premises : Pay attention to distinguish between 32/64 position , Your program is 32 You have to take it with you 32 Bit Library ,64 You must bring 64 Bit Library , this Qt This is also the requirement for the library .mysql Publishing is the easiest , Take one mysql The dynamic library file of (windows On is libmysql.dll), It's simple .sqlserver No need to bring it , Because he is the son of Microsoft , The general operating system comes with .postgres Need to bring libpq.dll、libintl-8.dll、libiconv-2.dll、libeay32.dll、ssleay32.dll Just a few files .oracle Need to bring oci.dll、oraociei11.dll( This file is very big 130MB+), If not, it is recommended to install one directly oracle client Client software , Then corresponding bin Just set the directory to the environment variable .
- Test it after packaging and publishing , Find out 32 Bit programs can also be connected normally 64 Bit mysql,64 Bit programs can also be connected normally 32 Bit mysql, Therefore, the judgment is as long as it is consistent with the number of bits in the library of the program ( The same rule applies when compiling ,32 Bit Qt The program compilation database plug-in also needs to use 32 Bit database link library .), It does not need to be consistent with the number of digits in a specific database , tested mysql、sqlserver、postgresql Databases are similar rules .
- A lot of tests are compared , adopt odbc A large number of data records are inserted in batch by means of data source and direct database , Direct connection is faster , about 5% about , Therefore, it is recommended to adopt this method as far as possible , It is only in the environment without this method that odbc The way of data sources ,Qt Default by oneself odbc Database plug-ins .
- mysql、postgresql The database is executing sql Script time , The table name and field name will be automatically converted to lowercase ,oracle All in capitals , This leads to the use of QSqlTableModel call setTable When setting the database table name , It must be consistent with the table name in the database , Case sensitive ,mysql The default configuration settings of the database are not case sensitive, so there is no such problem , So that's right postgresql and oracle You must pay attention to the database , I have been stuck here for a long time , Almost put this huge shit basin in Qt Of BUG On .
void DbHelper::bindTable(const QString &dbType, QSqlTableModel *model, const QString &table)
{
//postgresql All lowercase ,oracle All capitals , The two databases strictly distinguish the case of table names and field names
QString flag = dbType.toUpper();
if (flag == "POSTGRESQL") {
model->setTable(table.toLower());
} else if (flag == "ORACLE") {
model->setTable(table.toUpper());
} else {
model->setTable(table);
}
}
- Qt It supports opening a database without specifying a database name , Because sometimes it is necessary to connect to the database server , perform sql Statement create database . How to connect to the database does not exist yet , Tests found sqlite、mysql、sqlserver、postgresql All support this feature . The premise of deleting and creating a database is that the database is not occupied by other programs , For example, if other programs have opened the database, the execution will fail . Here I have tortured many times , Why did the execution fail ? Later, I found that the third-party database tool has opened the database , Just turn off the tools ok 了 .
QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL");
//database.setDatabaseName("dbtool");
database.setHostName("127.0.0.1");
database.setPort(3306);
database.setUserName("root");
database.setPassword("root");
if (database.open()) {
QSqlQuery query(database);
qDebug() << " Delete database " << query.exec("drop database dbtool");
qDebug() << " Create database " << query.exec("create database dbtool");
if (query.exec("select * from userinfo")) {
while (query.next()) {
qDebug() << " Query the database " << query.value(0);
}
}
} else {
qDebug() << " Open database " << database.lastError().text();
}
- use QSqlQueryModel+QTableView Display the data ,int Data of type , If exceeded 100 ten thousand , Will become a scientific count display , This is very annoying , Definitely not the result you want . Search the Internet , Finally found a friend with the same problem , You need to add an empty delegate to this column . It is not possible to find an empty delegate later , exceed 1000 Ten thousand pieces are awesome again , Need the ultimate Dafa overloaded data model display .
ui->tableView->setItemDelegateForColumn(0, new QItemDelegate);
// Here is the ultimate Dharma
QVariant SqlQueryModel::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
// exceed 100 The value of ten thousand will be displayed by scientific counting. It needs to be converted here into a string display
if (role == Qt::DisplayRole) {
int result = value.toInt();
if (result >= 1000000) {
value = QString::number(result);
}
}
return value
}
- mysql Databases have multiple database engines , among MyIsam Database transactions are not supported , The default is usually this engine , So when you use Qt Medium transaction After the method commit At the time of submission , You will find it unsuccessful , In fact, it is successful , It is correct to check the corresponding results in the database . There are two ways , The first is to change the database engine to InnoDB, The second is to make a wrong judgment after submitting if (database.commit() || !database.lastError().isValid()) , Error unavailability also indicates success .
- If the odbc Data source communication , You only need to set the database name setDatabaseName、 Set user name setUserName、 Set user password setPassword These three parameters are sufficient , Because the corresponding host address, port and associated database name have been set when configuring the data source , So in use odbc When communicating with the data source, you only need to verify the user information again . What we should pay special attention to here is setDatabaseName To set the database name, fill in the name of the data source configuration .
- After a lot of comparative tests , Including the insert 、 Delete 、 Batch 、 Inquire about 、 Paging and other operations , Tens of millions of data , stay Qt The response speed of the database part , The friendliness ranking is sqlite > postgresql > oracle > mysql > odbc . Tens of millions or more postgresql > oracle > mysql > sqlite > odbc . Above 100 million is oracle > postgresql > other . The above tests are based on the level of beginners , As for sub database and sub table 、 The joint query 、 cache 、 Memory database and other advanced knowledge points are useless .
Two 、 Related address
- Experience address :https://pan.baidu.com/s/1ZxG-oyUKe286LPMPxOrO2A Extraction code :o05q file name :bin_dbtool.zip
- Domestic site :https://gitee.com/feiyangqingyun
- International sites :https://github.com/feiyangqingyun
- Personal home page :https://blog.csdn.net/feiyangqingyun
- Zhihu Homepage :https://www.zhihu.com/people/feiyangqingyun/
- Article navigation :https://qtchina.blog.csdn.net/article/details/122839962

边栏推荐
- To: seek truth from facts
- MySQL recharge
- DVD digital universal disc
- Résolution du problème: la machine virtuelle n'a pas pu copier et coller le fichier
- Pinda general permission system (day 3~day 4)
- Button how to dynamically set drawablebottom (setcomposunddrawables is invalid)
- 一些基本错误
- Kubernetes 资源拓扑感知调度优化
- Basic and necessary common plug-ins of vscade
- 为什么我不推荐去SAP培训机构参加培训?
猜你喜欢
随机推荐
【推荐收藏】这8个常用缺失值填充技巧一定要掌握
Union, intersection and difference operations in SQL
抖音实战~分享模块~短视频下载(保存到相册)
Button how to dynamically set drawablebottom (setcomposunddrawables is invalid)
Boot指标监测
Comparing the size relationship between two objects turns out to be so fancy
转:苹果CEO库克:伟大的想法来自不断拒绝接受现状
关于不等式取值转义的思路
爬取豆瓣读书Top250,导入sqlist数据库(或excel表格)中
ROS query topic specific content common instructions
链游开发成品源码 链游系统开发详情说明
50 lines of code to crawl TOP500 books and import TXT documents
PC end records 515 ground sweeping robot /scan data
Boyun, standing at the forefront of China's container industry
【Kubernetes】Kubernetes 原理剖析与实战应用(更新中)
Invocation failed Unexpected end of file from server
微服务版单点登陆系统(SSO)
9. Intelligent transportation project (2)
成功解决之微服务@Value获取配置文件乱码问题
Boot indicator monitoring







