我在使用sqlite的iPhone App上工作。我想在桌子上插入一条记录。我的代码运行正常,但它没有填充表。我的代码如下所示。有人可以帮助解决该方法的问题。谢谢你的帮助:
- (void) saveProductDetails: (int)pklItemID :(NSString*)sItemDescription :(NSString*)barcodeValue :(int)lRemainingItems :(float)lCostPrice :(float)lSellingPrice
{
// The array of products that we will create
// NSMutableArray *products = [[NSMutableArray alloc] init];
NSLog(@"The ItemID in DBMethod is %d",pklItemID);
NSLog(@"The Selling Price in DBMethod is %f",lSellingPrice);
NSLog(@"The Cost Price in DBMethod is %f",lCostPrice);
NSLog(@"The Stock Quantity in DBMethod is %d",lRemainingItems);
NSDate* now = [NSDate date];
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO Spaza_Inventory (fklSpazaID,fklItemID,lRemainingItems,lCostPrice,lSellingPrice,fklUserID,fklSalesID,fklOrderListID,dtCostEffective,dtPriceEffective)\
VALUES ('%d','%d',' %d','%.02f','%.02f','%d','%d','%d','%@','%@')",0,pklItemID, lRemainingItems, lCostPrice, lSellingPrice,0,0,0,now, now];
NSLog(@"The SQl String is %@",insertSQL);
const char *sql = [insertSQL UTF8String];
//To run the above SQL in our code, we need to create an SQLite statement object. This object will execute our SQL against the database.
// The SQLite statement object that will hold the result set
sqlite3_stmt *statement;
// Prepare the statement to compile the SQL query into byte-code
int sqlResult = sqlite3_prepare_v2(database, sql, -1, &statement, NULL);
//After preparing the statement with sqlite3_prepare_v2 but before stepping through the results with sqlite3_step, we need to bind the parameters. We need to use the bind function that corresponds with the data type that we are binding.
sqlite3_bind_int(statement, 2, pklItemID);
sqlite3_bind_int(statement, 3, lRemainingItems);
sqlite3_bind_double(statement, 4, lCostPrice);
sqlite3_bind_double(statement, 5, lSellingPrice);
//sqlite3_bind_int(statement, 5, pklItemID);
//If the result is SQLITE_OK, we step through the results one row at a time using the sqlite3_step function:
if ( sqlResult== SQLITE_OK) {
// Step through the results - once for each row.
NSLog(@"Record Updated");
// Finalize the statement to release its resources
sqlite3_finalize(statement);
}
else {
NSLog(@"Problem with the database:");
NSLog(@"%d",sqlResult);
}
//return products;
}
答案 0 :(得分:0)
从我所看到的,你正在准备查询,但从未实际执行过......
答案 1 :(得分:0)
要执行查询,您需要致电sqlite3_step
。绑定所有变量后执行此操作。
在调用任何sqlite3_prepare_v2
语句之前,您还应该立即检查bind
的结果。
答案 2 :(得分:0)
sqlite3_step(statement);
绑定后添加上述语句。