这是我写的代码..
NSArray *path= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docpath = [path objectAtIndex:0];
dbpathstring = [docpath stringByAppendingPathComponent:@"UserTable.db"];
char *error,*error1;
NSFileManager *filemanager= [NSFileManager defaultManager];
if(![filemanager fileExistsAtPath:dbpathstring])
{
const char *dbpath=[dbpathstring UTF8String];
if(sqlite3_open(dbpath, &userdb1)==SQLITE_OK)
{
const char *sql_stat= "CREATE TABLE IF NOT EXISTS USERTABLE (CARDNUMBER INTEGER,BALANCE INTEGER,DATE TEXT)";
sqlite3_exec(userdb1, sql_stat, NULL, NULL, &error);
sqlite3_close(userdb1);
}
}
if(sqlite3_open([dbpathstring UTF8String], &userdb1)==SQLITE_OK)
{
NSString *insertstmnt= [NSString stringWithFormat:@"INSERT INTO USERTABLE (CARDNUMBER,BALANCE,DATE) VALUES((\"%d\", \"%d\", \"%s\")",[crd1 intValue],[bal1 intValue],[rdt1 UTF8String]];
const char *insert_stmt = [insertstmnt UTF8String];
sqlite3_exec(userdb1, insert_stmt, NULL, NULL, &error1);
NSLog(@"After Creation in insert exec");
NSLog(@"row added");
UserTables *usertable = [[UserTables alloc]init];
[usertable setCardnumber:[crd1 intValue]];
[usertable setBalance:[bal1 intValue]];
[usertable setDate:rdt1];
[sections addObject:usertable];
sqlite3_close(userdb1);
答案 0 :(得分:0)
您的(
查询中有额外的INSERT
,导致语法错误:
INSERT INTO USERTABLE (CARDNUMBER,BALANCE,DATE) VALUES((\"%d\", \"%d\", \"%s\")
更改为
INSERT INTO USERTABLE (CARDNUMBER,BALANCE,DATE) VALUES(\"%d\", \"%d\", \"%s\")