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, "无法获取行", "无查询")
我不确定我可能做错了什么。你能找到什么吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要插入字符串
'null'
作为列id
的值。请改用有效的整数。如果你确实希望第一行有一个NULL
id,SQL 语句看起来像这样... VALUES (NULL, ...
。你的代码很容易出现错误,因为您没有在输入中转义撇号。如果您的某些文本字段包含
'
,您的 SQL 语句将失败。这是来自 Qt 引用和。你应该使用这种方法:Don't insert a string
'null'
as a value for columnid
. Use a valid integer instead. If you really want the first row to have aNULL
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:嗨朋友,我想你错过了查询中的表名。
我在那个地方打了问号
hi friend i think you miss your table name in query.
i put question mark in that place