Qt mysql
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...
root@luo-ThinkPad-W540:sqldrivers# root@luo-ThinkPad-W540:sqldrivers# sudo apt install libqt5sql5-mysql Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: fcitx-libs libfcitx-qt0 libopencc1 libqt4-opengl libqtwebkit4 xbase-clients Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: libqt5sql5-mysql 0 upgraded, 1 newly installed, 0 to remove and 242 not upgraded. Need to get 44.1 kB of archives. After this operation, 238 kB of additional disk space will be used. Get:1 http://cn.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 libqt5sql5-mysql amd64 5.9.5+dfsg-0ubuntu2.5 [44.1 kB] Fetched 44.1 kB in 2s (22.1 kB/s) Selecting previously unselected package libqt5sql5-mysql:amd64. (Reading database ... 231530 files and directories currently installed.) Preparing to unpack .../libqt5sql5-mysql_5.9.5+dfsg-0ubuntu2.5_amd64.deb ... Unpacking libqt5sql5-mysql:amd64 (5.9.5+dfsg-0ubuntu2.5) ... Setting up libqt5sql5-mysql:amd64 (5.9.5+dfsg-0ubuntu2.5) ... root@luo-ThinkPad-W540:sqldrivers#
#########################################
QQ 3087438119