我在项目中使用 sqlite 数据库。我可以执行像 SELECT 这样的查询,但无法执行 INSERT!
在模拟器上 INSERT 工作正常。当我在 iPod 上编译时,就会出现以下错误消息:“尝试写入只读数据库”.
认为这是文件的权利,我做了: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);
}
}