我正在开发应用程序,我将数据存储在数据库中,
在将数据存储到DB时,我无法更新DB中的列值,
这是我的代码:
我将更新查询传递给此方法:
QUERY = UPDATE Tbl SET favStatus ='0' WHERE Merchant_ID='1522' and Voucher_Id='5038'
//调用方法..
[self UpdateStatus:QUERY];
-(void)UpdateStatus:(NSString*)Query{
// Get the documents directory
databasePath=[self GetDBPath];
if(sqlite3_open([databasePath UTF8String],&contactDB)==SQLITE_OK)
{
sqlite3_stmt *compiledStmt;
if(sqlite3_prepare_v2(contactDB, [Query UTF8String],-1,&compiledStmt, NULL)==SQLITE_OK)
{
NSLog(@"Successful update");
}
sqlite3_step(compiledStmt);
sqlite3_close(contactDB);
}
}
我的Tbl包含Col1
,Col2
,Col3
,Col4
,favStatus
,Col6
6列,我想更新内部值favStatus
列。
写完此内容后,即使日志显示favStatus
Successful update
列值也不会更新
我的错误在哪里?请帮忙。
感谢advnace。
答案 0 :(得分:0)
您能否检查一下名为favStatus
的col是bool
类型或数据库中定义的字符串类型。如果在db上输入字符串值,但列fvStatus
为bool
类型。并输入VoucherId和商家ID值作为字符串。请检查DB中的值是否为字符串。
你能分享你的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
答案 1 :(得分:0)
请检查名为favStatus
的列是bool
类型或数据库中定义的字符串类型。
sqlite3_prepare_v2(_contactDB, insert_stmt,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
///successful
}
else
{
///Unsuccessful
}
在您的代码中尝试此操作...