首页 > 数据库技术 > 详细

Qt mysql

时间:2021-04-29 09:51:58      阅读:23      评论:0      收藏:0      [点我收藏+]

Qt mysql

 

确保mysql已经安装

luo@luo-ThinkPad-W540:~$ 
luo@luo-ThinkPad-W540:~$ whereis mysql.h
mysql: /usr/bin/mysql /usr/lib/mysql /etc/mysql /usr/include/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
luo@luo-ThinkPad-W540:~$ 
luo@luo-ThinkPad-W540:~$ 
luo@luo-ThinkPad-W540:~$ 
luo@luo-ThinkPad-W540:~$ 
luo@luo-ThinkPad-W540:~$ whereis libmysqlclient.so
libmysqlclient: /usr/lib/x86_64-linux-gnu/libmysqlclient.a /usr/lib/x86_64-linux-gnu/libmysqlclient.so
luo@luo-ThinkPad-W540:~$ 
luo@luo-ThinkPad-W540:~$ 
luo@luo-ThinkPad-W540:~$ 

 

 

 

TestSql.pro

#-------------------------------------------------
#
# Project created by QtCreator 2021-04-29T08:01:26
#
#-------------------------------------------------

QT       += core gui
QT       +=sql

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = TestSql
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES +=         main.cpp         mainwindow.cpp

HEADERS +=         mainwindow.h

FORMS +=         mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

 

 

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void connectMySql();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

 

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"


#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QSqlError>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connectMySql();
}

MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::connectMySql()
{
    //Database driver
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    //database ip
    db.setHostName("localhost");
    db.setUserName("root");
    db.setPassword("123456");
    db.setPort(3306);
    db.setDatabaseName("ML_data");

    bool isConnect = db.open();
    if(isConnect)
    {
        qDebug()<<"connect mysql...";
    }
    else {
        qDebug()<<"connect failed...";
    }

    //close connect
    db.close();
}




///////////////

 

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

 

 

build log

08:22:30: Running steps for project TestSql...
08:22:30: Configuration unchanged, skipping qmake step.
08:22:30: Starting: "/usr/bin/make" -j8
g++ -c -pipe -g -std=gnu++11 -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_SQL_LIB -DQT_CORE_LIB -I../TestSql -I. -I/opt/Qt5.12.0/5.12.0/gcc_64/include -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtWidgets -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtGui -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtSql -I/opt/Qt5.12.0/5.12.0/gcc_64/include/QtCore -I. -isystem /usr/include/libdrm -I. -I/opt/Qt5.12.0/5.12.0/gcc_64/mkspecs/linux-g++ -o mainwindow.o ../TestSql/mainwindow.cpp
g++ -Wl,-rpath,/opt/Qt5.12.0/5.12.0/gcc_64/lib -o TestSql main.o mainwindow.o moc_mainwindow.o   -L/opt/Qt5.12.0/5.12.0/gcc_64/lib -lQt5Widgets -lQt5Gui -lQt5Sql -lQt5Core -lGL -lpthread   
08:22:31: The process "/usr/bin/make" exited normally.
08:22:31: Elapsed time: 00:01.

 

run log 1

08:21:58: Starting /home/luo/Desktop/MyFile/QtProject/build-TestSql-Desktop_Qt_5_12_0_GCC_64bit-Debug/TestSql...
08:22:12: The program has unexpectedly finished.
08:22:12: The process was ended forcefully.
08:22:12: /home/luo/Desktop/MyFile/QtProject/build-TestSql-Desktop_Qt_5_12_0_GCC_64bit-Debug/TestSql crashed.

08:25:32: Starting /home/luo/Desktop/MyFile/QtProject/build-TestSql-Desktop_Qt_5_12_0_GCC_64bit-Debug/TestSql...
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
connect failed...

 

 

#########################################

Qt mysql

原文:https://www.cnblogs.com/herd/p/14716540.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!