Qt Creator 链接数据库mysql 的demo 环境:ubuntu 10.10 Qt Creator mysql5 qt4 以及相关的lib(这个问题困扰了不少家伙,我想如果用ubnuntu,这个问题应该不大) 本文假设你已经配置好了的情况下; 用向导建立一个 基于对话框的qt gui application dialog 界
Qt Creator 链接数据库mysql 的demo
环境:ubuntu 10.10 Qt Creator mysql5 qt4 以及相关的lib(这个问题困扰了不少家伙,我想如果用ubnuntu,这个问题应该不大)
本文假设你已经配置好了的情况下;
用向导建立一个 基于对话框的qt gui application
dialog 界面设计包括:
一个textEdit(QTextEdit),用来显示信息,两个button(QPushButton),一个叫做 connect,一个叫做 close。
下面是头文件dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include
#include "QtSql"
#include "QSqlDatabase"
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
QSqlDatabase db ; //数据库链接
private slots:
void on_close_clicked();
void on_connect_clicked();
};
#endif // DIALOG_H
下面是dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include "QtSql"
#include "QString"
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
this->db= QSqlDatabase::addDatabase("QMYSQL"); //设置类型为QMYSQL 或者其它
db.setDatabaseName("sgm");
db.setHostName("127.0.0.1");
db.setUserName("root");
db.setPassword("123");
ui->textEdit->setEnabled(false);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_connect_clicked()
{
bool success = db.open() ;
QString string ;
if(success)
{
string =QObject::tr("链接成功/t");
}
else
{
string =QObject::tr("失败/n");
}
ui->textEdit->setText(string);
}
void Dialog::on_close_clicked()
{
db.close();
QString string ;
string = QObject::tr("已经关闭/n");
ui->textEdit->setText(string);
}
下面是main.cpp
#include
#include "dialog.h"
int main(int argc, char *argv[])
{
//设置编码格式,解决qt 中文乱码
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
下面附上 pro文件:
#-------------------------------------------------
#
# Project created by QtCreator 2010-11-30T17:03:34
#
#-------------------------------------------------
QT += core gui sql
TARGET = connect
TEMPLATE = app
SOURCES += main.cpp/
dialog.cpp
HEADERS += dialog.h
FORMS += dialog.ui
注意QT一行要加上 sql
当然我愿意不厌其烦的把最后一个dialog.ui文件贴出来,这样你所需要做的就只是拷贝了(不过要是你自己写,我会很欣慰):
我做demo一定要足够简单,所以上面仅仅显示了链接成功字样。
附上dialog.ui:
自己写完有个疑问:
事件一般都要有个connect吧,可是为什么这里没有connect呢?

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

There are four main index types in MySQL: B-Tree index, hash index, full-text index and spatial index. 1.B-Tree index is suitable for range query, sorting and grouping, and is suitable for creation on the name column of the employees table. 2. Hash index is suitable for equivalent queries and is suitable for creation on the id column of the hash_table table of the MEMORY storage engine. 3. Full text index is used for text search, suitable for creation on the content column of the articles table. 4. Spatial index is used for geospatial query, suitable for creation on geom columns of locations table.

TocreateanindexinMySQL,usetheCREATEINDEXstatement.1)Forasinglecolumn,use"CREATEINDEXidx_lastnameONemployees(lastname);"2)Foracompositeindex,use"CREATEINDEXidx_nameONemployees(lastname,firstname);"3)Forauniqueindex,use"CREATEU

The main difference between MySQL and SQLite is the design concept and usage scenarios: 1. MySQL is suitable for large applications and enterprise-level solutions, supporting high performance and high concurrency; 2. SQLite is suitable for mobile applications and desktop software, lightweight and easy to embed.

Indexes in MySQL are an ordered structure of one or more columns in a database table, used to speed up data retrieval. 1) Indexes improve query speed by reducing the amount of scanned data. 2) B-Tree index uses a balanced tree structure, which is suitable for range query and sorting. 3) Use CREATEINDEX statements to create indexes, such as CREATEINDEXidx_customer_idONorders(customer_id). 4) Composite indexes can optimize multi-column queries, such as CREATEINDEXidx_customer_orderONorders(customer_id,order_date). 5) Use EXPLAIN to analyze query plans and avoid

Using transactions in MySQL ensures data consistency. 1) Start the transaction through STARTTRANSACTION, and then execute SQL operations and submit it with COMMIT or ROLLBACK. 2) Use SAVEPOINT to set a save point to allow partial rollback. 3) Performance optimization suggestions include shortening transaction time, avoiding large-scale queries and using isolation levels reasonably.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor
