Help, not working UPDATE in SQite. query is executed but the changes do not occur
const char *dbPath=[databasePath UTF8String];
if (sqlite3_open(dbPath, &contactDB)==SQLITE_OK) {
NSLog(#"database Opened");
const char* updateQuery="update poi set test=\"111\"";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(contactDB, updateQuery, -1, &stmt, NULL)==SQLITE_OK) {
NSLog(#"Query Executed");
}
}
sqlite3_close(contactDB);
you made minor mistake....
Write ur stuff in following template, you missed the main execution statement i.e.
sqlite3_step(statement);
What is happening here, you are just preparing the statement and not executing it..
sqlite3_stmt *statement;
if(sqlite3_open(dbpath, &database_object) == SQLITE_OK)
{
NSString *sql = [NSString stringWithFormat:#"update table_name set column_name = \"%#\"", Your_value];
const char *sql_stmt = [sql UTF8String];
if(sqlite3_prepare_v2(database_object , sql_stmt, -1, &statement, NULL) != SQLITE_OK)
NSLog(#"Error while creating update statement. '%s'", sqlite3_errmsg(database_object));
if(SQLITE_DONE != sqlite3_step(statement))
NSLog(#"Error while updating. '%s'", sqlite3_errmsg(database_object));
sqlite3_finalize(statement);
}
Make these changes in your code and try, i think it will work...
You haven't executing the Query :
sqlite3_prepare_v2- Just prepared not to execute(affect) , You need to execute it
Try this Out:
const char *dbPath=[databasePath UTF8String];
if (sqlite3_open(dbPath, &contactDB)==SQLITE_OK) {
NSLog(#"database Opened");
const char* updateQuery="update poi set test=\"111\"";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(contactDB, updateQuery, -1, &stmt, NULL)==SQLITE_OK) {
NSLog(#"Query Prepared to execute");
}
if(sqlite3_step(stmt) != SQLITE_DONE){
NSAssert1(0, #"Error while updating. '%s'", sqlite3_errmsg(database));
}else
NSLog(#"Executed");
sqlite3_close(contactDB);
}
Related
I have some values in labels and i'm trying to store it in my database but when i hit save button it isn't save and log a message failed to save. The database is properly made but data is not storing. My code for saving data is this,
- (IBAction)btnSave:(id)sender {
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: #"INSERT INTO data (ID, ALERT, XREF,TEXT,GRAPHIC,PROMPT,VOICE) VALUES (\"%#\", \"%#\", \"%#\",\"%#\",\"%#\",\"%#\",\"%#\")", _lblID.text, _lblAlert.text, _lblxref.text,_lbltext.text, _lblgraphic.text, _lblprompt.text,_lblvoice.text];
NSLog(#"DDD %#",insertSQL);
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
_lblstatus.text = #"Contact added";
} else {
_lblstatus.text = #"Failed to add contact";
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
}
if (sqlite3_step(statement) == SQLITE_DONE){
_lblstatus.text = #"Contact added";
} else {
NSLog(#"prepare failed: %s", sqlite3_errmsg(contactDB));
_lblstatus.text = #"Failed to add contact";
}
1. Replace this snippet in your code to print error message. It will help you to find exact issue.
2. May be the SQL query which your forming is syntactically wrong.
My iOS app is terminating on the line
sqlite3_exec(database, "COMMIT", 0, 0, 0);
Following is the complete function.
-(void)clearCarts
{
sqlite3_exec(database, "BEGIN", 0, 0, 0);
sqlite3_stmt *stmt = nil;
const char *sql = "DELETE FROM carts";
if(sqlite3_prepare_v2(database, sql, -1, &stmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error:'%s'", sqlite3_errmsg(database));
if (SQLITE_DONE != sqlite3_step(stmt))
NSAssert1(0, #"Error:'%s'", sqlite3_errmsg(database));
sqlite3_reset(stmt);
sqlite3_finalize(stmt);
sqlite3_exec(database, "COMMIT", 0, 0, 0);// terminates here
}
What could be the reason and where should I use sqlite3_close(database) as sqlite3_close(database) is not being used in any function.
Please suggest, thank you in advanced.
This must be because you are not opening your database to execute the query and you should use sqlite3_close(database) after executing the statement.
Try with below code
sqlite3 *database;
sqlite3_stmt *selectstmt;
if (sqlite3_open([yourDatabasePath UTF8String], &database) == SQLITE_OK)
{
NSString *sql = [NSString stringWithFormat:#"DELETE FROM carts"];
const char *insert_stmt = [sql UTF8String];
sqlite3_prepare_v2(database,insert_stmt, -1, &selectstmt, NULL);
if(sqlite3_step(selectstmt)==SQLITE_DONE)
{
NSLog(#"Delete successfully");
}
else
{
NSLog(#"Error while Clean Database. '%s'", sqlite3_errmsg(database));
}
sqlite3_finalize(selectstmt);
}
sqlite3_close(database);
Code:
#try {
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
const char * sql="Select Id, Name,Designation, Skill, Credits, Selected from candidate_info where Designation like '%%%#%%'";
if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK)
{
sqlite3_bind_text(statement, 1, [searchWord UTF8String], -1, SQLITE_TRANSIENT);
while (sqlite3_step(statement) ==SQLITE_ROW)
{
}
}
else
NSAssert1(0, #"Error in candidateinfo. '%s'", sqlite3_errmsg(database));
sqlite3_reset(statement);
}
}#catch (NSException * e) {
NSLog(#"Exception is %#, %#", [e name], [e reason]);
}#finally {
sqlite3_finalize(statement);
sqlite3_close(database);
}
I try to fetch data from sqlite based on search keyword.I tried with like statement.The query is not executing and is goes out of while loop.what is wrong with my code.thanks in advance
Try creating your Query using NSString :
NSString *sql = [NSString stringWithFormat:#"Select Id, Name,Designation, Skill, Credits, Selected from candidate_info where Designation like '%#'", searchWord] ;
if (sqlite3_prepare_v2(database, [sql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK)
{
}
Problem is that you are using 'sqlite3_bind_text' in wrong way.
Your query must be :
const char * sql="Select Id, Name,Designation, Skill, Credits, Selected from candidate_info where Designation like '%%?%%'";
You are missing '?'
I found answer to my question
NSString *query = [NSString stringWithFormat:#"Select Id, Name,Designation, Skill, Credits, Selected from candidate_info where Designation LIKE '%%%#%%'", searchWord];
const char *sql = [query UTF8String];
I know this is very frequently asked question and i tried everything that i found here. But problem is still there.
Now the problem is i am trying to delete all records from 3 tables with following code but they are not being deleted
const char *dbpath = [dbPathString UTF8String];
sqlite3 *sqliteDB;
if (sqlite3_open(dbpath, &sqliteDB) == SQLITE_OK)
{
//Delete all records from attempts
sqlite3_stmt *statement;
NSString *deleteSQL = [NSString stringWithFormat: #"DELETE FROM table1"];
const char *delete_stmt = [deleteSQL UTF8String];
sqlite3_prepare_v2(sqliteDB, delete_stmt, -1, &statement, NULL);
sqlite3_finalize(statement);
sqlite3_close(sqliteDB);
}
if (sqlite3_open(dbpath, &sqliteDB) == SQLITE_OK)
{
//Delete all records from attempts
sqlite3_stmt *statement;
NSString *deleteSQL = [NSString stringWithFormat: #"DELETE FROM table2"];
const char *delete_stmt = [deleteSQL UTF8String];
sqlite3_prepare_v2(sqliteDB, delete_stmt, -1, &statement, NULL);
sqlite3_finalize(statement);
sqlite3_close(sqliteDB);
}
if (sqlite3_open(dbpath, &sqliteDB) == SQLITE_OK)
{
//Delete all records from attempts
sqlite3_stmt *statement;
NSString *deleteSQL = [NSString stringWithFormat: #"DELETE FROM table"];
const char *delete_stmt = [deleteSQL UTF8String];
sqlite3_prepare_v2(sqliteDB, delete_stmt, -1, &statement, NULL);
sqlite3_finalize(statement);
sqlite3_close(sqliteDB);
}
I have tried with if conditions like
if (sqlite3_prepare_v2(sqliteDB, delete_stmt, -1, &statement, NULL) == SQLITE_OK) {
NSLog("All records deleted");
}
And this if condition get executed and i do have statement in console log.
I am using Xcode 5.0.
Please help me. Thanx in advance
sqlite3_prepare_v2 just prepares the statement.
To actually execute it, you must also call sqlite3_step.
Dear friend this is little bit complex coding try to use external class for easy coding
this link will help you
How to insert update delete in database operation and also save database in document directory
for delete record you can use
NSString *strDelete = [Database executeScalarQuery:#"DELETE FROM table1"];
Happy Coding!!!
In the code below, the commented-out code works.
But using the saveData method of the DBMgr Class results in "Failded to add contact".
I want to see "Contact added" instead.
-(void) saveData{
NSString *insSQL = [NSString stringWithFormat:#"INSERT INTO CONTACTS (name,address,phone) VALUES (\"%#\",\"%#\",\"%#\")",name.text,address.text,phone.text];
DBMgr *dbmgr = [DBMgr alloc];
if([dbmgr saveData:insSQL]== 0){
status.text = #"Contact added";
}else if([dbmgr saveData:insSQL]== 1){
status.text=#"Failded to add contact";
}
/*sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:#"INSERT INTO CONTACTS (name,address,phone) VALUES (\"%#\",\"%#\",\"%#\")",name.text,address.text,phone.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE)
{
status.text = #"Contact added";
name.text = #"";
address.text = #"";
phone.text = #"";
}else{
status.text=#"Failded to add contact";
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}*/
}
-(NSInteger) saveData:(NSString *) querySQL{
NSInteger result;
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *insertSQL = querySQL;
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if(sqlite3_step(statement) == SQLITE_DONE)
{
result = 0;
}else{
result = 1;
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
return result;
}
You should check the result codes of all of your SQLite calls, and if they fail, log the error:
- (NSInteger) saveData:(NSString *) querySQL{
NSInteger result = 1;
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *insertSQL = querySQL;
const char *insert_stmt = [insertSQL UTF8String];
if (sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL) != SQLITE_OK)
NSLog(#"%s: prepare failed: %s", __FUNCTION__, sqlite3_errmsg(contactDB));
else
{
if(sqlite3_step(statement) == SQLITE_DONE)
{
result = 0;
}else{
NSLog(#"%s: step failed: %s", __FUNCTION__, sqlite3_errmsg(contactDB));
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
} else {
NSLog(#"%s: open failed", __FUNCTION__);
}
return result;
}
Unless you look at sqlite3_errmsg, you're just guessing. And check sqlite3_prepare_v2 return code, too, like I did above, (as that will more likely be the initial indication of a problem).
Two other, unrelated, observations:
The DBMgr should be initialized, e.g.:
DBMgr *dbmgr = [[DBMgr alloc] init];
You are building your INSERT statement using stringWithFormat. That's very dangerous, you should use ? placeholders in your SQL:
const char *insSQL = "INSERT INTO CONTACTS (name,address,phone) VALUES (?, ?, ?)";
sqlite3_prepare_v2(contactDB, insSQL, -1, &statement, NULL);
Then, after preparing that statement, you should then use the sqlite3_bind_text function to assign your values to those three placeholders, e.g.
sqlite3_bind_text(statement, 1, [name.text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [address.text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3, [phone.text UTF8String], -1, SQLITE_TRANSIENT);
By the way, if you wanted to specify NULL, you'd call sqlite3_bind_null instead of sqlite3_bind_text.
Obviously, check the return code from each of those to make sure you returned SQLITE_OK for each, again, logging sqlite3_errmsg if it failed.
I appreciate that this change is going to require some refactoring of your code, but it's important to use sqlite3_bind_text to avoid SQL injection attacks and errors that will result if the user typed in a value that included quotation marks.
By the way, if you're looking at the above and realizing that it takes a lot of code to do this properly, you might want to consider using FMDB which can significantly simplify your life.