linux - QT下打开已有的sqlite3数据库

标签 linux qt sqlite

我对 QT 和 SQLite DBMS 还很陌生。我试图在 ubuntu Linux 下打开一个使用“sqlite3”命令行程序创建的现有数据库。我尝试使用以下代码在 QT 下访问的同一数据库:

void MainWindow::func()
{
    QSqlQuery query;
    accounts_db = new QSqlDatabase();
    *accounts_db = QSqlDatabase::addDatabase("QSQLITE");
    perror("? ");
accounts_db->setDatabaseName("/home/user/xyz.db");
QSqlError *a = new QSqlError();
*a = accounts_db->lastError();
perror(a->text().toLatin1());
if (!accounts_db->open()) {
    perror("database open error :");
}
if ( !accounts_db->isOpen() ) {
    perror("database is not open");
}
query.exec("select accno,branchcode,fname,lname,curbalance,accdate from accounts");
while(query.next()) {
    QString str = query.value(0).toString();
    std::cerr << qPrintable(str) << std::endl;
}
end:
;
}

失败并出现以下错误...

No such file or directory
: Invalid argument
QSqlQuery::exec: database not open

请注意,我在 adddatabase() 之后得到“没有这样的文件或目录”,不知道它在说哪个文件。还要注意 isOpen() 和 open() 正在返回“true”(???)。 “数据库未打开”错误来自 db.exec() 调用(...我想...)。

迫切需要指导...

最佳答案

不带参数的 QSqlQuery 的构造函数使用应用程序的默认数据库。也许还没有定下来。使用指定查询需要使用的数据库的构造函数:

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "connection_name");
// Open db...
QSqlQuery query(db);
if (!query.exec(...)) {
   // ...
}
// ...

注意之后如何关闭连接。

编辑:这是我刚刚编写的测试,正在我的系统上运行。您可能想尝试一下。

#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QVariant>

int main(int argc, char *argv[])
{
    // Create database.
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "Connection");
    db.setDatabaseName("/tmp/test.db");
    if (!db.open()) {
        qDebug("Error occurred opening the database.");
        qDebug("%s.", qPrintable(db.lastError().text()));
        return -1;
    }

    // Insert table.
    QSqlQuery query(db);
    query.prepare("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, text TEXT)");
    if (!query.exec()) {
        qDebug("Error occurred creating table.");
        qDebug("%s.", qPrintable(db.lastError().text()));
        return -1;
    }

    // Insert row.
    query.prepare("INSERT INTO test VALUES (null, ?)");
    query.addBindValue("Some text");
    if (!query.exec()) {
        qDebug("Error occurred inserting.");
        qDebug("%s.", qPrintable(db.lastError().text()));
        return -1;
    }

    // Query.
    query.prepare("SELECT * FROM test");
    if (!query.exec()) {
        qDebug("Error occurred querying.");
        qDebug("%s.", qPrintable(db.lastError().text()));
        return -1;
    }
    while (query.next()) {
        qDebug("id = %d, text = %s.", query.value(0).toInt(),
               qPrintable(query.value(1).toString()));
    }

    return 0;
}

关于linux - QT下打开已有的sqlite3数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7817864/

相关文章:

c++ - 为什么即使我设置了它的 repeat = false,定时器也能工作这么多次?

c++ - Qt moc 文件没有扩展 #include "foo.h"之前定义的宏

c - 在 C 中使用通配符 % 和 sqlite3_mprintf %q 的 SQLite 查询

sql - 新手SQL表设计

python - 在 init.d 脚本中使用 Python 的特殊注意事项?

linux - 如何安装 svn post-commit 钩子(Hook)

Linux 无法删除由 www-data 创建的文件

linux - 添加具有不同标签的列

c - 函数 sqlite3_open 不是打开 db 文件,而是创建一个

c++ - 更新不调用paintEvent?