【问题标题】:Inserting into sqlite table插入sqlite表
【发布时间】:2013-06-18 00:31:13
【问题描述】:

我正在开发一个使用 sqlite 的 iPhone 应用程序。我正在尝试在表上插入一条记录。我的代码运行良好,但没有填充表格。我的代码如下所示。有人可以帮助解决该方法的问题。感谢您的帮助:

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

}

【问题讨论】:

    标签: ios sqlite


    【解决方案1】:
    sqlite3_step(statement);
    

    绑定后添加上述语句。

    【讨论】:

    • 我把上面的语句放了,表格还是没有填充数据。
    • 您应该从文档目录而不是从应用程序包中读取表格。
    • 这是包含方法的完整类。我要做的第一件事是创建一个可写的数据库并从中读取......你看到这有什么错误吗?
    【解决方案2】:

    据我所见,您正在准备查询,但从未真正执行它...

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2019-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多