qt sqlite插入自动增量表产生两行
你好 我有一个 sqlite 数据库,我正在使用 sqlite 数据库驱动程序中内置的 qts 进行操作。
我有一个小型测试应用程序,它允许我从行编辑运行 SQL 查询,并将执行该查询,然后在相关模型的视图中更新结果。
我创建了一个使用自动增量主键值的表,但是如果我在不提供键的情况下执行插入语句,则会插入两行,每行都有一个自动增量值。
如果我提供键值,则只会创建一行。有什么想法吗?
表足够简单,例如
CREATE TABLE GroupNames ( ID integer PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, Name varchar(50))
,当我运行查询时,
insert into groupnames (name) values ("testName");
我得到两个带有自动增量 id 的新行。但是,如果我运行,
insert into groupnames (id, name) values (100, "testName");
我会按预期得到一行,其正确的 id 为 100。 另外值得注意的是,如果我尝试
insert into table groupnames (id, name) values (100, "testName");
insert into table groupnames (name) values ("testName");
查询不会运行。 运行查询的 qt 代码再简单不过了:
QSqlQuery *DbCore::run_query(const QString &query_string)
{
QSqlDatabase db = QSqlDatabase::database(defConnectionName);
if(!db.isOpen())
return NULL;
QSqlQuery *q = new QSqlQuery(query_string, db);
q->exec();
return q;
}
我添加了一些日志记录代码来检查查询是否执行一次:
QSqlDatabase db = QSqlDatabase::database(defConnectionName);
if(!db.isOpen())
return NULL;
qDebug() << "Running query:" << query_string;
QSqlQuery *q = new QSqlQuery(query_string, db);
if(!q->exec())
qDebug() << "Error running query:" << q->lastError();
return q;
日志确认我只执行一次:
Running query: "insert into groupnames (name) values ("hello")"
如果我然后使用 sqlite3 shell 检查数据库(以删除对 qt 视图等有任何疑问):
sqlite> select * from groupnames;
1|hello
2|hello
Hi
I have a sqlite db which I am manipulating using qts built in sqlite database driver.
I have a small test app that allows me to run an sql query from a line edit and it will be executed and the results are then updated in a view of the relevant model.
I have created a table which uses autoincremented primary key values, but if I execute an insert statement without providing the key, I get two rows inserted, each with an autoincremented value.
If I provide the key value, only one row is created. Any ideas why this is?
Table is simple enough, e.g
CREATE TABLE GroupNames ( ID integer PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, Name varchar(50))
and when I run the query
insert into groupnames (name) values ("testName");
I get two new rows with autoincremented ids. However, if I run
insert into groupnames (id, name) values (100, "testName");
I get one row as expected, with the correct id 100.
Also of note is that if I try
insert into table groupnames (id, name) values (100, "testName");
insert into table groupnames (name) values ("testName");
the query does not run.
The qt code to run the query could not be simpler:
QSqlQuery *DbCore::run_query(const QString &query_string)
{
QSqlDatabase db = QSqlDatabase::database(defConnectionName);
if(!db.isOpen())
return NULL;
QSqlQuery *q = new QSqlQuery(query_string, db);
q->exec();
return q;
}
I have added some logging code to check that the query is executed once:
QSqlDatabase db = QSqlDatabase::database(defConnectionName);
if(!db.isOpen())
return NULL;
qDebug() << "Running query:" << query_string;
QSqlQuery *q = new QSqlQuery(query_string, db);
if(!q->exec())
qDebug() << "Error running query:" << q->lastError();
return q;
The log confirms that I'm only executing once:
Running query: "insert into groupnames (name) values ("hello")"
If i then check the database using sqlite3 shell (to remove any doubt about qt views etc):
sqlite> select * from groupnames;
1|hello
2|hello
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题已在上面的评论中得到回答:
正如我在文档中看到的,当您按照您的方式创建 QSqlQuery 时,查询(如果不为空)将被执行。要创建 QSqlQuery 并执行查询,请使用: QSqlQuery *q = new QSqlQuery(db); q->exec(query_string) 要查看最后执行的查询,请使用 QSqlQuery::lastQuery() 对于成功执行的最后一个查询 QSqlQuery::execulatedQuery() 希望这有帮助。 – 赫克托 3 月 16 日
question was answered above in a comment:
As i see in the documentation, when you create a QSqlQuery the way you do, the query, if not empty, is executed. To create the QSqlQuery and execute the query, use this: QSqlQuery *q = new QSqlQuery(db); q->exec(query_string) To see the last executed query, use QSqlQuery::lastQuery() And for the last query that was successfully executed QSqlQuery::executedQuery() Hope this helps. – Hector Mar 16 at