Not able to insert data in to sqlite database on iphone
I am trying to create(successful), insert(problem here) and select(just to check i i am going right) records into data base.
My whole code is as:
-(NSInteger) InsertDataInDatabae {
char *sqlStatement;
sqlite3 *pDb;
char *errorMsg;
int returnCode;
char *databaseName;
databaseName="WaitListAll.db";
returnCode = sqlite3_open(databaseName, &pDb);
if(returnCode!=SQLITE_OK) {
NSLog(@"Error in opening Database");
return -1;
}
sqlStatement="drop table if exists waitlistallTable";
returnCode = sqlite3_exec(pDb, sqlStatement, NULL, NULL, &errorMsg);
if(returnCode!=SQLITE_OK) {
NSLog(@"Table could not be droped");
return -1;
}
sqlStatement="create table waitlistallTable(source varchar(10),waitlistid varchar(10),""restaurantid varchar(10),consumerid varchar(10), timeadded varchar(10))";
returnCode=sqlite3_exec(pDb, sqlStatement, NULL, NULL, &errorMsg);
if(returnCode!=SQLITE_OK) {
NSLog(@"Table could not be Created Error:%@",errorMsg);//**gettin开发者_开发技巧g error here as:unrecognized token ""**
return -1;
}
NSInteger i;
for(i=0;i<[waitListAllRecordsArray count];i++) {
NSDictionary *objDict=[waitListAllRecordsArray objectAtIndex:i];
NSString *src,*wid,*rid,*cid,*tadded;
src=[objDict objectForKey:@"SOURCE"];
wid=[objDict objectForKey:@"WAITLISTID"];
rid=[objDict objectForKey:@"RESTAURANTID"];
cid=[objDict objectForKey:@"CONSUMERID"];
tadded=[objDict objectForKey:@"TIMEADDED"];
printf("\n %s %s %s %s %s",src,wid,rid,cid);
sqlStatement = sqlite3_mprintf("insert into waitlistallTable values(%s, %s, %s, %s, %s)",src,wid,rid,cid,tadded);
returnCode=sqlite3_exec(pDb, sqlStatement, NULL, NULL, &errorMsg);
if(returnCode!=SQLITE_OK) {
printf("Error in inserting record :%s",errorMsg);
return -1;
}
}
sqlite3_stmt *statement;
sqlStatement=sqlite3_mprintf("selet * from waitlistallTable");
returnCode = sqlite3_prepare_v2(pDb, sqlStatement, strlen(sqlStatement), &statement,NULL);
if(returnCode!=SQLITE_OK) {
NSLog(@"Record could not be Inserted Error:%@",errorMsg);
return -1;
}
returnCode=sqlite3_step(statement);
while(returnCode==SQLITE_ROW) {
char *src,*wid,*rid,*cid,*tadded;
src=sqlite3_column_text(statement, 0);
wid=sqlite3_column_text(statement, 1);
rid=sqlite3_column_text(statement, 2);
cid=sqlite3_column_text(statement, 3);
tadded=sqlite3_column_text(statement, 4);
NSLog(@"%@\t %@\t %@ \t%@ %@",src,wid,rid, cid,tadded);
}
sqlite3_finalize(statement);
sqlite3_close(&pDb);
// sqlite3_free(sqlStatement);
//NSLog(@"%@\t %@\t %@ \t%@ %@",src,wid,rid, cid,tadded); //NSLog(@"*******object for dictionary is **************%@",[objDict objectForKey:@"SOURCE"]);
}
Any suggestion would be very helpful. thanks
You are trying to insert NSString object with %s formatter, try out:
sqlStatement=sqlite3_mprintf("insert into waitlistallTable values(%s, %s, %s, %s, %s)",[src UTF8String],[wid UTF8String],[rid UTF8String],[cid UTF8String],[tadded UTF8String]);
hope it'll help..!
INSERT INTO history (prescriptionname,prescriptiondate,doctorname,personname,profileid) VALUES(?,?,?,?,?)
Insert the values using above format
精彩评论