I have an SQLite database though when I try to dynamically add items it doesn't work.
Here is the add script:
-(void) addPatientToDatabase:(Patient *)newPatient {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"cities.sqlite"];
sqlite3 *database;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "insert into patients (firstName, surname, dob, homeNumber, mobileNumber, email, address, image) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_text(compiledStatement, 1, [newPatient.patientName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 2, [newPatient.patientSurname UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 3, [newPatient.patientDoB UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 4, [newPatient.patientHomeNumber UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 5, [newPatient.patientMobileNumber UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 6, [newPatient.patientEmail UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 7, [newPatient.patientAddress UTF8String], -1, SQLITE_TRANSIENT);
NSData *dataForPicture = UIImagePNGRepresentation(newPatient.patientPicture);
sqlite3_bind_blob(compiledStatement, 8, [dataForPicture bytes], [dataForPicture length], SQLITE_TRANSIENT);
}
if(sqlite3_step(compiledStatement) == SQLITE_DONE) {
sqlite3_finalize(compiledStatement);
}
}
sqlite3_close(database);
}
I have add some breakpoints and noted that if there is a breakpoint within this if statement:
if(sqlite3_step(compiledStatement) == SQLITE_DONE) {
Then it doesn't get picked up.
Thanks in advance
When step returns anything other then SQLITE_DONE, try calling: sqlite3_errmsg(database);
It will return a pointer to an error message( http://www.sqlite.org/c3ref/errcode.html ). Display or log that message, to find out whats wrong.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.