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”.
Thinking it is a right of the file I did a: chmod 777 mydatabase.sqlite
But that does not change!
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: 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);
}
}
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 :
Add these codes to your
AppDelegate:.h file:
.m file:
Also in your
SQLite classadd these lines :.h file:
.m file:
and also in INSERT and SELECT make these changes :
Hope it helps 🙂