gpt4 book ai didi

ios - sqlite ios : attempt to write a readonly database

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:04:08 31 4
gpt4 key购买 nike

我在项目中使用 sqlite 数据库。我可以执行 SELECT 之类的查询,但无法执行 INSERT!在模拟器上,INSERT 工作正常。一旦我在我的 iPod 上编译,就会出现这个错误信息:“attempt to write a readonly database”

以为是文件的权限我做了一个:chmod 777 mydatabase.sqlite但这并没有改变!

我也试过按照我在其他网站上看到的那样,复制文件使用他的副本就可以了,但是没有用。

你有解决办法吗?亲切地。

PS:这是我的代码:

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 文件的根目录,该目录目前是只读的。进行这些更改:

将这些代码添加到您的 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 类 中添加这些行:

.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 中进行这些更改:

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

希望对你有帮助:)

关于ios - sqlite ios : attempt to write a readonly database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12000311/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com