2015-01-01 25 views
-1

我已經設置了一個GlobalVars類來容納我的sqlite3數據庫變量。sqlite3準備語句不工作,可能不適當的指針iOS

static sqlite3** database; 
const char *dbPath; 


@implementation GlobalVars : NSObject 

+(GlobalVars*)sharedInstance { 
    static GlobalVars *myInstance = nil; 
    if(myInstance == nil) { 
     myInstance = [[[self class] alloc] init]; 
    } 

    return myInstance; 
} 

+(sqlite3*)getGlobalDatabase { 
    return &database; 
} 

+(void)setGlobalDatabase:(sqlite3*)_database { 
    database = &_database; 
} 

然後在頭文件我有

static sqlite3** database; 

這是接口的上方。這是我如何設置我的數據庫變量。

我然後試圖訪問它,當我打開數據庫並準備它。我可以打開它,因爲打開的調用返回true。我無法準確地進行準備,因爲該語句返回false,並且不會進入if語句。我想知道我是否搞亂了我的指針,因爲prepare語句不起作用,而且它沒有正確準備。

-(void)setAllValues:(NSMutableArray*)array { 
if(sqlite3_open([GlobalVars getGlobalDBPath], [GlobalVars getGlobalDatabase]) == SQLITE_OK) { 
    sqlite3_stmt *insertStatement; 
    NSString *sqlInsert = [NSString stringWithFormat:@"insert into my_table ('_id', 'name', 'age', 'weight', 'height', 'description') VALUES (%i, '%@', '%i', '%i', '%@', '%@')", ID, name, age, weight, height, description]; 

//*********** This is not opening, and SQLITE_OK is equal to false *********** 

if(sqlite3_prepare_v2(([GlobalVars getGlobalDatabase]), [sqlInsert UTF8String], -1, &insertStatement, nil) == SQLITE_OK) { 
     if(sqlite3_step(insertStatement) == SQLITE_DONE) { 
      NSLog(@"insert stepping done"); 
     } 

     sqlite3_reset(insertStatement); 
    } 
    sqlite3_finalize(insertStatement); 
    sqlite3_close([GlobalVars getGlobalDatabase]); 
} 
} 

數據庫的變量都填充了正確的數據,它似乎打開數據庫沒有問題。在準備時,它不能正常工作並返回錯誤。任何想法爲什麼。感謝您的幫助,任何幫助表示讚賞。

+0

登錄'sqlite3_errmsg'。 – rmaddy

+0

那麼,sqlite3_errmsg說什麼?什麼是從sqlite3_prepare_v2返回的返回碼? –

+0

@HotLicks sqlite3_errmsg說「庫程序調用不按順序」 –

回答

-1

**

你一定要嚐嚐這個......也許它可以幫助您:當調用`sqlite3_prepare_v2`失敗

**

#import "DBManager.h" 
#import "userRegistrationClass.h" 

static DBManager *sharedInstance = nil; 
static sqlite3 *database = nil; 
static sqlite3_stmt *statement = nil; 

@implementation DBManager 


#pragma mark 
#pragma mark Get shared Function 
+(DBManager*)getSharedInstance{ 
    if (!sharedInstance) { 
     sharedInstance = [[super allocWithZone:NULL]init]; 
     [sharedInstance createDB]; 
    } 
    return sharedInstance; 
} 

#pragma mark 
#pragma mark Create DataBase 
-(BOOL)createDB{ 
    NSString *docsDir; 
    NSArray *dirPaths; 
    // Get the documents directory 
// http://www.mycashkit.com/my-earnings.php 
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSLog(@"Dir Path Value is %@",dirPaths); 
    docsDir = [dirPaths objectAtIndex:0]; 
    NSLog(@"DocsDir Path Value is %@",docsDir); 
    // Build the path to the database file 
    databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent: @"UserRegInfo.sqlite"]]; 
    NSLog(@"Data base Work's %@:",databasePath); 
    BOOL isSuccess = YES; 
    NSFileManager *filemgr = [NSFileManager defaultManager]; 
    // NSString *currentPath = [filemgr currentDirectoryPath]; 
    NSLog(@"My file managaer value is %@",filemgr); 
    //NSLog(@"My current drictory path is %@",currentPath); 
    //the file will not be there when we load the application for the first time 
    //so this will create the database table 
    if ([filemgr fileExistsAtPath: databasePath ] == NO) 
    { 
     const char *dbpath = [databasePath UTF8String]; 

     NSLog(@"Constan charcter value is %s:-",dbpath); 

     if (sqlite3_open(dbpath, &database) == SQLITE_OK) 
     { 
      char *errMsg; 
      const char *sql_stmt = "create table if not exists UserInfo(UserID integer primary key, UserName,Gender,UserEmailID,Password,RePassword,DOB, MobileNo,IsUserType)"; 
      const char *sql_stmt2 = "create table if not exists TestInfo(TestID integer primary key, TestName,TestType)"; 

//   NSString *dbPathFromApp=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"UserRegInfo.sqlite"];[filemgr copyItemAtPath:dbPathFromApp toPath:databasePath error:nil]; 

      NSLog(@"Constan charcter value is %s:-",dbpath); 
      if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg) && (sqlite3_exec(database, sql_stmt2, NULL, NULL, &errMsg)!= SQLITE_OK)) 
      { 
       isSuccess = NO; 
       NSLog(@"Failed to create table"); 
      } 
      NSLog(@"Print Sqlite%d",(sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg))); 
      sqlite3_close(database); 
      return isSuccess; 
     } 
     else { 
      isSuccess = NO; 
      NSLog(@"Failed to open/create database"); 
     } 

    } 
    return isSuccess; 
} 
#pragma mark 
#pragma mark Save Data Function 

-(BOOL)saveData:(NSString*)insertSQL{ 
    const char *dbpath = [databasePath UTF8String]; 
    if (sqlite3_open(dbpath, &database) == SQLITE_OK) 
    { 

     NSLog(@" my Sqlite Query is :- %@",insertSQL); 
     const char *insert_stmt = [insertSQL UTF8String]; 
     sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL); 
     if (sqlite3_step(statement) == SQLITE_DONE) 
     { 
      return YES; 
     } 
     else 
     { 
      NSLog(@"Squlite Error Msg is %s",sqlite3_errmsg(database)); 
      return NO; 
     } 
    } 
    return NO; 
} 
+0

本答案是假設....謝謝+1 –

+0

此代碼有許多錯誤。 'saveData'方法打開數據庫,但不關閉它。 'saveData'方法準備一個語句,但沒有完成它。對'sqlite3_exec'的兩次調用沒有正確的錯誤檢查。 – rmaddy

+0

但它在我的應用上工作 –