SQLite iOS Insérer des données

Im essayant d'insert juste un nom à mon dossier de sqlite. Im en utilisant ce code, mais ça ne fonctionne pas: /

-(void)InsertRecords:(NSMutableSsortingng *) txt{ if(addStmt == nil) { const char *sql = "INSERT INTO myMovies (movieName) VALUES(?) "; if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK) NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database)); else sqlite3_bind_text(addStmt, 1, [txt UTF8Ssortingng], -1, SQLITE_TRANSIENT); } if(SQLITE_DONE != sqlite3_step(addStmt)) NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database)); else sqlite3_reset(addStmt); } 

passez votre requête à cette méthode et essayez,

 -(void)Insertdata:(NSSsortingng*)query{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSSsortingng *documentsDirectory = [paths objectAtIndex:0]; NSSsortingng *databasePath = [documentsDirectory ssortingngByAppendingPathComponent:@"YourDBName.sql"]; if(sqlite3_open([databasePath UTF8Ssortingng],&db) == SQLITE_OK) { NSSsortingng *querySQL = [NSSsortingng ssortingngWithFormat: @"%@",query]; char *errmsg=nil; if(sqlite3_exec(db, [querySQL UTF8Ssortingng], NULL, NULL, &errmsg)==SQLITE_OK) { NSLog(@".. Row Added .."); } } sqlite3_close(db); } 

Une erreur "out of memory" de sqlite signifie généralement que le handle de database que vous utilisez n'a pas encore été ouvert. Assurez-vous que vous appelez sqlite3_open_v2 avec la variable de database indiquée dans le code que vous avez publié?

ok enfin je l'ai fait! c'est le code que j'ai utilisé pour tous ceux qui en ont besoin:

 -(void)InsertRecords:(NSMutableSsortingng *)txt{ NSSsortingng *dbPath = [[[NSBundle mainBundle] resourcePath ]ssortingngByAppendingPathComponent:@"movieData.sqlite"]; const char *dbpath = [dbPath UTF8Ssortingng]; sqlite3 *contactDB; sqlite3_stmt *statement; NSLog(@"%@",dbPath); if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) { NSSsortingng *insertSQL = [NSSsortingng ssortingngWithFormat: @"INSERT INTO myMovies (movieName) VALUES (\"%@\")", txt]; const char *insert_stmt = [insertSQL UTF8Ssortingng]; sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL); if (sqlite3_step(statement) == SQLITE_DONE) { sqlite3_bind_text(statement, 1, [txt UTF8Ssortingng], -1, SQLITE_TRANSIENT); } else { NSLog(@"error"); } sqlite3_finalize(statement); sqlite3_close(contactDB); } } 

Essaye ça:

 //save our data - (BOOL) saveEmployee:(Employee *)employee { BOOL success = false; sqlite3_stmt *statement = NULL; const char *dbpath = [databasePath UTF8Ssortingng]; if (sqlite3_open(dbpath, &mySqliteDB) == SQLITE_OK) { if (employee.employeeID > 0) { NSLog(@"Exitsing data, Update Please"); NSSsortingng *updateSQL = [NSSsortingng ssortingngWithFormat:@"UPDATE EMPLOYEES set name = '%@', department = '%@', age = '%@' WHERE id = ?", employee.name, employee.department, [NSSsortingng ssortingngWithFormat:@"%d", employee.age]]; const char *update_stmt = [updateSQL UTF8Ssortingng]; sqlite3_prepare_v2(mySqliteDB, update_stmt, -1, &statement, NULL ); sqlite3_bind_int(statement, 1, employee.employeeID); if (sqlite3_step(statement) == SQLITE_DONE) { success = true; } } else{ NSLog(@"New data, Insert Please"); NSSsortingng *insertSQL = [NSSsortingng ssortingngWithFormat: @"INSERT INTO EMPLOYEES (name, department, age) VALUES (\"%@\", \"%@\", \"%@\")", employee.name, employee.department, [NSSsortingng ssortingngWithFormat:@"%d", employee.age]]; const char *insert_stmt = [insertSQL UTF8Ssortingng]; sqlite3_prepare_v2(mySqliteDB, insert_stmt, -1, &statement, NULL); if (sqlite3_step(statement) == SQLITE_DONE) { success = true; } } sqlite3_finalize(statement); sqlite3_close(mySqliteDB); } return success; }