検索

Qt Creator 链接数据库mysql 的demo

Jun 07, 2016 pm 03:41 PM
creatordemomysqlデータベースリンク

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文件贴出来,这样你所需要做的就只是拷贝了(不过要是你自己写,我会很欣慰):

 

Qt Creator  链接数据库mysql 的demo

 

 

我做demo一定要足够简单,所以上面仅仅显示了链接成功字样。

 

附上dialog.ui:



 Dialog
 
 
  
    0
    0
    400
    300
  

 

 
   Dialog
 

 
  
   
     40
     200
     98
     27
   

  

  
    connect
  

 

 
  
   
     200
     200
     98
     27
   

  

  
    close
  

 

 
  
   
     60
     40
     211
     121
   

  

 

 

 
 
 

 

 

自己写完有个疑问:

事件一般都要有个connect吧,可是为什么这里没有connect呢?

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
MySQLでビューを使用することの限界は何ですか?MySQLでビューを使用することの限界は何ですか?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)supportallsqloperations、制限、dataManipulationswithjoinsorubqueries.2)それらは、特にパフォーマンス、特にパルフェクソルラージャターセット

MySQLデータベースのセキュリティ:ユーザーの追加と特権の付与MySQLデータベースのセキュリティ:ユーザーの追加と特権の付与May 14, 2025 am 12:09 AM

reperusermanmanagementInmysqliscialforenhancingsecurationsinginuring databaseaperation.1)usecreateusertoaddusers、指定connectionsourcewith@'localhost'or@'% '。

MySQLで使用できるトリガーの数にどのような要因がありますか?MySQLで使用できるトリガーの数にどのような要因がありますか?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers、しかしpracticalfactorsdeTerminetheireffectiveuse:1)serverconufigurationStriggermanagement; 2)complentiggersincreaseSystemload;

mysql:Blobを保管しても安全ですか?mysql:Blobを保管しても安全ですか?May 14, 2025 am 12:07 AM

はい、それはssafetostoreblobdatainmysql、butonsiderheSeCactors:1)Storagespace:blobscanconsumesificantspace.2)パフォーマンス:パフォーマンス:大規模なドゥエットブロブスメイズ階下3)backupandrecized recized recized recize

MySQL:PHP Webインターフェイスを介してユーザーを追加しますMySQL:PHP Webインターフェイスを介してユーザーを追加しますMay 14, 2025 am 12:04 AM

PHP Webインターフェイスを介してMySQLユーザーを追加すると、MySQLI拡張機能を使用できます。手順は次のとおりです。1。MySQLデータベースに接続し、MySQLI拡張機能を使用します。 2。ユーザーを作成し、CreateUserステートメントを使用し、パスワード()関数を使用してパスワードを暗号化します。 3. SQLインジェクションを防ぎ、MySQLI_REAL_ESCAPE_STRING()関数を使用してユーザー入力を処理します。 4.新しいユーザーに権限を割り当て、助成金ステートメントを使用します。

MySQL:BLOBおよびその他のNO-SQLストレージ、違いは何ですか?MySQL:BLOBおよびその他のNO-SQLストレージ、違いは何ですか?May 13, 2025 am 12:14 AM

mysql'sblobissuitable forstoringbinarydatawithinarationaldatabase、whileenosqloptionslikemongodb、redis、andcassandraofferferulesions forunstructureddata.blobissimplerbutcanslowdowdowd withwithdata

MySQLユーザーの追加:構文、オプション、セキュリティのベストプラクティスMySQLユーザーの追加:構文、オプション、セキュリティのベストプラクティスMay 13, 2025 am 12:12 AM

toaddauserinmysql、使用:createuser'username '@' host'identifidedby'password '; here'showtodoitsely:1)chosehostcarefilytoconを選択しますTrolaccess.2)setResourcelimitslikemax_queries_per_hour.3)usestrong、uniquasswords.4)endforcessl/tlsconnectionswith

MySQL:文字列データ型の一般的な間違いを回避する方法MySQL:文字列データ型の一般的な間違いを回避する方法May 13, 2025 am 12:09 AM

toavoidcommonMonmistakeswithStringDatatypesinmysql、undultingStringTypenuste、choosetherightType、andManageEncodingandCollat​​ionsEttingtingive.1)U​​secharforfixed-LengthStrings、Varcharforaible Length、AndText/Blobforlardata.2)setCurrectCherts

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール