Ios 无法更新sqlite中的列

Ios 无法更新sqlite中的列,ios,sql,iphone,ipad,sqlite,Ios,Sql,Iphone,Ipad,Sqlite,我正在开发一个应用程序,在其中我将数据存储在数据库中 在将数据存储到DB时,我无法更新我在DB中的列值 这是我的代码: 我正在将更新查询传递给此方法: QUERY = UPDATE Tbl SET favStatus ='0' WHERE Merchant_ID='1522' and Voucher_Id='5038' //调用方法 [自我更新状态:查询] -(void)UpdateStatus:(NSString*)Query{ // Get the documents direc

我正在开发一个应用程序,在其中我将数据存储在数据库中

在将数据存储到DB时,我无法更新我在DB中的列值

这是我的代码:

我正在将更新查询传递给此方法:

QUERY = UPDATE Tbl SET favStatus ='0' WHERE Merchant_ID='1522' and Voucher_Id='5038'
//调用方法

[自我更新状态:查询]

-(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
列值即使在日志显示
成功更新后也不会得到更新

我的错在哪里?请帮忙


谢谢您的建议。

您可以检查一下,名为
favStatus
的列是数据库中定义的
bool
类型或字符串类型。如果在db上输入字符串值,但列
fvStatus
bool
类型。然后输入凭证ID和商户ID值作为字符串。请检查DB中的值是否为字符串

你能分享你的Tb1模式结构吗

更新:

//
//  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
//
//VMViewController.m
//iDev_Sqlite
//
//由Pathik于2014年3月14日创建。
//版权所有(c)2014 Pathik。版权所有。
//
#导入“VMViewController.h”
#进口
@接口VMViewController()
{
sqlite3*sqliteObj;
NSString*数据库路径;
}
@结束
@VMViewController的实现
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
[自建数据库];
[自插入值列表];
[自更新值列表];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#pragma标记-SQLITE操作
-(void)创建数据库{
NSString*docsDir;
NSArray*dirpath;
dirpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
docsDir=[dirpath objectAtIndex:0];
//生成数据库文件的路径
databasePath=[[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@“VM_TEST_DB.sqlite”];
NSFileManager*filemgr=[NSFileManager defaultManager];
if([filemgr fileExistsAtPath:databasePath]==否)
{
const char*dbpath=[databasePath UTF8String];
if(sqlite3_打开(dbpath,&sqliteObj)==SQLITE_正常)
{
char*errMsg;
const char*sql_stmt=“创建不存在的表tbl1(ID整数主键自动递增、favStatus整数、凭证ID文本、Merchent_ID文本)”;
if(sqlite3_exec(sqliteObj、sql stmt、NULL、NULL和errMsg)!=SQLITE_OK)
{
NSLog(@“未能创建表”);
}
sqlite3_关闭(sqliteObj);
}否则{
NSLog(@“无法打开/创建数据库”);
}
}
}
-(无效)插入值可插入{
sqlite3_stmt*语句;
const char*dbpath=[databasePath UTF8String];
if(sqlite3_打开(dbpath,&sqliteObj)==SQLITE_正常){
NSString*insertSQL=[NSString stringWithFormat:
@“将值(0,'102','102')插入tbl1(favStatus,凭证ID,Merchent_ID)中”];
常量字符*insert_stmt=[insertSQL UTF8String];
sqlite3\u prepare\u v2(sqliteObj,insert\u stmt,-1,&语句,NULL);
if(sqlite3\u步骤(语句)==SQLITE\u完成){
NSLog(@“插入成功”| |“);
}否则{
NSLog(@“插入操作错误”);
}
sqlite3_最终确定(声明);
sqlite3_关闭(sqliteObj);
}
}
-(void)updateValuesInTable{
if(sqlite3_打开([databasePath UTF8String],&sqliteObj)==SQLITE_正常){
NSString*cmd=[NSString stringWithFormat:@“更新tbl1集合favStatus=1,其中凭证_ID='101'和美世_ID='101';”;
const char*sql=[cmd UTF8String];
sqlite3_stmt*编译语句;
if(sqlite3\u prepare\u v2(sqliteObj,sql,-1,&compiledStatement,NULL)=SQLITE\u OK){
sqlite3_步骤(compiledStatement);//下面是添加的步骤。
NSLog(@“更新成功-已执行命令%@”,cmd);
}
否则{
NSLog(@“更新失败-无法执行命令%@”,cmd);
}
sqlite3_最终确定(编译语句);
}
否则{
NSLog(@“失败-无法打开数据库”);
}
sqlite3_关闭(sqliteObj);
}
![输出][1]
@结束
[1]: http://i.stack.imgur.com/q0RTU.png

请检查名为
favStatus
的列是否为数据库中定义的
bool
类型或字符串类型

sqlite3_prepare_v2(_contactDB, insert_stmt,-1, &statement, NULL);

if (sqlite3_step(statement) == SQLITE_DONE)
{
           ///successful 
} 
else 
{
          ///Unsuccessful 
}

在代码中尝试此操作…

可能它已经是所需的值。所需的值为“1”我想将其更改为“0”是否需要提交数据?(“开始交易/结束交易”在SQLite的说法中,我想…)是的,我想提交datafavStatus列是整数类型,
VoucherId
merchant ID
是字符串类型请参考新注释。更新一个是工作代码供您参考,请检查,如果您有任何问题,让我知道。我已经接受了你的答案。请投票我的问题FAVSTATUS列是整数类型