2010-08-27 114 views
1

我的应用程序中存在持久性问题。我正在使用sqlite数据库。当一些插入查询执行结果临时添加到数据库时。重新启动应用程序后,新值将消失!我认为存储在RAM上的新值不会保存在硬盘上。Xcode Sqlite持久性问题

-(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]; 

    } 

回答

0

你需要编程数据库复制到Documents目录的应用,并与可写副本。包中的资源是只读的。

+0

我正在使用Xcode iPhone应用程序。我将我的数据库复制到xcode中的资源文件夹。但仍然不工作。 – estergones 2010-08-27 08:15:56

+0

xcode中的Documents文件夹在哪里?我如何复制和使用可写副本? – estergones 2010-08-27 08:27:48

+0

这是应用程序中的文档目录。看看这里 - http://stackoverflow.com/questions/272544/whats-the-best-way-to-find-the-users-documents-directory-on-an-iphone – Liam 2010-08-27 08:34:17

0

我用下面的代码到我的数据库复制到文件夹,比获得可写的部分:

- (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; 

}

将c ALLS您applicationDidFinishLaunching方法

0

至于我还记得,当您使用路径DB你的方式,它应该被添加到项目。

0

有时候,特别是数据库,只是清洁不起作用!

转到目录:/用户/ qjsi /库/应用程序支持/ iPhone模拟器/ 4.3 /应用

在那里,你会发现你所运行的所有项目。找到包含您正在处理的项目的文件夹,并将其删除。然后通过Xcode清理你的项目,然后运行。该目录中的文件夹将被重新创建,数据库也将被重新创建。

注意:数据库也将被删除!如果你把它保存在你的软件包中,并将其复制到可编辑的目录中,请注意数据库将与你的软件包中的数据库相同(因此,iPhone模拟器中没有修改记录)。