2011-10-24 40 views
3

我想插入一些數據到mydatabase.sqlite,但我有一個問題。插入數據時沒有錯誤消息,但隨後我打開數據庫,但無法看到任何數據。沒有任何數據。SQLite插入數據在目標c

這是addCoffee方法MyClass的:

if(addStmt == nil) { 
    const char *sql = "insert into records(Date, Latitude, Longitude) Values(?, ?, ?)"; 
    if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK) 
     NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database)); 
} 

sqlite3_bind_text(addStmt, 1, [Date UTF8String], -1, SQLITE_TRANSIENT); 
sqlite3_bind_double(addStmt, 2, [Latitude doubleValue]); 
sqlite3_bind_double(addStmt, 3, [Longitude doubleValue]); 

if(SQLITE_DONE != sqlite3_step(addStmt)) 
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database)); 
else 
    //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid 
    recordID = sqlite3_last_insert_rowid(database); 

//Reset the add statement. 
sqlite3_reset(addStmt); 

發送數據:

iDailyAppDelegate *appDelegate = (iDailyAppDelegate *)[[UIApplication sharedApplication] delegate]; 

//Create a MyClass Object. 
MyClass *coffeeObj = [[MyClass alloc] initWithPrimaryKey:0]; 
coffeeObj.Date = dateInString; 
NSDecimalNumber *temp = [[NSDecimalNumber alloc] initWithString:first]; 
coffeeObj.Latitude = temp; 
NSDecimalNumber *temp2 = [[NSDecimalNumber alloc] initWithString:second]; 
coffeeObj.Longitude = temp2; 
[temp release]; 
coffeeObj.isDirty = NO; 
coffeeObj.isDetailViewHydrated = YES; 

//Add the object 
[appDelegate addCoffee:coffeeObj]; 
+0

是否有可能你執行'BEGIN TRANSACTION'如此沒有執行相應的「COMMIT」? –

+0

抱歉,我能做什麼,我不明白你? –

+0

請幫我! –

回答

1
- (void)inserting{ 

    database=nil; 
    if (sqlite3_open([[DBAction getSqlitePath] UTF8String], &database) == SQLITE_OK){ 
     const char *sql = [[NSString stringWithFormat:@"insert into SiteTable set Site_Address = '%@'",someString] UTF8String]; 
     sqlite3_stmt *updateStmt = nil; 
     if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK) 
     { 
      //NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database)); 
     } 
     if (SQLITE_DONE != sqlite3_step(updateStmt)){ 
      //NSAssert1(0, @"Error while creating database. '%s'", sqlite3_errmsg(database)); 
     } 
     sqlite3_reset(updateStmt); 
     sqlite3_finalize(updateStmt); 
    } 
    else{ 
     //NSAssert1(0, @"Error while opening database '%s'", sqlite3_errmsg(database)); 
    } 
    sqlite3_close(database); 

} 
+0

爲什麼你使用update語句而不是insert?另外,我嘗試過這種方式,但它不是記錄:S –

+0

而不是更新,你可以使用插入 – Tendulkar

+0

看看修改後的答案 – Tendulkar

1

爲您發送到數據庫,然後嘗試從執行它們所有SQL命令創建的NSLog終端,你會看到所有的錯誤,並能夠修復它們

+0

每個sql語句後有nslog ..它不給我任何錯誤信息! –

+0

我修正了thnx –