您能否检查一下,名为favStatus 的列是您的数据库中定义的bool 类型或字符串类型。如果您在 db 上输入字符串值,但列 fvStatus 是 bool 类型。然后输入 VoucherId 和商家 ID 值作为字符串。请检查数据库中的值是否为字符串。
你能分享你的 Tb1 Schema 结构吗?
更新:
//
// VMViewController.m
// iDev_Sqlite
//
// Created by Pathik on 3/14/14.
// Copyright (c) 2014 Pathik. All rights reserved.
//
#import "VMViewController.h"
#import <sqlite3.h>
@interface VMViewController ()
{
sqlite3 *sqliteObj;
NSString *databasePath;
}
@end
@implementation VMViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createDatabase];
[self insertValuesInTable];
[self updateValuesInTable];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - SQLITE Actions
-(void)createDatabase{
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"VM_TEST_DB.sqlite"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &sqliteObj) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS tbl1 (ID INTEGER PRIMARY KEY AUTOINCREMENT, favStatus INTEGER, Voucher_ID TEXT, Merchent_ID TEXT)";
if (sqlite3_exec(sqliteObj, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(@"Failed to create table");
}
sqlite3_close(sqliteObj);
} else {
NSLog(@"Failed to open/create database");
}
}
}
-(void)insertValuesInTable{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &sqliteObj) == SQLITE_OK) {
NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO tbl1 (favStatus,Voucher_ID,Merchent_ID) VALUES (0,'102','102')"];
const char*insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(sqliteObj, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE) {
NSLog(@"Insert Successfully||");
} else {
NSLog(@"INSERT OPERTAION FALIED||");
}
sqlite3_finalize(statement);
sqlite3_close(sqliteObj);
}
}
-(void)updateValuesInTable{
if(sqlite3_open([databasePath UTF8String], &sqliteObj) == SQLITE_OK) {
NSString *cmd = [NSString stringWithFormat:@"UPDATE tbl1 SET favStatus=1 WHERE Voucher_ID='101' AND Merchent_ID='101';"];
const char * sql = [cmd UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(sqliteObj, sql, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_step(compiledStatement); // Here is the added step.
NSLog(@"update SUCCESS - executed command %@",cmd);
}
else {
NSLog(@"update FAILED - failed to execute command %@",cmd);
}
sqlite3_finalize(compiledStatement);
}
else {
NSLog(@" FAILED - failed to open database");
}
sqlite3_close(sqliteObj);
}
![output][1]
@end
[1]: http://i.stack.imgur.com/q0RTU.png