1

I have a persistence problem in my application. I'm using sqlite database. when some insert queries executed results temporary added to the database. After restarting application the new values vanish! I think new values stored on RAM do not save on hard-disk.

    -(IBAction)add:(id)sender
{

NSString *myDB;
NSString *query;
myDB=[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Ssozluk.sql"];
database =[[Sqlite alloc] init];
[database open:myDB];
query=[NSString stringWithFormat:@"INSERT INTO words(col_1,col_2) VALUES('asd','asd2');"];

[database executeNonQuery:query];
[database commit];
[database close];

}

 -(IBAction)show:(id)sender
    {
    NSString *myDB;
    NSString *query;
    NSArray *asdasd;
    NSString *asd;
    myDB=[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Ssozluk.sql"];
    database =[[Sqlite alloc] init];
    [database open:myDB];
    query=[NSString stringWithFormat:@"Select col_2,col_1 FROM words"];

     asdasd=[database executeQuery:query];
     for(NSDictionary *row in kelimeler)
     {
     asd=[row valueForKey:@"col_2"];
     olabel1.text=asd;
     }


    [database commit];
    [database close];

    }
estergones
  • 11
  • 1
  • 2

4 Answers4

0

You need programmatically copy your database to Documents dir of application, and work with writable copy. Resources in bundle is readonly.

jamapag
  • 9,290
  • 4
  • 34
  • 28
  • i am using xcode iphone application.I copied my database to resources folder in xcode.But still dont work. – estergones Aug 27 '10 at 08:15
  • Where is Documents folder in xcode.And how do i copy and work with writable copy? – estergones Aug 27 '10 at 08:27
  • It's the documents directory in the application. Look here - http://stackoverflow.com/questions/272544/whats-the-best-way-to-find-the-users-documents-directory-on-an-iphone – Liam Aug 27 '10 at 08:34
  • oke but how i copy that. for exp: [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); myDB=[NSHomeDirectory() resourcePath] stringByAppendingPathComponent:@"Ssozluk.sql"]; database =[[Sqlite alloc] init]; [database open:myDB]; query=[NSString stringWithFormat:@"Select col_2,col_1 FROM words"]; like that . Or can you write me how can i do that becouse in forums they always use sqlite_prepare_v2 method and i dont know how its work. – estergones Aug 27 '10 at 09:27
0

I use the following code to copy my database to the documents folder and than get the writable part:

- (void)createEditableCopyOfDatabaseIfNeeded {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"db.sqlite"];
//NSLog(writableDBPath);
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
    NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}

}

- (void)initializeDatabase {
// The database is stored in the application bundle. 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"db.sqlite"];
// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
    //Add initial sql statements here

} else {
    // Even though the open failed, call close to properly clean up resources.
    sqlite3_close(database);
    NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
    // Additional error handling, as appropriate...
}

}

- (sqlite3*) getDB{
return database;

}

Insert the calls to your applicationDidFinishLaunching method

AlexVogel
  • 10,601
  • 10
  • 61
  • 71
0

As far as I remember, when you use path to db the way you do, it should be added to project.

eviltrue
  • 679
  • 8
  • 9
0

Sometimes, and especially with databases, just cleaning doesn't work!!!

Go to the dir: /Users/qjsi/Library/Application Support/iPhone Simulator/4.3/Applications

There, you'll find all the projects you've run. Find the folder containing the project your working on, and delete it. Then clean your project through Xcode, then run. The folder in that dir will be recreated, and so will the database.

NOTE: The database will be removed as well! If you have it saved in your bundle and copy it to an editable directory, please note that the database will be the same as the one in your bundle (so, without altered records made in the iPhone Simulator).

Joetjah
  • 6,292
  • 8
  • 55
  • 90