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

发布于 2024-11-26 23:53:20 字数 2302 浏览 0 评论 0原文

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

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

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

I'm trying to insert data into a table. Here's the code:

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!";
    }

Here is what I used to create the table:

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

I'm getting this error:

QSqlError(-1, "Unable to fetch row", "No query")

I'm not sure what I could be doing wrong. Can you find anything?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑寡妇 2024-12-03 23:53:20

不要插入字符串 '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();

Don't insert a string 'null' as a value for column id. Use a valid integer instead. If you really want the first row to have a NULL id, the SQL statement looks like this ... VALUES (NULL, ....

Your code is very prone to errors since you don't escape apostrophes in your input. If some of your text fields contain a ', your SQL statement will fail. You should bind your columns to variables. This is from the Qt references and you should use this approach:

 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();
沩ん囻菔务 2024-12-03 23:53:20

嗨朋友,我想你错过了查询中的表名。

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

我在那个地方打了问号

hi friend i think you miss your table name in query.

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

i put question mark in that place

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文