库例程调用不按顺序进行
我正在尝试在 iPhone 上的 sqlite 中写入数据库,我想在数据库中输入 3 个值。下面是我的代码,但我收到以下 sqlite 错误消息:
-(void) writeintoDatabase:(id)value1:(id)value2:(id)value3 {
sqlite3 *database;
const char *sql = "insert into UserData(firstName,lastName,userName) Values(?, ?, ?)";
sqlite3_stmt *insert_statement;
sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL);
sqlite3_bind_text(insert_statement, 1, [value1 UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 2, [value1 UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 3, [value1 UTF8String], -1, SQLITE_TRANSIENT);
NSInteger keyID;
printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) );
keyID = sqlite3_last_insert_rowid(database);
sqlite3_reset(insert_statement);
}
I am trying to write into a DB in sqlite on the iphone , i want to enter 3 values into the DB . Given Below is my code but i am getting the following sqlite error message:
-(void) writeintoDatabase:(id)value1:(id)value2:(id)value3 {
sqlite3 *database;
const char *sql = "insert into UserData(firstName,lastName,userName) Values(?, ?, ?)";
sqlite3_stmt *insert_statement;
sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL);
sqlite3_bind_text(insert_statement, 1, [value1 UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 2, [value1 UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insert_statement, 3, [value1 UTF8String], -1, SQLITE_TRANSIENT);
NSInteger keyID;
printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) );
keyID = sqlite3_last_insert_rowid(database);
sqlite3_reset(insert_statement);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
谢谢mmcomb,谢谢马特。根据您的建议,我已经完全解决了问题。
首先我没有检查数据库路径然后打开
连接。
其次在准备好声明并限制之后
在调用之前我必须调用
sqlite3_step
的适当变量sqlite3_last_insert_rowid
。第三我需要调用
sqlite3_finalize
来释放资源。第四次我需要
sqlite3_close
连接。“库例程调用顺序错误”错误是由于未正确打开和关闭连接造成的。
这是正确的代码:
Thanks mmcomb, Thanks Mat. From both your advices i have solved the problem completely.
Firstly I wasn't checking the DB path and then opening the
connection.
Secondly after having prepared the statement and bounding the
appropriate variables i had to call
sqlite3_step
before callingsqlite3_last_insert_rowid
.Thirdly i needed to call
sqlite3_finalize
to free the resources.Fourthly i needed to
sqlite3_close
the connection.'library routine called out of sequence' error was due to not opening and closing the connection properly.
This is the right code:
您的代码缺少对
sqlite3_step
的调用。您需要在准备好语句并绑定适当的变量之后、调用 sqlite3_last_insert_rowid 之前调用它。有关详细信息,请查看
sqlite3_step
的文档,以及始终检查此类函数返回的错误代码。如果没有发生错误,请不要调用sqlite3_errormsg,在这种情况下其结果是未定义的。
最后,如果您没有在某处保留该语句的句柄,则需要对该语句调用 sqlite3_finalize 。否则你会遇到资源泄漏并最终崩溃。 sqlite3_reset 不会释放资源,它只是“清理”语句,以便您可以使用不同的绑定值重新运行它。
Your code is missing a call to the
sqlite3_step
. You need to call that after having prepared the statement and bound the appropriate variables, and before you callsqlite3_last_insert_rowid
.Check the documentation for
sqlite3_step
for details, and always check the error codes returned by this type of function.Don't call
sqlite3_errormsg
if no error has occurred, its result is undefined in that case.Finally, you need to call
sqlite3_finalize
on the statement if you're not keeping a handle to it somewhere. Otherwise you'll get resource leaks and eventually crash.sqlite3_reset
does not free the resources, it just "cleans" the statement so you can re-run it with different bound values.在准备 sqlite 语句/查询之前,您需要打开与数据库的连接...
请参阅 sqlite 文档以获取 sqlite3_open 方法了解更多信息。
哦,别忘了关闭该连接......
Before preparing your sqlite stament/query you need to open the connection to your database...
See the sqlite documentation for the sqlite3_open method for more info.
Oh, and don't forget to close that connection...
就我而言,它是一个包含一些特殊字符的字符串,例如(')单个冒号,我将其替换为(“”)空格,然后它开始工作。 [[[items objectAtIndex:locBtn.tag]valueForKey:@"description"] stringByReplacingOccurrencesOfString:@"'" withString:@""]
In my case it was a the string contained some special characters like it was ( ' ) single colon i replaced it with (" ") a space and it start working. [[[items objectAtIndex:locBtn.tag]valueForKey:@"description"] stringByReplacingOccurrencesOfString:@"'" withString:@""]