QT 关于Driver not loaded 与 结构体的构造函数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/theArcticOcean/article/details/79342971

QT 关于Driver not loaded

在程序中使用SQLite数据库,如下的代码:

    QSqlDatabase db;
    QSqlQuery query;
    db = QSqlDatabase::addDatabase("QSQLITE");
      if(db.open()){
        if(!query.exec("create table student(name text)")){
            qDebug()<<query.lastError();
        }
        db.close();
    }

总是报错:Driver not loaded Driver not loaded.
在帮助文档中,查看说明,有这样的话:

Ensure that you are using a shared Qt library; you cannot use the plugins with a static build.
Ensure that the plugin is in the correct directory. You can use QApplication::libraryPaths() to determine where Qt looks for plugins.
Ensure that the client libraries of the DBMS are available on the system. On Unix, run the command ldd and pass the name of the plugin as parameter, for example ldd libqsqlmysql.so. You will get a warning if any of the client libraries couldn't be found. On Windows, you can use Visual Studio's dependency walker. With Qt Creator, you can update the PATH environment variable in the Run section of the Project panel to include the path to the folder containing the client libraries.
Compile Qt with QT_DEBUG_COMPONENT defined to get very verbose debug output when loading plugins.

奇怪,我查看QApplication::libraryPaths()输出的信息,确信plugin路径正确。动态库文件也存在:/Users/weiyang/Qt5.9.2/5.9.2/clang_64/plugins/sqldrivers。仔细查看输出,我观察到在此之前还有QSqlQuery::exec: database not open
我试着这样改:

    QSqlDatabase db;
    db = QSqlDatabase::addDatabase("QSQLITE");
    QSqlQuery query;

先创建好Sqlite connection class,然后创建query对象。OK!

结构体构造函数

假设结构体中包含有参构造函数,如下:

typedef struct _Data {
    int a;
    string s;
    _Data(int _a, string _s){ a = _a; s = _s; }
}Data;

那么,就不能使用类似于这样的语句:sentenceUnit senUnit
C++会尝试匹配move constructure, copy constructrue, 有参构造函数。

    Data d(1,"hello");
    Data d1(d);             //copy constructrue
    Data d2(std::move(d));  //move constructure.

如果这些都没有,那么编译器则会报出不匹配的错误。
我们需要自己写出无参构造函数。
那么如果是类的话,情况是类似的:

class Data {
public:
    int a;
    string s;
    Data(int _a, string _s){ a = _a; s = _s; }
};

int main()
{
    Data d = Data(1,"hello"); //we can't use 'Data d()', or compiler regard it as a functon.
    Data d1(d);             //copy constructrue
    Data d2(std::move(d));  //move constructure.
    Data d3;                //error: no matching constructure.
    return 0;
}
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页