当前位置: 技术问答>linux和unix
QT 与 MySQL 连接的问题
来源: 互联网 发布时间:2015-09-30
本文导语: connection.h: #ifndef CONNECTION_H #define CONNECTION_H #include inline bool createOneConnection() { QSqlDatabase *db; db = QSqlDatabase::addDatabase("QMYSQL3"); db->setHostName("127.0.0.1"); db->setDatabaseName("qttest"); db->setUs...
connection.h:
#ifndef CONNECTION_H
#define CONNECTION_H
#include
inline bool createOneConnection()
{
QSqlDatabase *db;
db = QSqlDatabase::addDatabase("QMYSQL3");
db->setHostName("127.0.0.1");
db->setDatabaseName("qttest");
db->setUserName("root");
if (!db->open()) {
db->lastError().showMessage();
return false; }
return false;
}
return true;
}
#endif
main.cpp:
#include
#include
#include "connection.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
if (createOneConnection())
return 1;
QSqlQuery query;
query.exec("CREATE TABLE artist ("
"id INTEGER PRIMARY KEY, "
"name VARCHAR(40) NOT NULL, "
"country VARCHAR(40))");
query.exec("CREATE TABLE cd ("
"id INTEGER PRIMARY KEY, "
"artistid INTEGER NOT NULL, "
"title VARCHAR(40) NOT NULL, "
"year INTEGER NOT NULL)");
query.exec("CREATE TABLE track ("
"id INTEGER PRIMARY KEY, "
"cdid INTEGER NOT NULL, "
"number INTEGER NOT NULL, "
"title VARCHAR(40) NOT NULL, "
"duration INTEGER NOT NULL)");
#if 1
query.exec("INSERT INTO artist "
"VALUES (1001, 'Gluecifer', 'Norway')");
query.exec("INSERT INTO artist "
"VALUES (1002, 'The Sounds', 'Sweden')");
query.exec("INSERT INTO artist "
"VALUES (1003, 'Melvins', 'USA')");
query.exec("INSERT INTO artist "
"VALUES (1004, 'Strapping Young Lads', 'Canada')");
query.exec("INSERT INTO cd "
"VALUES (2001, 1001, 'Riding the Tiger', 1997)");
query.exec("INSERT INTO cd "
"VALUES (2002, 1001, 'Easy Living', 2002)");
query.exec("INSERT INTO cd "
"VALUES (2003, 1002, 'Living in America', 2002)");
query.exec("INSERT INTO cd "
"VALUES (2004, 1003, 'Bullhead', 1991)");
query.exec("INSERT INTO cd "
"VALUES (2005, 1003, 'Stag', 1996)");
query.exec("INSERT INTO cd "
"VALUES (2006, 1003, 'Hostile Ambient Takeover', "
"2002)");
query.exec("INSERT INTO cd "
"VALUES (2007, 1003, '26 Songs', 2003)");
query.exec("INSERT INTO cd "
"VALUES (2008, 1004, 'City', 1997)");
query.exec("INSERT INTO cd "
"VALUES (2009, 1004, 'No Sleep Till Bedtime', "
"1998)");
query.exec("INSERT INTO cd "
"VALUES (2010, 1004, 'Syl', 2003)");
query.exec("INSERT INTO track "
"VALUES (3001, 2004, 01, 'Boris', 514)");
query.exec("INSERT INTO track "
"VALUES (3002, 2004, 02, 'Anaconda', 143)");
query.exec("INSERT INTO track "
"VALUES (3003, 2004, 03, 'Ligature', 229)");
query.exec("INSERT INTO track "
"VALUES (3004, 2004, 04, 'It''s Shoved', 155)");
query.exec("INSERT INTO track "
"VALUES (3005, 2004, 05, 'Zodiac', 254)");
query.exec("INSERT INTO track "
"VALUES (3006, 2004, 06, 'If I Had An Exorcism', "
"187)");
query.exec("INSERT INTO track "
"VALUES (3007, 2004, 07, 'Your Blessed', 339)");
query.exec("INSERT INTO track "
"VALUES (3008, 2004, 08, 'Cow', 271)");
#endif
return 0;
}
运行时弹出对话框:Driver not loaded Driver not loaded.
实在不知道是什么原因。
为什么QT与MySQl连不上呢?
在网上看到很多人问这个问题,可是没人答得出来,
还望高手不吝赐教.
#ifndef CONNECTION_H
#define CONNECTION_H
#include
inline bool createOneConnection()
{
QSqlDatabase *db;
db = QSqlDatabase::addDatabase("QMYSQL3");
db->setHostName("127.0.0.1");
db->setDatabaseName("qttest");
db->setUserName("root");
if (!db->open()) {
db->lastError().showMessage();
return false; }
return false;
}
return true;
}
#endif
main.cpp:
#include
#include
#include "connection.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
if (createOneConnection())
return 1;
QSqlQuery query;
query.exec("CREATE TABLE artist ("
"id INTEGER PRIMARY KEY, "
"name VARCHAR(40) NOT NULL, "
"country VARCHAR(40))");
query.exec("CREATE TABLE cd ("
"id INTEGER PRIMARY KEY, "
"artistid INTEGER NOT NULL, "
"title VARCHAR(40) NOT NULL, "
"year INTEGER NOT NULL)");
query.exec("CREATE TABLE track ("
"id INTEGER PRIMARY KEY, "
"cdid INTEGER NOT NULL, "
"number INTEGER NOT NULL, "
"title VARCHAR(40) NOT NULL, "
"duration INTEGER NOT NULL)");
#if 1
query.exec("INSERT INTO artist "
"VALUES (1001, 'Gluecifer', 'Norway')");
query.exec("INSERT INTO artist "
"VALUES (1002, 'The Sounds', 'Sweden')");
query.exec("INSERT INTO artist "
"VALUES (1003, 'Melvins', 'USA')");
query.exec("INSERT INTO artist "
"VALUES (1004, 'Strapping Young Lads', 'Canada')");
query.exec("INSERT INTO cd "
"VALUES (2001, 1001, 'Riding the Tiger', 1997)");
query.exec("INSERT INTO cd "
"VALUES (2002, 1001, 'Easy Living', 2002)");
query.exec("INSERT INTO cd "
"VALUES (2003, 1002, 'Living in America', 2002)");
query.exec("INSERT INTO cd "
"VALUES (2004, 1003, 'Bullhead', 1991)");
query.exec("INSERT INTO cd "
"VALUES (2005, 1003, 'Stag', 1996)");
query.exec("INSERT INTO cd "
"VALUES (2006, 1003, 'Hostile Ambient Takeover', "
"2002)");
query.exec("INSERT INTO cd "
"VALUES (2007, 1003, '26 Songs', 2003)");
query.exec("INSERT INTO cd "
"VALUES (2008, 1004, 'City', 1997)");
query.exec("INSERT INTO cd "
"VALUES (2009, 1004, 'No Sleep Till Bedtime', "
"1998)");
query.exec("INSERT INTO cd "
"VALUES (2010, 1004, 'Syl', 2003)");
query.exec("INSERT INTO track "
"VALUES (3001, 2004, 01, 'Boris', 514)");
query.exec("INSERT INTO track "
"VALUES (3002, 2004, 02, 'Anaconda', 143)");
query.exec("INSERT INTO track "
"VALUES (3003, 2004, 03, 'Ligature', 229)");
query.exec("INSERT INTO track "
"VALUES (3004, 2004, 04, 'It''s Shoved', 155)");
query.exec("INSERT INTO track "
"VALUES (3005, 2004, 05, 'Zodiac', 254)");
query.exec("INSERT INTO track "
"VALUES (3006, 2004, 06, 'If I Had An Exorcism', "
"187)");
query.exec("INSERT INTO track "
"VALUES (3007, 2004, 07, 'Your Blessed', 339)");
query.exec("INSERT INTO track "
"VALUES (3008, 2004, 08, 'Cow', 271)");
#endif
return 0;
}
运行时弹出对话框:Driver not loaded Driver not loaded.
实在不知道是什么原因。
为什么QT与MySQl连不上呢?
在网上看到很多人问这个问题,可是没人答得出来,
还望高手不吝赐教.
|
没做过!跟踪进去哪错了?
|
你看看有没有安装QMYSQL3 这个QT下mysql驱动
可疑在QT开发环境的"Project"-->"Database connections"-->new 然后选择看看
可疑在QT开发环境的"Project"-->"Database connections"-->new 然后选择看看
|
如果你是从光盘安装的,在哪张碟子我不记得了,反正里面有的,名字好像是qt-xx的!
如果从从源代码安装,QT的帮助文档里面有安装方法:
How to build the plugin on Unix/Linux
You need the MySQL header files and as well as the shared library libmysqlclient.so. Depending on your Linux distribution you need to install a package which is usually called "mysql-devel".
Tell qmake where to find the MySQL header files and shared libraries (here it is assumed that MySQL is installed in /usr/local) and run make:
cd $QTDIR/plugins/src/sqldrivers/mysql
qmake -o Makefile "INCLUDEPATH+=/usr/local/include" "LIBS+=-L/usr/local/lib" mysql.pro
make
这个有点文体,就是连接的时候要加上-lmysqlclient,否则调试程序要出错,你可以试试把第二步改成:
qmake -o Makefile "INCLUDEPATH+=/usr/local/include" "LIB+=-L/usr/local/lib -lmysqlclient" mysql.pro
如果从从源代码安装,QT的帮助文档里面有安装方法:
How to build the plugin on Unix/Linux
You need the MySQL header files and as well as the shared library libmysqlclient.so. Depending on your Linux distribution you need to install a package which is usually called "mysql-devel".
Tell qmake where to find the MySQL header files and shared libraries (here it is assumed that MySQL is installed in /usr/local) and run make:
cd $QTDIR/plugins/src/sqldrivers/mysql
qmake -o Makefile "INCLUDEPATH+=/usr/local/include" "LIBS+=-L/usr/local/lib" mysql.pro
make
这个有点文体,就是连接的时候要加上-lmysqlclient,否则调试程序要出错,你可以试试把第二步改成:
qmake -o Makefile "INCLUDEPATH+=/usr/local/include" "LIB+=-L/usr/local/lib -lmysqlclient" mysql.pro
|
windows环境我就不太清楚了,在linux下,编译好的mysql的模块会在$QTDIR/plugins/sqldrivers下有个libqsqlmysql.so
我想windows下也应该有个类似的模块文件吧?
如果安装好了,在QT 开发界面的Project-->database connections--->new 然后选择driver可以看到QMYSQL3的.
请问,你的编译通过了哈??
我想windows下也应该有个类似的模块文件吧?
如果安装好了,在QT 开发界面的Project-->database connections--->new 然后选择driver可以看到QMYSQL3的.
请问,你的编译通过了哈??
|
没有编译起那个mysql模块?
那..................我也不晓得了!
mysql自带的有一套C API,用这个如何呢?
那..................我也不晓得了!
mysql自带的有一套C API,用这个如何呢?
|
libmysql.lib
这个你有吗?
然后还需要mysql的头文件,我的linux下的QT编译是成功了的!
这个你有吗?
然后还需要mysql的头文件,我的linux下的QT编译是成功了的!