iphone: delete row from sqlite database - iphone

I have tried many ways to achieve this biut unable to delete row from sqlite, please help me to correct following piece of code
dbPath = [self applicationDocumentsDirectory];
NSLog(#"%#", dbPath);
dbPath=[dbPath stringByAppendingPathComponent:#"database"];
dbPath=[dbPath stringByAppendingPathComponent:#"OFFENDERSDB.sqlite"];
NSLog(#"database path --> %#", dbPath);
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = ("DELETE * from OFFENDERS_LIST where ID=%d ",2);
sqlite3_stmt *compiledStatement;
if (sqlite3_prepare(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
NSLog(#"offender deleted");
}
sqlite3_finalize(compiledStatement);
}
Thanx in advance

have you tried using:
sqlite3_exec(database, sqlStatement...
with your DELETE statement?
ALSO.... it's DELETE FROM.... not DELETE * FROM....
Snagged the following code from another place out on the internet for a more complete example...
sqlite3 *db;
int rc;
rc = sqlite3_open( "C:\\MyDatabase", &db );
if ( rc )
{
sqlite3_close(db);
}
else //Database connection opened successfuly
{
char *zErrMsg = 0;
rc = sqlite3_exec( db, "DELETE FROM yourTable", NULL, NULL, &zErrMsg );
if( rc != SQLITE_OK )
{
sqlite3_free( zErrMsg );
}
sqlite3_close(db);
}

Related

Works fine for the first time, next time getting DB locked issue

Please find the below function.it works fine for the first time.
I mean , when i add a text it works for the first time , then when i try to add a new text again , it give me like "DB is locked".
I am not sure how it works for the first not again?
Please let me know what is the problem in my code.
//==================================================================
- ( BOOL ) addNewSimpleTemplates:(NSString*)dbPath:(NSString*)title{
//==================================================================
BOOL returnVal = NO;
NSString *maxValuePosition;
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSString *selectSQL = [NSString stringWithFormat:#"select MAX(pr.position) FROM phrase_reference pr inner join storyboard_main_categories smc on smc.id = pr.main_category_id where smc.category_name = %#", #"'Simple Templates'"];
const char *sql = [selectSQL UTF8String];
sqlite3_stmt *selectStmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK){
while(sqlite3_step(selectStmt) == SQLITE_ROW)
{
//
char *localityChars = (char*)sqlite3_column_text(selectStmt, 0);
if (localityChars == NULL)
maxValuePosition = nil;
else
maxValuePosition = [NSString stringWithUTF8String: localityChars];
// increment the postion value
int postion = [maxValuePosition intValue] + 1;
NSLog(#"%d look here %# gtitle", postion , title);
selectStmt = nil;
//saving a new simple phrase is started here
sql = "insert into storyboard_phrases(phrase) Values(?)";
if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK){
sqlite3_bind_text(selectStmt, 1, [title UTF8String], -1, SQLITE_TRANSIENT);
}
if(sqlite3_step(selectStmt) != SQLITE_DONE ) {
NSLog( #"Error: %s just here itself", sqlite3_errmsg(database) );
} else {
NSLog( #"Insert into row id = %d", sqlite3_last_insert_rowid(database));
///
int phrase_id = sqlite3_last_insert_rowid(database);
int sub_category_id = 0;
selectStmt = nil;
NSString *selectSQL = [NSString stringWithFormat:#"select id from storyboard_main_categories where category_name = %#", #"'Simple Templates'"];
sql = [selectSQL UTF8String];
if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(selectStmt) == SQLITE_ROW)
{
char *localityChars = (char*)sqlite3_column_text(selectStmt, 0);
NSString* main_category_id = [NSString stringWithUTF8String: localityChars];
sql = "insert into phrase_reference (phrase_id, sub_category_id,main_category_id, position) Values(?,?,?,?)";
if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK){
sqlite3_bind_int(selectStmt, 1, phrase_id);
sqlite3_bind_int(selectStmt, 2, sub_category_id);
sqlite3_bind_int(selectStmt, 3, [main_category_id intValue]);
sqlite3_bind_int(selectStmt, 4, postion);
}
if(sqlite3_step(selectStmt) != SQLITE_DONE ) {
NSLog( #"Error: %s", sqlite3_errmsg(database) );
}else {
NSLog( #"Insert into row id = %d", sqlite3_last_insert_rowid(database));
returnVal = YES;
}
}
}
sqlite3_finalize(selectStmt);
selectStmt = nil;
/////
}
}
}
sqlite3_finalize(selectStmt);
}
NSLog(#"sdsdfsdsd closed");
sqlite3_close(database);
return returnVal;
}
I am not going deep within your code but make sure that -
Dont forget to finalize the statements before close the database.
And do reset after every execution of the sqlite3 statement
You are not using reset statement any where.. Try using reset statement..

Sqlite finalise and Db locking issue

I am using the below function in my app,and i have started using the sq-lite recently and i would like to get your opinion that i am going with that correctly or not.
Since i am facing db locked issue in my app when searched i found that i need to use sqlite3 finalise statement.
what i am not sure is do i need to place one finalise statement for each sqlite3 prepare statement
Please let me know
- ( BOOL ) addNewCate:(NSString*)dbPath:(NSString*)title:(NSString*)tierOneID:(NSString*)tierTwoID{
BOOL returnVal = NO;
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "insert into storyboard_phrases(phrase) Values(?)";
sqlite3_stmt *addStmt;
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) == SQLITE_OK){
sqlite3_bind_text(addStmt, 1, [title UTF8String], -1, SQLITE_TRANSIENT);
}
if(sqlite3_step(addStmt) != SQLITE_DONE ) {
NSLog( #"Error: %s", sqlite3_errmsg(database) );
} else {
NSLog( #"Insert into row id = %d", sqlite3_last_insert_rowid(database));
int ph_id = sqlite3_last_insert_rowid(database);
int sub_category_id = [tierTwoID intValue];
int main_category_id = [tierOneID intValue];
addStmt = nil;
sql = "insert into phrase_reference(phrase_id, sub_category_id,main_category_id) Values(?,?,?)";
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) == SQLITE_OK){
sqlite3_bind_int(addStmt, 1, ph_id);
sqlite3_bind_int(addStmt, 2, sub_category_id);
sqlite3_bind_int(addStmt, 3, main_category_id);
}
if(sqlite3_step(addStmt) != SQLITE_DONE ) {
NSLog( #"Error: %s", sqlite3_errmsg(database) );
} else {
NSLog( #"Insert into row id = %d", sqlite3_last_insert_rowid(database));
returnVal = YES;
}
}
sqlite3_finalize(addStmt);
}
sqlite3_close(database);
return returnVal;
}
hii you get the locked issues in log right, that means your database is open and you doing some changes in that database so close database and try again to run application and insert in table...
hope this will help you..

SQLite update statement performed without error, but table row not updated?

I cant for the life of me see what it could be? Any Advice? Thanks
(void)setRegId: (NSInteger) _regId
{
NSInteger r_id = _regId;
NSInteger row = 1;
ret = sqlite3_open(dbName, &database);
if (ret != SQLITE_OK)
{
printf("Error opening the db: %s\n", sqlite3_errmsg(database));
sqlite3_close(database);
return;
}
sqlite3_stmt *compiledStatement = nil;
if (compiledStatement == nil) {
const char *sql = "UPDATE RegDB Set id = ? Where rowindex = ?";
if (sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) != SQLITE_OK)
{
NSLog(#"Error: failed to prepare compiledStatement with message '%s'.",sqlite3_errmsg(database));
return;
}
}
sqlite3_bind_int(compiledStatement, 1, r_id);
sqlite3_bind_int(compiledStatement, 2, row);
int success = sqlite3_step(compiledStatement);
if (success == SQLITE_ERROR){
NSLog(#"Error: failed to update compiledStatement with message '%s'.", sqlite3_errmsg(database));
}
sqlite3_finalize(compiledStatement);
if(sqlite3_close(database) != SQLITE_OK)
{
NSLog(#"Error: failed to closed DB with message '%s'.",sqlite3_errmsg(database));
}
}
You need to check that you are both sending the correct update statement, and that you are correctly reading the result.
Try logging your final statement (or at least the variables r_id and row). Use sqlite3 or SQLiteManager with Firefox to check changes in the database.

How to delete data from sqlite

I have created a table with three entries name,adress and phone no.
Now i want to delete data based on the name i type in the name text field. I successfully doing this but the problem is that if I type a name which is not in my database label still show 'contact deleted '... Here is my code
-(void)deleteContact{
const char *dbPath=[databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbPath, &contactDB)==SQLITE_OK)
{
NSString *querySQL=[NSString stringWithFormat:#"delete from
contacts where name=\"%#\"",name.text];
const char *query_stmt=[querySQL UTF8String];
sqlite3_prepare_v2(contactDB, query_stmt,-1,&statement, NULL);
(sqlite3_step(statement)==SQLITE_OK);
status.text=#"Contact Deleted";
name.text=#"";
address.text=#"";
phone.text=#"";
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
[name resignFirstResponder];
}
Delete is OK even if there is nothing to delete.
If you need to know if there is anything to delete you should check this.
NSString *sql = [NSString stringWithFormat:#"select count (*) from contacts where name=\"%#\"", name.text];
sqlite3_stmt *stmt;
int res = sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, NULL);
if (res != SQLITE_OK) {
NSLog(#"sqlite3_prepare_v2() failed");
return;
}
int count = 0;
if (sqlite3_step(stmt) == SQLITE_ROW) {
count = sqlite3_column_int(stmt, 0);
}
sqlite3_finalize(stmt);
if (count == 0) {
// nothing to delete, do whatever you like
} else {
// do real delete
// your code for that seems to be OK
}
delete from YourTable where name = "NameToDelete"
will delete all records with field name equal to NameToDelete from table YourTable.

sqlite3 is not working in my iphone application

I want to use sqlite3 for my iphone application. But it is not working. Scenario is :- On click event of a button a method should run and retrieve the data from the database. But My sequence of program is not going into
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "select * from records";
sqlite3_stmt *compiledStatement;
0NSLog(#"%s", sqlite3_errmsg(database)); // Results - no error
-
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
//here NSLog statement does not work.
There is no problem in opening a database. What should be the reason? Any solution appreciable.
Thanks
Try this: NSLog(#"%s", sqlite3_errmsg(database)); and see if there are some errors in your query.
Here is a modified example of my own code :
NSString *file = #"youdatabase.db"
sqlite3 *db;
if(sqlite3_open([file UTF8String], &db) == SQLITE_OK)
{
NSString *req = #"SELECT * FROM totoTable";
sqlite3_stmt *returnReq;
if(sqlite3_prepare(db, [req cStringUsingEncoding:NSUTF8StringEncoding], -1, &returnReq, NULL) == SQLITE_OK)
{
while (sqlite3_step(returnReq) == SQLITE_ROW)
{
//Printf first returned value
NSLog(#"%s", (char*)sqlite3_column_text(returnReq, 0));
}
}
sqlite3_close(db);
}
Hope it will help you ;-)