std::vector<std::vector<std::string> > InternalDatabaseManager::query(std::string query)
{
sqlite3_stmt *statement;
std::vector<std::vector<std::string> > results;
if(sqlite3_prepare_v2(internalDbManager, query.c_str(), -1, &statement, 0) == sqlITE_OK)
{
int cols = sqlite3_column_count(statement);
int result = 0;
while(true)
{
result = sqlite3_step(statement);
std::vector<std::string> values;
if(result == sqlITE_ROW)
{
for(int col = 0; col < cols; coL++)
{
std::string s;
char *ptr = (char*)sqlite3_column_text(statement, col);
if(ptr) s = ptr;
values.push_back(s);
}
results.push_back(values);
} else
{
break;
}
}
sqlite3_finalize(statement);
}
std::string error = sqlite3_errmsg(internalDbManager);
if(error != "not an error") std::cout << query << " " << error << std::endl;
return results;
}
当我尝试传递查询字符串时:
INSERT INTO cpuUsage (NODE_ID, TIME_ID, CORE_ID, USER, NICE, SYSMODE, IDLE, IOWAIT, IRQ, SOFTIRQ, Steal, GUEST) VALUES (1, 1, -1, 1014711, 117915, 175551, 5908257, 112996, 2613, 4359, 0, 0); INSERT INTO cpuUsage (NODE_ID, TIME_ID, CORE_ID, USER, NICE, SYSMODE, IDLE, IOWAIT, IRQ, SOFTIRQ, Steal, GUEST) VALUES (1, 1, 0, 1014711, 117915, 175551, 5908257, 112996, 2613, 4359, 0, 0); INSERT INTO cpuUsage (NODE_ID, TIME_ID, CORE_ID, USER, NICE, SYSMODE, IDLE, IOWAIT, IRQ, SOFTIRQ, Steal, GUEST) VALUES (1, 1, 1, 1014711, 117915, 175551, 5908257, 112996, 2613, 4359, 0, 0);
它只会插入第一个插入.使用其他一些工具精简版sqliteStudio它可以正常运行.
有什么想法可以帮助我吗?
谢谢,
佩德罗
编辑
const char** pzTail;
const char* q = query.c_str();
int result = -1;
do {
result = sqlite3_prepare_v2(internalDbManager, q, -1, &statement, pzTail);
q = *pzTail;
}
while(result == sqlITE_OK);
这给了我描述:无法将’const char *’转换为’const char **’以将参数’5’转换为’int sqlite3_prepare_v2(sqlite3 *,const char *,int,sqlite3_stmt *,const char *)’
解决方法:
sqlite的prepare_v2只会从字符串中的第一个插入创建一个语句.您可以将其视为“流行前沿”机制.
int sqlite3_prepare_v2(
sqlite3 *db, /* Database handle */
const char *zsql, /* sql statement, UTF-8 encoded */
int nByte, /* Maximum length of zsql in bytes. */
sqlite3_stmt **ppStmt, /* OUT: Statement handle */
const char **pzTail /* OUT: Pointer to unused portion of zsql */
);
从http://www.sqlite.org/c3ref/prepare.html起
If pzTail is not NULL then *pzTail is made to point to the first byte
past the end of the first sql statement in zsql. These routines only
compile the first statement in zsql, so *pzTail is left pointing to
what remains uncompiled.
pzTail参数将指向其余插入,因此您可以遍历所有插入,直到它们都已准备好.
另一种选择是一次只进行一次插入,这使得其余的处理代码通常更简单一些.
通常情况下,我看到人们会在相同的交易下对他们进行评估.但事实并非如此.第二个可能会失败,第一个和第三个仍然会成功.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。