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

时间:2011-07-31 17:57:59

标签: qt insert sqlite qtsql

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

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,“无法获取行”,“无查询”)

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

2 个答案:

答案 0 :(得分:4)

请勿将字符串'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();

答案 1 :(得分:3)

朋友,我想你在查询时想念你的桌面名字。

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);

我在那个地方放了问号