sqlite ios:尝试编写只读数据库 [英] sqlite ios : attempt to write a readonly database

查看:237
本文介绍了sqlite ios:尝试编写只读数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为项目使用sqlite数据库。我可以做SELECT之类的查询但不可能做INSERT!
在模拟器上,INSERT正常工作。一旦我在iPod上编译,就会出现以下错误消息:尝试编写只读数据库

I use a sqlite database for a project. I can do queries like SELECT but impossible to do INSERTs! On the simulator the INSERT works properly. As soon as I compile on my iPod this error message appears: "attempt to write a readonly database".

认为这是一项权利我做的文件: chmod 777 mydatabase.sqlite
但是这没有改变!

Thinking it is a right of the file I did a: chmod 777 mydatabase.sqlite But that does not change!

I还试过,因为我已经在其他网站上阅读并复制文件以使用他的副本使其完好无损,但无济于事。

I also tried as I have read on other site and copy the file to use his copy to have it all right, but to no avail.

您有解决方案吗?
亲切。

Have you a solution? Cordially.

PS:这是我的代码:

PS: Here is my code:

for(NSDictionary *q in quotes) {
    sqlite3_stmt    *statement;
    sqlite3 *contactDB;
    const char *dbpath = [dbPath UTF8String];

    if (sqlite3_open_v2(dbpath, &contactDB, SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK)
    {
        NSInteger identifiant = [[q objectForKey:@"id"] integerValue];
        NSString *texte = [q objectForKey:@"texte_english"];
        NSString *auteur = [q objectForKey:@"auteur"];
        NSString *date = [q objectForKey:@"date"];
        NSInteger auteurId = [[q objectForKey:@"auteur_id"] integerValue];
        NSInteger nbComments = [[q objectForKey:@"nb_comments"] integerValue];

        NSString *insertSQL = 
                [NSString stringWithFormat:@"INSERT INTO quotes (id, texte_english, auteur, date, auteur_id, nb_comments) VALUES (%d, \"%@\", \"%@\", \"%@\", \"%d\", \"%d\")", 
                                          identifiant, 
                                          texte, 
                                          auteur, 
                                          date, 
                                          auteurId, 
                                          nbComments];

        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);

        if (sqlite3_step(statement) != SQLITE_DONE)
        {
            NSLog(@"ERREUR1: %s",sqlite3_errmsg(contactDB));
        }
        sqlite3_finalize(statement);
        sqlite3_close(contactDB);
    }
}


推荐答案

全部您需要做的是更改您在手机中保存SQLite文件的根目录,此时此刻只读取。进行以下更改:

All you need to do is change the root that you are saving your SQLite file in you phone which is readonly at the moment. make these changes :

将这些代码添加到 AppDelegate

.h文件:

@property (nonatomic,retain) NSFileManager *fileMgr;
@property (nonatomic,retain) NSString *homeDir;
@property (nonatomic,retain) NSString *title;

.m文件:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self checkAndCreateDatabase];
    return YES;
}

-(NSString *)GetDocumentDirectory{
    fileMgr = [NSFileManager defaultManager];
    homeDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    return homeDir;
}

    -(void) checkAndCreateDatabase{
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *databasePath = [self.GetDocumentDirectory stringByAppendingPathComponent:@"YOUR DATABASE NAME.sqlite"];
    success = [fileManager fileExistsAtPath:databasePath];
    if(success) {
        NSLog(@"working");
        return;}
    else{
        NSLog(@"notworking");
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"YOUR DATABASE NAME.sqlite"];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
    }
}

同样在你的 SQLite类中添加以下行:

Also in your SQLite class add these lines :

.h文件:

@property (nonatomic,retain) NSFileManager *fileMgr;
@property (nonatomic,retain) NSString *homeDir;

.m文件:

-(NSString *)GetDocumentDirectory{
    fileMgr = [NSFileManager defaultManager];
    homeDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    return homeDir;
}

以及在INSERT和SELECT中进行以下更改:

and also in INSERT and SELECT make these changes :

NSString *dbPath = [self.GetDocumentDirectory stringByAppendingPathComponent:@"YOUR DATABASE NAME.sqlite"];
    const char *dbpath = [dbPath UTF8String];

希望有所帮助:)

这篇关于sqlite ios:尝试编写只读数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆