【问题标题】:Qt SQlite cannot create tableQt SQlite 无法创建表
【发布时间】:2020-06-04 01:09:14
【问题描述】:

我试图让一个按钮点击创建一个表格:

void summary::on_pushButton_saveSummary_clicked()
{
if(db.open())
    {
        query.exec("create table parrot (total int, defects int, rust int)");
        if (!query.exec())
        {
        qDebug() << query.lastError();
        }
else
{
   qDebug() << db.lastError();
}
}

上面产生:QSqlError("1", "Unable to fetch row", "table parrot already exists") 如果我进行这些更改:

query.prepare("create table parrot (total int, defects int, rust int)");
query.exec();

我收到新错误:QSqlError("", "Unable to fetch row", "No query")

db.setDatabaseName("/home/igraves/Databases/testdb");
db.open();

以上是在构造函数中。

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

以上在我的.h中

我已经尝试了其他人类似问题的所有解决方案,但无济于事。我希望其他人有一个新的想法。

【问题讨论】:

  • table parrot 已经存在 看来你应该检查一下。
  • 使用CREATE TABLE IF NOT EXISTS parrot (total int, defects int, rust int)
  • 数据库是空的,所以没有表可以触发该事件。
  • @ig213 你如何创建数据库?也许你已经在其他地方创建了它,检查构建文件夹
  • @ellyanesc 我正在使用数据库浏览器,但您的回答解决了它。

标签: c++ sqlite qt


【解决方案1】:

@ellanesc 是正确的,我之前尝试过该解决方案但没有成功,显然在我疯狂地重新排列代码时,它现在可以工作了。谢谢!

CREATE TABLE IF NOT EXISTS parrot (total int, defects int, rust int)

以上解决了问题

【讨论】:

    猜你喜欢
    • 2021-09-03
    • 1970-01-01
    • 2018-10-05
    • 2020-10-07
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多