Sqlite update command is not working - ios

When i try to update a table its giving me a
error message : near "(": syntax error
Please anybody suggest me to rectify this issue.
I am updating a table when i click the save button (Bar button item) on the tool bar which is placed below.
The problem is it's always going to else statement of sqlite3_step.
I am not getting whats the exact problem for this issue and the above mentioned error.
Any suggestions would be appreciated.
Following is my update method :
if ([databaseManager didExistDatabaseInDocumentDirectory] == YES)
{
const char *dbpath = [[databaseManager getDatabasePathFromDocument] UTF8String];
sqlite3_stmt *stmt;
if (sqlite3_open(dbpath, &scanningDB) == SQLITE_OK)
{
const char *update_stmt1 = "UPDATE FILES SET (documentType,orderNumber) VALUES (?,?) WHERE ID = ?";
sqlite3_prepare_v2(scanningDB, update_stmt1, -1, &stmt, NULL);
sqlite3_bind_text(stmt, 1, [str UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, [str1 UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 3,[num intValue]);
if(sqlite3_step(stmt)==SQLITE_DONE)
{
sqlite3_reset(stmt);
NSLog(#"Record Updated Successfully");
}
else
{
NSLog(#"Could not Update Record");
NSLog(#" error message %s",sqlite3_errmsg(scanningDB));
sqlite3_finalize(stmt);
sqlite3_close(scanningDB);
return NO;
}
sqlite3_finalize(stmt);
sqlite3_close(scanningDB);
}
else
return NO;
}
else
return NO;

Please correct your query with the following.
const char *update_stmt1 = "UPDATE FILES SET documentType = ? ,orderNumber = ? WHERE ID = ?
Hope this will help you.

Your update query is wrong.
UPDATE FILES SET (documentType,orderNumber) VALUES (?,?) WHERE ID = ?
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
Your query:
UPDATE FILES SET documentType =?, orderNumber =? where ID =?, documentTypeString, Yourordernumber, yourID
Sample:
- (void) update
{
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
if(updateStatement == nil)
{
const char *sql = "UPDATE FILES SET documentType =?, orderNumber =? where ID =?";
if(sqlite3_prepare_v2(database, sql, -1, & updateStatement, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating update statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(documentType
, 1, [comment UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(orderNumber
, 2, [categoria UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(ID, 3, id);
if(SQLITE_DONE != sqlite3_step(updateStatement))
NSAssert1(0, #"Error while updating data. '%s'", sqlite3_errmsg(database));
else
//SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
//noteID = sqlite3_last_insert_rowid(database);
//Reset the update statement.
sqlite3_reset(updateStatement);
sqlite3_close(database);
deleteStatement = nil;
}
else
sqlite3_close(database);
}
Basic SQLite tutorial:
http://www.tutorialspoint.com/sqlite/index.htm

Related

iOS- SQLITE_MISUSE error (code 21)

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
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);
}
}
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")

error: near "?": syntax error using sqlite and obj-c

After a suggestion on here I have tried to bind the values being passed into my queries, but I keep getting the syntax error: error: near "?": can someone explain why please?
NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:#"banklist" ofType:#"sqlite3"];
if(sqlite3_open([sqLiteDb UTF8String], &_database) == SQLITE_OK)
{
{
// prep statement
sqlite3_stmt *statement;
NSString *querySQL = #"UPDATE ? SET recipe_name=? WHERE cell_id=?";
NSLog(#"query: %#", querySQL);
const char *query_stmt = [querySQL UTF8String];
// preparing a query compiles the query so it can be re-used.
sqlite3_prepare_v2(_database, query_stmt, -1, &statement, NULL);
sqlite3_bind_text(statement, 1, [del.dayName UTF8String], -1, SQLITE_STATIC);
sqlite3_bind_text(statement, 2, [info.name UTF8String], -1, SQLITE_STATIC);
sqlite3_bind_int(statement, 1, del.tableRowNumber);
// process result
if (sqlite3_step(statement) != SQLITE_DONE)
{
NSLog(#"error: %s", sqlite3_errmsg(_database));
}
sqlite3_finalize(statement);
}
{
// prep statement
sqlite3_stmt *statement;
NSString *querySQL = #"UPDATE ? SET recipe_id = (SELECT key FROM recipes WHERE name = ?.recipe_name)";
NSLog(#"query: %#", querySQL);
const char *query_stmt = [querySQL UTF8String];
// preparing a query compiles the query so it can be re-used.
sqlite3_prepare_v2(_database, query_stmt, -1, &statement, NULL);
sqlite3_bind_text(statement, 1, [del.dayName UTF8String], -1, SQLITE_STATIC);
sqlite3_bind_text(statement, 2, [del.dayName UTF8String], -1, SQLITE_STATIC);
// process result
if (sqlite3_step(statement) != SQLITE_DONE)
{
NSLog(#"error: %s", sqlite3_errmsg(_database));
}
sqlite3_finalize(statement);
}
}
sqlite3_close(_database);
You use sqlite3_bind_xxx for values in your SQL, but not for table names. You have to use stringWithFormat for the table names and sqlite3_bind_xxx for the values.
You need to bind a value for each ? in the query. The 2nd parameter to the sqlite3_bind_xxx function is the index number. These are 1-based indexes.
In you 1st query you bind a value to the 1st ? two times. You probably need to change the sqlite3_bind_int call to pass 3 as the index instead of 1.
sqlite3_bind_int(statement, 3, del.tableRowNumber);
One other possible issue is your use of sqlite3_bind_text for the table name. This will put the table name in quotes. As suggested by Rob, you should use a string format to apply the table name but use sqlite3_bind_xxx for actual values you need in your query.

ios sqlite3_bind_text step returns null

In the following code everything seems right but the notated lines always return an error code of null no matter what data I feed them. I've researched and changed all the parameters I can think of. The first sqlite3_bind_text line is successful and the next three fail every time. I can't figure it out. Help?
-(void)fillSqliteDb
{
sqlite3 *database;
if (sqlite3_open([[self sqliteFilePath] UTF8String], &database) != SQLITE_OK) {
sqlite3_close(database);
NSAssert(0, #"Failed to open database");
}
NSString *createSQL = #"CREATE TABLE IF NOT EXISTS FUNCTIONS (nouns TEXT, verbs TEXT, adverbs TEXT, adjectives TEXT);";
char *errorMsg;
if (sqlite3_exec (database, [createSQL UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) {
sqlite3_close(database);
NSAssert(0, #"Error creating table: %s", errorMsg);
}
sqlite3_stmt *stmt;
for (int i=0; i<260; i++) {
NSString * pln = self.pluralNouns[i]; // pre-filled array of 260 words
NSString * vrb = self.verb[i]; // pre-filled array of 260 words
NSString * adv = self.adverb[i]; // pre-filled array of 260 words
NSString * adj = self.adjective[i]; // pre-filled array of 260 words
char *update = "INSERT INTO FUNCTIONS (nouns, verbs, adverbs, adjectives) VALUES (?, ?, ?, ?);";
if (sqlite3_prepare_v2(database, update, -1, &stmt, nil) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, [pln UTF8String], -1, NULL);
if (sqlite3_step(stmt) != SQLITE_DONE) // Works, word ends up in database
NSLog(#"Error updating table: %s", errorMsg);
sqlite3_bind_text(stmt, 2, [vrb UTF8String], -1, NULL);
if (sqlite3_step(stmt) != SQLITE_DONE) // ALWAYS RETURNS Error: NULL
NSLog(#"Error updating table: %s", errorMsg);
sqlite3_bind_text(stmt, 3, [adv UTF8String], -1, NULL);
if (sqlite3_step(stmt) != SQLITE_DONE) // ALWAYS RETURNS Error: NULL
NSLog(#"Error updating table: %s", errorMsg);
sqlite3_bind_text(stmt, 4,[adj UTF8String], -1, NULL);
if (sqlite3_step(stmt) != SQLITE_DONE) // ALWAYS RETURNS Error: NULL
NSLog(#"Error updating table: %s", errorMsg);
}
}
}
There are several things wrong with this code.
Only call sqlite3_prepare_v2 once. Do it before the for loop.
You need to call sqlite3_bind_xxx once for each variable, all before calling sqlite3_step.
Only call sqlite2_step once per loop. Do this at the end of the loop.
After calling sqlite3_step at the end of the loop, you need to call sqlite3_reset.
After the loop you need to call sqlite3_finalize on the prepared statement.
Since you opened the database connection at the start of the method, you need to close it at the end of the method.
Your use of errorMsg for all of the logs after checking the result of each sqlite3_step call is wrong. errorMsg is only set from the call to sqlite3_exec. To get the error message of the other calls you need to use sqlite3_errmsg.
Updated code:
- (void)fillSqliteDb {
sqlite3 *database;
if (sqlite3_open([[self sqliteFilePath] UTF8String], &database) != SQLITE_OK) {
sqlite3_close(database);
NSAssert(0, #"Failed to open database");
}
NSString *createSQL = #"CREATE TABLE IF NOT EXISTS FUNCTIONS (nouns TEXT, verbs TEXT, adverbs TEXT, adjectives TEXT);";
char *errorMsg;
if (sqlite3_exec (database, [createSQL UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) {
sqlite3_close(database);
NSAssert(0, #"Error creating table: %s", errorMsg);
}
sqlite3_stmt *stmt;
char *update = "INSERT INTO FUNCTIONS (nouns, verbs, adverbs, adjectives) VALUES (?, ?, ?, ?);";
if (sqlite3_prepare_v2(database, update, -1, &stmt, nil) == SQLITE_OK) {
for (int i=0; i<260; i++) {
NSString * pln = self.pluralNouns[i]; // pre-filled array of 260 words
NSString * vrb = self.verb[i]; // pre-filled array of 260 words
NSString * adv = self.adverb[i]; // pre-filled array of 260 words
NSString * adj = self.adjective[i]; // pre-filled array of 260 words
sqlite3_bind_text(stmt, 1, [pln UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 2, [vrb UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 3, [adv UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 4, [adj UTF8String], -1, NULL);
if (sqlite3_step(stmt) != SQLITE_DONE) // ALWAYS RETURNS Error: NULL
NSLog(#"Error updating table: %s", sqlite3_errmsg(database));
sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);
}
sqlite3_close(database);
}

SQLStatement is getting rejected

I'm trying to insert a user note into a sqliteDB through this method but for some reason, its not passing this if statement...
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK){}
I'm not sure but I think my SQLStatement is getting rejected or some reason.
(void) insertQuickNoteInDataBase:(NSString *)name :(NSString *)note{
sqlite3 * database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "insert into detail(QuickNote) values (?), where name = ?";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_text(compiledStatement, 8, [note UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 2, [name UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(compiledStatement))
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
You need to log the error using sqlite3_errmsg. But the problem is most likely the comma in the query as well as the where clause. Insert statements should not have a where clause. Perhaps you mean to use an update:
"UPDATE detail SET QuickNote = ? WHERE name = ?"
Also, your calls to sqlite3_bind_xxx need to have the proper indexes. The bind indexes start at 1 and go up. You are using 8 then 2. You want 1 then 2.

Not able to update the column value in the sqlite3

I am new to the iPhone programming and was hoping that someone would be able to help me out with a problem that I've been trying to fix for a few days now. I have created a database and a table. In my program, it contain 5 fields and I am setting two of the fields to zero. And later I am updating these fields to one for a purpose. But the problem is I am not able to set the field value to one. I will paste the code below :
NSString *querySQL = [NSString stringWithFormat: #"UPDATE TODOLIST_03 SET DELETEROW ='1' WHERE TASK=\"%#\"", cellText1];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(TODOLIST_03_DB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
.........
}
}
Can anyone please tell me what is the error in the query?
try following :
const char *sqlQuery="UPDATE TODOLIST_03 SET DELETEROW ='1' WHERE TASK= ?";
if(sqlite3_prepare_v2(database, sqlQuery,-1, &queryStatement, NULL) == SQLITE_OK)
{
sqlite3_bind_text(queryStatement, 1, [cellText UTF8String], -1, NULL);
}
What path did you put database file at? It can not be wrote if you put the database file at the path same as project, you should put it in the sandbox for example NSHomeDocument where it is writeable, and you can modify code like this to track the error:
NSError *err;
if (sqlite3_prepare_v2(TODOLIST_03_DB, query_stmt, -1, &statement, &err) == SQLITE_OK)
{
if(err)
{
// NSLog error or what.
}
}

Resources