【问题标题】:Xcode Sqlite persistence problemXcode Sqlite 持久化问题
【发布时间】:2011-04-04 16:21:32
【问题描述】:

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

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

    }

【问题讨论】:

    标签: iphone database xcode sqlite


    【解决方案1】:

    您需要以编程方式将数据库复制到应用程序的Documents 目录,并使用可写副本。 bundle 中的资源是只读的。

    【讨论】:

    • 我正在使用 xcode iphone 应用程序。我将数据库复制到 xcode 中的资源文件夹。但仍然无法正常工作。
    • xcode 中的 Documents 文件夹在哪里。我如何复制和使用可写副本?
    • 这是应用程序中的文档目录。看这里 - stackoverflow.com/questions/272544/…
    • ok 但我是如何复制的。 for exp: [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); myDB=[NSHomeDirectory() resourcePath] stringByAppendingPathComponent:@"Ssozluk.sql"];数据库 =[[Sqlite alloc] 初始化]; [数据库打开:myDB]; query=[NSString stringWithFormat:@"Select col_2,col_1 FROM words"];像那样 。或者你能写信告诉我我该怎么做,因为在论坛中他们总是使用 sqlite_prepare_v2 方法,我不知道它是如何工作的。
    【解决方案2】:

    我使用以下代码将我的数据库复制到文档文件夹,然后获取可写部分:

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

    }

    插入对applicationDidFinishLaunching 方法的调用

    【讨论】:

      【解决方案3】:

      据我所知,当您以这种方式使用 db 路径时,应该将其添加到项目中。

      【讨论】:

        【解决方案4】:

        有时,尤其是对于数据库,仅仅清理是行不通的!!!

        转到目录:/Users/qjsi/Library/Application Support/iPhone Simulator/4.3/Applications

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

        注意:数据库也将被删除!如果您将它保存在您的捆绑包中并将其复制到一个可编辑的目录,请注意数据库将与您捆绑包中的数据库相同(因此,在 iPhone 模拟器中不会更改记录)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-18
          • 2020-03-10
          • 2012-03-15
          • 1970-01-01
          • 1970-01-01
          • 2010-12-07
          相关资源
          最近更新 更多