2013-03-05 57 views
-2

我目前正在開發一個與sqlite數據庫交互的項目。問題是每次我想連接到數據庫時,我必須打開並準備數據庫。所以我想讓這些步驟更具通用性,我決定創建一個能爲我完成這些步驟的普通課程。在目標中傳遞引用參數C

+(void)openAndPrepareDatabase:(sqlite3 *)db andStatement:(sqlite3_stmt *)statement andSql:(const char *)sql 
{ 
@try 
{ 
    if(!sqlite3_open([[self getDatabasePath] UTF8String], &db) == SQLITE_OK) 
    { 
     NSException *myException = [NSException exceptionWithName:@"Can't open database" reason:@"Can't open database" userInfo:Nil]; 

     @throw myException; 
    } 

    if(!sqlite3_prepare(db, sql, -1, &statement, NULL) == SQLITE_OK) 
    { 
     NSException *myException = [NSException exceptionWithName:@"Can't prepare database" reason:@"Can't prepare database" userInfo:Nil]; 

     @throw myException; 
    } 
} 
@catch (NSException *exception) 
{ 
    @throw exception; 
} 
} 


+(void)openAndPrepareDatabaseV2:(sqlite3 *)db andStatement:(sqlite3_stmt *)statement andSql:(const char *)sql 
{ 
@try 
{ 
    if(!sqlite3_open([[self getDatabasePath] UTF8String], &db) == SQLITE_OK) 
    { 
     NSException *myException = [NSException exceptionWithName:@"Can't open database" reason:@"Can't open database" userInfo:Nil]; 

     @throw myException; 
    } 

    if(!sqlite3_prepare_v2(db, sql, -1, &statement, NULL) == SQLITE_OK) 
    { 
     NSException *myException = [NSException exceptionWithName:@"Can't prepare database" reason:@"Can't prepare database" userInfo:Nil]; 

     @throw myException; 
    } 
} 
@catch (NSException *exception) 
{ 
    @throw exception; 
} 
} 

但是當我嘗試調用它在我的對象,即:

[Common openAndPrepareDatabase:&db andStatement:&statement andSql:sql]; 

我得到了一個警告:

"Incompatible pointer types sending 'sqlite3_stmt **' (aka 'struct sqlite3_stmt **') to parameter of type 'sqlite3_stmt *' (aka 'struct sqlite3_stmt *'); remove &" 

有誰知道我的問題的解決方案?

+0

你爲什麼不讀的複製粘貼前的錯誤信息順便說一下? – 2013-03-05 06:40:06

回答

1

有沒有人知道我的問題的解決方案?

編譯器有只顯示你一個。

「不兼容的指針......等等等等; 刪除&

(重點煤礦)

+0

刪除「&」後,那些代碼的行爲就像當我打開並在我的對象上準備數據庫(而不是通過普通的類)? – 2013-03-05 06:41:23

+0

@DoanCuong我不明白這個問題,你不明白指針。 – 2013-03-05 06:42:51

+0

我想知道,在將db和statement參數傳遞給這些方法(並且當然除去「&」)之後,我可以使用它們來執行進一步的過程,如綁定sql參數並在我的對象上執行查詢 – 2013-03-05 06:55:13