TEMPLATE = appTARGET =DEPENDPATH += .INCLUDEPATH += .CONFIG += qt debug pluginQT += sqlQTPLUGIN += qsqlmysql# InputHEADERS += qsql.hSOURCES += main.cxx
#include <QApplication>#include "qsql.h"int main(int argc, char *argv[]){ QApplication app(argc, argv); sqltmp db; return app.exec();}
#ifndef aa_#define aa_#include <QSqlDatabase>#include <QWidget>class sqltmp : public QWidget { Q_OBJECT public: sqltmp(QWidget *parent=0):QWidget(parent){ QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); }};#endif
an9bit@bithome% qmake-qt4 -spec freebsd-g++an9bit@bithome%make++ -c -pipe -g -Wall -W -DQT_PLUGIN -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/share/qt4/mkspecs/freebsd-g++ -I. -I/usr/local/include/QtCore -I/usr/local/include/QtCore -I/usr/local/include/QtGui -I/usr/local/include/QtGui -I/usr/local/include/QtSql -I/usr/local/include/QtSql -I/usr/local/include -I. -I. -I. -I/usr/local/include -o main.o main.cxx/usr/local/bin/moc-qt4 -DQT_PLUGIN -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/share/qt4/mkspecs/freebsd-g++ -I. -I/usr/local/include/QtCore -I/usr/local/include/QtCore -I/usr/local/include/QtGui -I/usr/local/include/QtGui -I/usr/local/include/QtSql -I/usr/local/include/QtSql -I/usr/local/include -I. -I. -I. -I/usr/local/include qsql.h -o moc_qsql.cppc++ -c -pipe -g -Wall -W -DQT_PLUGIN -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/share/qt4/mkspecs/freebsd-g++ -I. -I/usr/local/include/QtCore -I/usr/local/include/QtCore -I/usr/local/include/QtGui -I/usr/local/include/QtGui -I/usr/local/include/QtSql -I/usr/local/include/QtSql -I/usr/local/include -I. -I. -I. -I/usr/local/include -o moc_qsql.o moc_qsql.cppc++ -pthread -o mysql main.o moc_qsql.o -L/usr/local/lib -lQtSql -L/usr/local/lib -pthread -pthread -lQtGui -L/usr/X11R6/lib -laudio -lXt -lpng -lSM -lICE -pthread -pthread -lXi -lXrender -lXrandr -lXfixes -lXcursor -lXinerama -lfreetype -lfontconfig -lXext -lX11 -lQtCore -lz -lm -pthread -lgthread-2.0 -lglib-2.0 -liconv -L/usr/local/lib/plugins/sqldrivers/ -lqsqlmysql/usr/local/lib/plugins/sqldrivers//libqsqlmysql.so: undefined reference to `QMYSQLDriver::staticMetaObject'/usr/local/lib/plugins/sqldrivers//libqsqlmysql.so: undefined reference to `vtable for QMYSQLDriver'*** Error code 1
#include <QtSql>#include <QApplication>#include <QMessagebox>bool createConnection();int main(int argc, char *argv[]){ QApplication app(argc, argv); if (!createConnection()) return 1; return app.exec();} bool createConnection(){ QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setDatabaseName("base"); db.setUserName("username"); db.setPassword("password"); if (!db.open()) { QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text()); return false; } return true;}