How to use database plug-ins to connect ODBC (call static plug-ins) in a statically compiled QT 5.5.1

Source: Internet
Author: User



Some time ago because of the working relationship, you need to write a desktop software that inserts data into SQL Server 2012.



Because of the previous use of MFC, accidental contact with the QT, was its simplicity and surprise to the, then resolutely and then transferred to the embrace of QT, haha ...






Needless to say, I'm using the latest QT 5.5.1 version (QT 5.5.1 for Windows 32-bit MinGW), after looking through the help documentation,



Finally, the program compiles and runs normally. While I was glad to deliver it, I had a problem, and when the program ran on the other computer,



Prompt missing dynamic library! And the missing libraries of different computers are not necessarily the same! After struggling to find the missing library, the original full sense of accomplishment reduced a lot ...



In the online search for help, I think of the way of static compilation ...








How to build a static Qt version for Windows with GCC








Say do, according to the official website instructions, no brain copy steps compiled out of the static



Version, but it's a problem when it's used:



When running the prompt:



"Driver not loaded Driver not loaded"
Available drivers:
"Qsqlite"



Wipe, the original default does not compile the ODBC driver, OK, according to the QT Help document instructions compiled LIBQSQLODBCD.A, LIBQSQLODBC.A
These two files, and then take it for granted that the two files in the%qt/plugins folder Sqldrivers, you guess what, there is no this folder!



Well, since there is no, then I will build one for you, but there is no ghost to use ...



Finally, emperor, I learned that I now use the ODBC driver is called "Call Static plug-ins", Qt just have an article about the article:


How to Create Qt Plugins



Follow the instructions above to include the following statement in the. Pro file:






qtplugin+= QSQLODBC






and add statements in the. cpp file:






#include <QtPlugin>



Q_import_plugin (Qodbcdriverplugin) here to say, the name of the plug-in, which is the "Qodbcdriverplugin", where to find it? This gets us to compile this driver's. Pro file to find, here is Odbc.pro, (directory:%qt\src\qt-everywhere-opensource-src-5.5.1\qtbase\src\plugins\sqldrivers\ ODBC). In addition, the two. prl files compiled with the. A library are placed in the%qt\lib directory, and the two files are edited to modify the static library path in the Qmake_prl_libs to the path of the static library you compiled. Finish above, on the execution once clean, qmake recompile OK, this time is not able to connect on ah, haha ... (This process of sadness, finally in exchange for the joy of harvesting ...) Ah ah AH) The following test Demo code is attached Statictest.pro

  

[CPP]View PlainCopy



#-------------------------------------------------

#

# Project created by Qtcreator 2016-01-16t20:55:32

#

#-------------------------------------------------

QT + + core GUI sql

GreaterThan (Qt_major_version, 4): QT + = Widgets

Qtplugin + = Qsqlodbc

TEMPLATE = App

SOURCES + = Main.cpp\

Mainwindow.cpp

HEADERS + = Mainwindow.h

FORMS + = Mainwindow.ui


---------------------------------------------------
Mainwindow.cpp

[CPP]View PlainCopy


#include "Mainwindow.h"

#include "Ui_mainwindow.h"

#include <QDebug>

#include <QtSql/QSqlDatabase>

#include <QtSql/QSqlError>

#include <QtSql/QSqlDriver>

#include <QtPlugin>

Q_import_plugin (Qodbcdriverplugin)

Mainwindow::mainwindow (Qwidget *parent):

Qmainwindow (parent),

UI (new Ui::mainwindow)

{

UI->SETUPUI (this);

}

Mainwindow::~mainwindow ()

{

Delete UI;

}

void Mainwindow::on_pushbutton_clicked ()

{

Qdebug () <<"haha";

Qsqldatabase QDB = qsqldatabase::adddatabase ("Qodbc");

Qdb.sethostname ("127.0.0.1");

Qdb.setdatabasename ("Driver={sql server};server=127.0.0.1;database=mytempdb;");

Qdb.setusername ("sa");

Qdb.setpassword ("123456");

bool IsOpen = Qdb.open ();

if (IsOpen)

{

Qdebug () <<"Connect to Database Success.";

}

Else

{

Qdebug () <<"Connect failed!";

Qsqlerror qerror = Qdb.lasterror ();

Qdebug () <<qerror.text ();

}

Qdebug () << "Available drivers:";

Qstringlist drivers = qsqldatabase::d rivers ();

foreach (QString driver, drivers)

Qdebug () << "\ t" << driver;

}




*/






*--------------------------------------



* * Edit by: Side dishes



* * time:2016-01-19



*--------------------------------------



*/



http://blog.csdn.net/humadivinity/article/details/50545100



How to use database plug-ins to connect ODBC (call static plug-ins) in a statically compiled QT 5.5.1


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.