0

I'm working on an iPhone App which uses sqlite. I am trying to insert a record on a table. My code runs fine but it does not populate the table. My code is as shown below. Can someone help on what is wrong with the method. Thanks for the help:

- (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;

}

4

3 回答 3

0

从我所看到的,您正在准备查询,但从未真正执行它......

于 2013-06-17T16:26:39.443 回答
0

要执行查询,您需要调用sqlite3_step. 在绑定所有变量后执行此操作。

在调用任何语句sqlite3_prepare_v2之前,您还应该立即检查结果。bind

于 2013-06-17T16:28:53.023 回答
0
sqlite3_step(statement);

绑定后添加上述语句。

于 2013-06-17T16:30:36.427 回答