VS编译QT程序调用SQLite数据库研究

环境:win8 + VS2008 +QT4.8.1 + QT Creator2.4.1,QT4.8.1是利用vs2008静态编译的


问题描述:

直接运行QT自带的SQLite操作例子工程tableModel,用QT自带的编译器MinGW编译运行没有问题,但是把QT工程转成VS工程后,用VS编译没有问题,运行的时候调用db.open的时候失败,失败的原因是“diverError=Driver not loaded”


问题解决研究:

1.把QT4.8.1利用vs2008重新静态编译,要求编译的时候把sql和SQlite部分加入,尝试后不行。

2.把plugins文件夹下面的sqldrivers文件夹直接复制到执行程序同级目录里面,尝试后不行


未完继续研究。。。


参考文章:

http://www.qtcn.org/bbs/simple/?t35292.html

http://blog.csdn.net/shuixin536/article/details/8562639

http://www.qtcentre.org/threads/32585-QT-amp-SQLite-driver-not-loaded

http://stackoverflow.com/questions/5151279/qsqlite-driver-not-loaded-where-to-put-qt-database-driver-plugins

http://stackoverflow.com/questions/32442744/qsqlite-driver-not-loaded


代码例子:

static bool createConnection()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(":memory:");
    if (!db.open()) {
        QMessageBox::critical(0, qApp->tr("Cannot open database"),
            qApp->tr("Unable to establish a database connection.\n"
                     "This example needs SQLite support. Please read "
                     "the Qt SQL driver documentation for information how "
                     "to build it.\n\n"
                     "Click Cancel to exit."), QMessageBox::Cancel);
        return false;
    }

    QSqlQuery query;
    query.exec("create table person (id int primary key, "
               "firstname varchar(20), lastname varchar(20))");
    query.exec("insert into person values(101, 'Danny', 'Young')");
    query.exec("insert into person values(102, 'Christine', 'Holand')");
    query.exec("insert into person values(103, 'Lars', 'Gordon')");
    query.exec("insert into person values(104, 'Roberto', 'Robitaille')");
    query.exec("insert into person values(105, 'Maria', 'Papadopoulos')");

    query.exec("create table items (id int primary key,"
                                             "imagefile int,"
                                             "itemtype varchar(20),"
                                             "description varchar(100))");
    query.exec("insert into items "
               "values(0, 0, 'Qt',"
               "'Qt is a full development framework with tools designed to "
               "streamline the creation of stunning applications and  "
               "amazing user interfaces for desktop, embedded and mobile "
               "platforms.')");
    query.exec("insert into items "
               "values(1, 1, 'Qt Quick',"
               "'Qt Quick is a collection of techniques designed to help "
               "developers create intuitive, modern-looking, and fluid "
               "user interfaces using a CSS & JavaScript like language.')");
    query.exec("insert into items "
               "values(2, 2, 'Qt Creator',"
               "'Qt Creator is a powerful cross-platform integrated "
               "development environment (IDE), including UI design tools "
               "and on-device debugging.')");
    query.exec("insert into items "
               "values(3, 3, 'Qt Project',"
               "'The Qt Project governs the open source development of Qt, "
               "allowing anyone wanting to contribute to join the effort "
               "through a meritocratic structure of approvers and "
               "maintainers.')");

    query.exec("create table images (itemid int, file varchar(20))");
    query.exec("insert into images values(0, 'images/qt-logo.png')");
    query.exec("insert into images values(1, 'images/qt-quick.png')");
    query.exec("insert into images values(2, 'images/qt-creator.png')");
    query.exec("insert into images values(3, 'images/qt-project.png')");

    return true;
}

你可能感兴趣的:(QT)