Qt - QSql无法获取行,尝试插入时无查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt - QSql无法获取行,尝试插入时无查询相关的知识,希望对你有一定的参考价值。

我正在尝试将数据插入表中。这是代码:

void AddContacts::saveContact() {

    QString first_name = ui->lineFirstName->text();
    QString last_name = ui->lineLastName->text();
    QString street_address = ui->lineStreetAddress->text();
    QString city = ui->lineCity->text();
    QString state = ui->comboBoxState->currentText();
    QString zip_code = ui->lineZip->text();
    QString personal_email = ui->linePersonalEmail->text();
    QString work_email = ui->lineWorkEmail->text();
    QString home_phone = ui->lineHomePhone->text();
    QString cellular_phone = ui->lineCellularPhone->text();
    QString work_phone = ui->lineWorkPhone->text();
    QString pager = ui->linePager->text();
    QString facebook = ui->lineFacebook->text();
    QString twitter = ui->lineTwitter->text();
    QString notes = ui->textEditNotes->toPlainText();

    // Insert into the database
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("db.db3");
    db.open();

    QSqlQuery qry;
    QString query = "INSERT INTO (id, first_name, last_name, street_address, city, state, zip_code, personal_email, work_email, home_phone, cellular_phone, work_phone, pager, facebook, twitter, notes) VALUES('null','" + first_name + "','" + last_name +"','" + street_address + "','" + city + "','" + state + "','" + zip_code + "','" + personal_email + "','" + work_email + "','" + home_phone + "','" + cellular_phone + "','" + work_phone + "','" + pager + "','" + facebook + "','" + twitter + "', '" + notes + "')";
    qry.prepare(query);

    if(!qry.exec()) {
        qDebug() << qry.lastError();
    }
    else {
        qDebug() << "Success!";
    }

这是我用来创建表格的内容:

CREATE TABLE contacts (
id int primary key,
first_name varchar(20),
last_name varchar(20),
street_address text,
city varchar(30),
state varchar(20),
zip_code varchar(10),
personal_email varchar(40),
work_email varchar(40),
home_phone varchar(12),
cellular_phone varchar(12),
work_phone varchar(12),
pager varchar(12),
facebook text,
twitter varchar(20),
notes text);

我收到这个错误:

QSqlError(-1,“无法获取行”,“无查询”)

我不确定我做错了什么。你能找到什么吗?

答案

不要插入字符串'null'作为列id的值。请改用有效整数。如果你真的希望第一行有一个NULL id,那么SQL语句就像这个... VALUES (NULL, ...

您的代码很容易出错,因为您没有在输入中转义撇号。如果您的某些文本字段包含',则您的SQL语句将失败。您应该将列绑定到变量。这是来自Qt参考,你应该使用这种方法:

 QSqlQuery query;
 query.prepare("INSERT INTO contacts (id, first_name, last_name) "
               "VALUES (:id, :first_name, :last_name)");
 query.bindValue(":id", 1001);
 query.bindValue(":first_name", first_name);
 query.bindValue(":last_name", last_name);
 query.exec();
另一答案

嗨朋友,我想你在查询中想念你的表名。

QString query = "INSERT INTO ??? (id, first_name, last_name, street_address, city, state, zip_code, personal_email, work_email, home_phone, cellular_phone, work_phone, pager, facebook, twitter, notes) VALUES('null','" + first_name + "','" + last_name +"','" + street_address + "','" + city + "','" + state + "','" + zip_code + "','" + personal_email + "','" + work_email + "','" + home_phone + "','" + cellular_phone + "','" + work_phone + "','" + pager + "','" + facebook + "','" + twitter + "', '" + notes + "')";
qry.prepare(query);

我把问号放在那个地方

以上是关于Qt - QSql无法获取行,尝试插入时无查询的主要内容,如果未能解决你的问题,请参考以下文章

我正在尝试从 qSQL 数据库中检索数据

SqlDataAdapter 插入后无法获取行

QT ODBC 驱动程序 .mdb ms-access 插入在 1000 行以上时不起作用(Windows)

qt编译oracle驱动,qt 5.12 连接 oracle 数据库示例代码

qt 如何从数据库中获得表名

查询GPU时无进程运行,但是显存却被占用了