I created a SQLite database that currently has only one table. I connected it to Xcode, made the all required steps, and found a problem in writing my files.
My table has 5 items (primary key, name, birth date, college name, course name).
While saving my data, it says:
Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Error while creating add statement. ‘library routine called out of sequence”
I set breakpoints and found where the problem is, but I just can’t solve it.
-(void)saveStudentInfo{
sqlite3_stmt *statement = NULL;
const char *sql = "insert into studentInfo (name,collegeName,courseName) Values (?, ?, ?)";
if (sqlite3_open("my_db_filename.db", &database) == SQLITE_OK)
{
//sqlite3_stmt *addStmt;
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
{
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
} else {
// do things with addStmt, call sqlite3_step
sqlite3_bind_text(addStmt, 1, [studentName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 3, [studentCollegeName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 4, [studentCourseName UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStmt))
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
studentID = sqlite3_last_insert_rowid(database);
sqlite3_finalize(statement);
}
sqlite3_close(database);
}
if (sqlite3_step(statement)==SQLITE_DONE){
UIAlertView *error = [[UIAlertView alloc]initWithTitle:@"Done" message:@"Files were added to database" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil, nil];
[error show];
}
else {
UIAlertView *error = [[UIAlertView alloc]initWithTitle:@"Error" message:@"StudentOrganizer could not add database" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil, nil];
[error show];
}
I think you forgot to open the database before calling sqlite3_prepare_v2.
A good way to do it is like this: