How to use sqlite_prepare_statement to InsertData in Sqlite Database in iphone application
-(void)insertDataImage_ID:(NSInteger)ID ImageName:(NSString*)Image Bookmark:(NSString*)Title
{
[self checkAndCreateDB];
sqlite3 *database;
if (sqlite3_open([DBPath UTF8String], &database)==SQLITE_OK) {
NSString *statement;
sqlite3_stmt *compliedstatement;
statement =[[NSString alloc] initWithFormat:@"insert into tblBookMark values(%d, '%@' , '%@')", ID,Image, Title ];
statement = [[NSString alloc] initWithFormat :@"select * from tblBookMark"];
NSLog(@"T-1");
const char *sqlstatement = [statement UTF8String];
if (sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL) == SQLITE_OK) {
NSLog(@"T-2");
if (SQLITE_DONE!=sqlite3_step(compliedstatement)) {
NSLog(@"T-3");
NSAssert1 (0,@"Error by inserting '%s'",sqlite3_errmsg(database));
UIAlertView *AlertOK=[[UIAlertView alloc] initWithTitle:@"Error !" message:@"Error by insertin开发者_开发问答g" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes",nil];
[AlertOK show];
[AlertOK release];
}
NSLog(@"T-4");
sqlite3_finalize(compliedstatement);
}
NSLog(@"T-5");
}
sqlite3_close(database);
NSLog(@"T-6");
}
What is wrong in this code. It not showing any error but it
if (sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL) == SQLITE_OK)
Code flow is not entering in this line can any one help me to identify the error or suggest me solution.
Code
static sqlite3_stmt *insertStmt = nil;
if(insertStmt == nil)
{
insertSql = "INSERT INTO Loginchk (uname,password) VALUES(?,?)";
if(sqlite3_prepare_v2(database, insertSql, -1, &insertStmt, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating insert statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(insertStmt, 1, [Gunameq UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insertStmt, 2, [Gpassq UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(insertStmt))
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
NSLog("Inserted");
//Reset the add statement.
sqlite3_reset(insertStmt);
insertStmt = nil;
You're not doing any error checking at all, no wonder you don't know what's going wrong!
Take a look at the sqlite 3 error codes. If the error is not SQLITE_OK, what is it?
if (sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL) == SQLITE_OK) {
...
} else {
NSLog(@"%i - %@", sqlite3_errcode(database), sqlite3_errmsg(database));
}
fix your query..."INSERT INTO tblBookMark (field1,field2) VALUES("","")";
精彩评论