我正在構建一個使用storyboard的iOS應用程序。我使用SQLite創建了一個表格。在SQLite ios中獲取錯誤?
我面臨着數據的插入問題表明我的錯誤:
Failed to insert record rc:1, msg=no such column: Afternoon
這裏是我的代碼:
創建表的代碼:
char * query ="CREATE TABLE IF NOT EXISTS events (id INTEGER PRIMARY KEY AUTOINCREMENT, eventid TEXT, addinfo TEXT,date TEXT,players TEXT, sportsname TEXT, Time TEXT,userid TEXT, venue TEXT)";
插入表中的功能:
-(int) insert:(NSString *)filePath withName:(NSString *)eventid addinfo:(NSString*)addinfo date:(NSString*)date
players:(NSString*)players sportsname:(NSString*)sportsname Time:(NSString*)Time userid:(NSString*)userid venue:(NSString*)venue {
sqlite3* db = NULL;
int rc=0;
rc = sqlite3_open_v2([filePath cStringUsingEncoding:NSUTF8StringEncoding], &db, SQLITE_OPEN_READWRITE , NULL);
if (SQLITE_OK != rc) {
sqlite3_close(db);
NSLog(@"Failed to open db connection");
}
else {
NSString * query = [NSString stringWithFormat:@"INSERT INTO events (eventid,addinfo,date,players,sportsname,Time,userid,venue)
VALUES (\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",%@,\"%@\",\"%@\")", eventid,addinfo,date,players,sportsname,Time,userid,venue];
char * errMsg;
rc = sqlite3_exec(db, [query UTF8String] ,NULL,NULL,&errMsg);
if (SQLITE_OK != rc) {
NSLog(@"Failed to insert record rc:%d, msg=%s",rc,errMsg);
}
sqlite3_close(db);
}
return rc;
}
獲取記錄功能
-(NSArray *) getRecords:(NSString*) filePath where:(NSString *)whereStmt {
NSMutableArray * events =[[NSMutableArray alloc] init];
sqlite3* db = NULL;
sqlite3_stmt* stmt =NULL;
int rc=0;
rc = sqlite3_open_v2([filePath UTF8String], &db, SQLITE_OPEN_READONLY , NULL);
if (SQLITE_OK != rc) {
sqlite3_close(db);
NSLog(@"Failed to open db connection");
} else {
NSString * query = @"SELECT * from events";
if(whereStmt) {
query = [query stringByAppendingFormat:@" WHERE %@",whereStmt];
}
rc =sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, NULL);
if(rc == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW) //get each row in loop
{
NSString * eventid = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
NSString *addinfo = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
NSString *date = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];
NSString *players =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 4)];
NSString *sportsname =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 5)];
NSString *Time =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 6)];
NSString *userid =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 7)];
NSString *venue =[NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 8)];
NSDictionary *student = [NSDictionary dictionaryWithObjectsAndKeys:eventid,@"eventid",addinfo,@"addinfo",date,@"date",players,@"players",sportsname,@"sportsname",Time,@"Time",userid,@"userid",venue,@"venue",nil];
[events addObject:student];
NSLog(@"eventsid= %@, userid=%@ , sportsname= %@ , date= %@ , Time= %@,players= %@, venue= %@, addinfo=%@",eventid,userid,sportsname,date,Time,players,venue,addinfo);
}
NSLog(@"Done");
sqlite3_finalize(stmt);
}
else {
NSLog(@"Failed to prepare statement with rc:%d",rc);
}
sqlite3_close(db);
}
return events;
}
NSData* data = [NSData dataWithContentsOfURL:url];
NSArray *ys_avatars = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"arrayyyyyy=%@",ys_avatars);
if(ys_avatars) {
for (int j=0;j<ys_avatars.count;j++) {
int rc= [self insert:[self getDbFilePath] withName:ys_avatars[j][@"_id"] addinfo:ys_avatars[j][@"addinfo"] date:ys_avatars[j][@"date"] players:ys_avatars[j][@"players"] sportsname:ys_avatars[j][@"sportsname"] Time:ys_avatars[j][@"time"] userid:ys_avatars[j][@"userid"] venue:ys_avatars[j][@"venue"]];
if(rc != SQLITE_OK) {
[self showMessage:@"ERROR" withMessage:@"Failed to insert record"];
}
else
[self showMessage:@"SUCCESS" withMessage:@"Record is added"];
NSArray * events = [self getRecords:[self getDbFilePath] where:nil];
感謝Lalji,我lauhing我怎麼錯過了這一點。 – Hitesh 2014-12-04 06:53:05