我已编写此代码用于插入具有外键值的数据,但它不在Sqlite3_open方法中。请帮我 。我没有得到什么问题?
- (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);
}
答案 0 :(得分: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);
}