views:

83

answers:

2

Hi all,

I am getting an "sqlite3_exce read only data base" error while inserting or updating data into table.

Because initially I have to create number of tables in data base & insert lots of data into it, so I create a one dummy application which create "database.sql" database,create Table & it insert data into the tables.

Now I put "database.sql" file into resources folder of my Main application & gives main bundle path while accessing data from data base.

I am able to access the data from it but while insert or update data from table, I get above mention error.

kindly help me to solve above problem.

Thank You.

+4  A: 

Yes, files in maib bundle are read only.

You can write ONLY in Documents folder.

Copy your database file into documents and work with it

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // path to Documents folder
oxigen
A: 

static sqlite3 * getDbHandle() {

NSFileManager *fileManager = [NSFileManager defaultManager];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];     // path to Documents folder
NSString * databaseFileName = [documentsDirectory stringByAppendingString:@"/feeds.db"];

// check if db file is in documents folder
// If it is already there, thats great.
// Else, we need to copy it there.

BOOL dbExists = [fileManager fileExistsAtPath:databaseFileName];

if ( !dbExists ) {
 NSLog(@"No database in document folder.Need to move it there");

 // we need to copy the file here...
 NSString * databaseFileinAppDir = [ [NSBundle mainBundle] pathForResource:@"feeds" ofType:@"db"];

 NSError *error;

 NSLog(@"Target Db %@",databaseFileName);

 [fileManager copyItemAtPath:databaseFileinAppDir toPath:databaseFileName error:&error];

 if ( error ){
  NSLog(@"ERROR IN FILE MOVE:{%@}",[error description]);
  return NULL;
 }
}

sqlite3 *database = NULL;

if ( sqlite3_open([databaseFileName UTF8String], &database ) != SQLITE_OK ) {
 printf("ERROR: Unable to open database %@",databaseFileName);
 return NULL;
}

return database;

}

iPhoneBoy