- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试将一组对象插入到我的数据库中,除了最后一个之外,所有对象都成功插入,它给了我这个错误
这是我用来插入数据库的代码
- (int)insertWithQuery:(NSString *)query {
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK) {
const char *insert_stmt = [query UTF8String];
if(sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL)!= SQLITE_OK){
NSLog(@"the error occurred here is %s ",sqlite3_errmsg(database));
return DATABASE_FAILED;
}
int res = sqlite3_step(statement);
if (res == SQLITE_DONE) {
return DATABASE_SUCCESS;
} else if (res == SQLITE_CONSTRAINT) {
return DATABASE_ALREADY_EXISTS;
} else {
return DATABASE_FAILED;
}
sqlite3_finalize(statement);
sqlite3_close(database);
} else {
return DATABASE_FAILED;
}
}
打印给我的错误是
near "3": syntax error
我传递的查询看起来像
INSERT INTO report (report_id, student_id, report_title, report_body, from_date, to_date, timestamp) VALUES ("217", "1", "", "<h3>Hazem Taha Ghareeb <small>report</small></h3>From: <p>2014-06-17 </p> To: <p>2014-06-24 </p><table><thead><th>Exam</th><th>Date</th><th>Result</th></thead><tbody><tr><td>Java </td><td>2014-06-18 </td><td>138 </td></tr></tbody></table><h3>Absence</h3><table><thead><th>Date</th></thead><tbody><tr><td>2014-06-17 </td></tr><tr><td>2014-06-22 </td></tr><tr><td>2014-06-24 </td></tr></tbody></table></body></html>", "2014-06-17", "2014-06-24", "2014-06-24 12:23:58")
有问题的是
INSERT INTO report (report_id, student_id, report_title, report_body, from_date, to_date, timestamp) VALUES ("631", "1", "C class report4", "<h3>Hazem Taha <small>report</small></h3>From: <p>30-01-2015 </p> To: <p>30-01-2015 </p><table><thead><th>Exam</th><th>Date</th><th>Result</th></thead><tbody><tr><td colspan="3"> No exams records. </td></tr></tbody></table><h3>Absence</h3><table><thead><th>Date</th></thead><tbody><tr><td> No absence records. </td></tr></tbody></table></body></html>", "2015-01-30", "2015-01-30", "2015-01-29 08:43:21”)
有知道的可以帮帮我
最佳答案
主要问题是您的查询参数中有 "
。您需要使用 \"
对它们进行转义。
但我想建议使用 sqlite3_bind_xxx
方法将参数绑定(bind)到您的查询,而不是在查询语句中指定它们。您可以在这里阅读更多相关信息:SQLite3 Reference
如果你打开了一个数据库连接,你应该关闭它。在您的代码中,您从许多地方返回,因此数据库连接将无法正确关闭,这将导致数据库锁定和其他问题。
- (int)insertWithQuery:(NSString *)query
{
int status = DATABASE_FAILED;
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
const char *insert_stmt = [query UTF8String];
if(sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL)!= SQLITE_OK)
{
NSLog(@"the error occurred here is %s ",sqlite3_errmsg(database));
status = DATABASE_FAILED;
}
else
{
int res = sqlite3_step(statement);
if (res == SQLITE_DONE)
{
status = DATABASE_SUCCESS;
}
else if (res == SQLITE_CONSTRAINT)
{
status = DATABASE_ALREADY_EXISTS;
}
else
{
status = DATABASE_FAILED;
}
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
}
关于iOS- SQLITE_MISUSE 错误(代码 21),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28279701/
我在以下代码中收到 SQLITE_MISUSE 错误,我想知道这是否可能是由于将表名作为绑定(bind)参数引起的? SQLITE_MISUE 有哪些不同的原因? const char sqlNeur
我是第一次使用SQLite3,想不通出现如下错误。以下是相关行: //open db; I checked, and sqlite3_open() returns 0 here (SQLITE_OK)
我尝试将一组对象插入到我的数据库中,除了最后一个之外,所有对象都成功插入,它给了我这个错误 这是我用来插入数据库的代码 - (int)insertWithQuery:(NSString *)que
我在 Java 应用程序中使用 SQlite。如果我运行我的程序,它会在某个时候出错。 有时这是一个简单的异常,有时则是 JVM 发生 fatal error 。 java.sql.SQLExcept
我正在编写一个直接访问 SQLite 的 iOS 应用程序。我在 Android 上做过很多次这种事情,所以我很难找出我的错误所在 - 但是我的插入返回了 SQLITE_MISUSE 错误(代码 21
当我用这个查询启动代码时,有时我可以看到列出的所有表,有时只看到一个,而且我总是得到这个错误: 查询错误:错误:SQLITE_MISUSE:未知错误 我读到当 SQLITE API 使用不当时会发生
随着 iOS 5 的发布,我们在设置 sqlite 数据库的序列化选项时遇到越来越多的错误(因此其保存用于多线程)。我们在 sqlite3_config 上收到 SQLITE_MISUSE 错误代码。
我是一名优秀的程序员,十分优秀!