0

I am new on iphone programming.. I keep getting the error and I have no idea how to change the permission for SQLite

Error while updating. attempt to write a readonly database

and the following is my coding

NSString *filePath = [[NSBundle mainBundle]pathForResource:@"card" ofType:@"sqlite"];
sqlite3_stmt *statement;

if(sqlite3_open([filePath UTF8String], &db) == SQLITE_OK)
{
  const char *query_stmt = "UPDATE card SET isActivate =?";
  if(sqlite3_prepare_v2(db, query_stmt, -1, &statement, NULL) != SQLITE_OK)
     {
         NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(db));
     }
}


sqlite3_bind_int(statement, 1, 1);
if(SQLITE_DONE != sqlite3_step(statement))
   {
      NSLog(@"Error while updating. %s", sqlite3_errmsg(db));
    }

sqlite3_finalize(statement);
sqlite3_close(db);

Hope someone could help me because this really makes me feel frustrated..

1 Answer 1

1

You need to copy your sql file to your cache directory in order to use it correctly.
Here's some of my code:

NSString *sqlFile = @"database.sql";
NSArray *cachePath= NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDir = [cachePathobjectAtIndex:0];
databasePath = [cacheDirstringByAppendingPathComponent:sqlFile];

NSFileManager *fileManager = [NSFileManager defaultManager];
// Copy the database sql file from the resourcepath to the documentpath
if (![fileManager fileExistsAtPath:databasePath]) {
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:sqlFile];
    NSError *error;
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:&error];
    if (error != nil) {
        NSLog(@"[Database:Error] %@", error);
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Ya.. I have copied the database into my directory.. It can SELECT perfectly, but when comes to UPDATE, it just not working..
No you haven't copied it into your documents directory. Your opening the database directly from your bundle. This should be done from within your cache (or documents) directory. These are 2 different things. NSCachesDirectory != NSBundle
What basvk is saying is after you run the code he gave you, then replace your file path string with NSString * filePath = databasePath;
is this method only for sql? I am using sqlite and it seems like not functioning
.sql or .sqlite are the same, matter of naming your extensions. But I think you're missing the big picture. Try any of the online tutorials (google is your friend): dblog.com.au/iphone-development-tutorials/… or dblog.com.au/iphone-development-tutorials/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.