Reputation: 21452
I try Insert an array of objects into my database , all of them inserted successfully except last one , It give me this error
here is my code I use to insert into database
- (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;
}
}
The error Printed to me is
near "3": syntax error
my Passed query is look like
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")
The one have problem is
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”)
Any one know can help me
Upvotes: 0
Views: 2455
Reputation: 107121
The main issue is you have "
in your query parameters. You need to escape them with \"
.
But I would like to suggest using sqlite3_bind_xxx
methods for binding parameters to your query rather than specifying them in the query statement.
You can read more about it here : SQLite3 Reference
If you open a database connection you should close it. In your code you are returning from many places, so database connection won't be closed properly, it'll cause database locking and other issues.
- (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);
}
}
Upvotes: 1
Reputation: 8954
If you will look on "2015-01-29 08:43:21”, you will notice that you have an incorrect quote ”
which will cause the problem.
So right query will be
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")
Upvotes: 1