2012-08-30 79 views
0

我已經編寫了用於插入具有外鍵值的數據的代碼,但它不在Sqlite3_open方法中。請幫幫我 。我沒有得到什麼問題?添加語句不適用於sqlite

- (void)AddCashGame { 
    sqlite3_stmt *addStmt; 

    appDelegate = (XYZAppDelegate *)[UIApplication sharedApplication].delegate; 
    NSString *dbPath = [appDelegate.GetDocumentDirectory stringByAppendingPathComponent:@"PokerDatabase.sqlite"]; 
    NSLog(@"Db path is %@",dbPath); 
    if (sqlite3_open([dbPath UTF8String], &database)){ 
    const char *sql="insert into CashGameCell (CashLocation,CashGames,CashBank,CashBuy,CashFood,CashNote,CashRebuyCount,CashRebuyTotal,CashTokes,CashPlayer,CashProfileid) values (?,?,?,?,?,?,?,?,?,?,?)"; 
    if (sqlite3_prepare(database, sql, -1, &addStmt, NULL)!= SQLITE_OK) { 
     NSLog(@"Error while creating database %s",sqlite3_errmsg(database)); 

    } 
    sqlite3_bind_text(addStmt, 1, [CashLocation UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(addStmt, 2, [CashGames UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(addStmt, 3, [CashBank UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(addStmt, 4, [CashBuy UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(addStmt, 5, [CashFood UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(addStmt, 6, [CashNote UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(addStmt, 7, [CashRebuyCount UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(addStmt, 8, [CashRebuyTotal UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(addStmt, 9, [CashTokes UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_text(addStmt, 10, [CashPlayer UTF8String], -1, SQLITE_TRANSIENT); 
    sqlite3_bind_int(addStmt, 11, CashProfileid); 
    } 

if (SQLITE_DONE!= sqlite3_step(addStmt)) { 
    NSLog(@"Error while inserting"); 
} 
else Cashid = sqlite3_last_insert_rowid(database); 
NSLog(@"cash id in profile model %d",Cashid); 
sqlite3_finalize(addStmt); 
sqlite3_close(database); 

}

回答

1

我有類似的代碼值添加到數據庫中。爲什麼不試試這個:

- (void)AddCashGame { 
sqlite3 *database; 
NSString *path =[NSSearchPathForDirectoriesInDomains(NSDocumentDictionary, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *database_path=[path stringByAppendingPathComponent:@"PokerDatabase.sqlite"]; 

const char *dbPath = [database_path UTF8String]; 

NSLog(@"Db path is %@",dbPath); 

sqlite3_stmt *statment; 

NSString *q=[NSString stringwithFormat:@"insert into CashGameCell (CashLocation,CashGames,CashBank,CashBuy,CashFood,CashNote,CashRebuyCount,CashRebuyTotal,CashTokes,CashPlayer,CashProfileid) values (\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")", value1,value2,value3,value4,value5,value6,value7,value8,value9,value10,value11]; 

const char *query = [q UTF8String]; 

if (sqlite3_open(dbPath, &database)==SQLITE_OK){ 

    if (sqlite3_prepare_v2(database, query, -1, &statement, nil)== SQLITE_OK) { 

    if(sqlite3_step(statement){ 
    //I assume this code below is correct   
     sqlite3_bind_text(addStmt, 1, [CashLocation UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(addStmt, 2, [CashGames UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(addStmt, 3, [CashBank UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(addStmt, 4, [CashBuy UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(addStmt, 5, [CashFood UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(addStmt, 6, [CashNote UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(addStmt, 7, [CashRebuyCount UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(addStmt, 8, [CashRebuyTotal UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(addStmt, 9, [CashTokes UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(addStmt, 10, [CashPlayer UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_int(addStmt, 11, CashProfileid); 
    } 
    } 
    else{ 
    NSLog(@"Error while creating database %s",sqlite3_errmsg(database)); 
    } 
} 

sqlite3_close(database); 
} 
+0

好吧讓我試試... – DeviPhone26