2014-03-13 36 views
0

我開發的應用程序來更新列,其中我是存儲在數據庫的數據,無法SQLite中

雖然將數據存儲到數據庫,我無法更新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包含Col1Col2Col3Col4favStatusCol6 6列,我想裏面favStatus列更新值。

寫這之後,favStatus列值沒有更新日誌顯示Successful update

即使在哪裏是我的錯?請幫忙。

感謝advnace。

+0

也許這已經是期望值。 – Mihai

+0

期望值爲'1'ii想要將其更改爲'0' – Krunal

+0

您需要提交數據嗎? (在SQLite的說法「BEGIN TRANSACTION/END TRANSACTION」我認爲....) – monojohnny

回答

0

您可以檢查,請在列表中定義名爲favStatus的列是bool類型或字符串類型。如果在db上輸入字符串值,但列fvStatusbool類型。並輸入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 
+0

favStatus列是整數類型,'VoucherId'和'商家ID'是字符串類型 – Krunal

+0

請參閱新的評論。 –

+0

更新一是工作代碼供您參考,請檢查一下,如果您有任何問題,請告訴我。 –

0

請檢查名爲favStatus列在您的數據庫中定義bool類型或字符串類型。

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

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

在你的代碼試試這個...

+0

favStatus列是整數類型 – Krunal