2012-05-02 130 views
14

我已經創建了一個數據庫類並打包了一些方法。但是,一旦建立項目...架構i386的未定義符號:「_sqlite3_open」,引用自:錯誤

Undefined symbols for architecture i386: 
"_sqlite3_open", referenced from: 
    -[MyDataBase openOrCreateDatabase:] in MyDataBase.o 
"_sqlite3_exec", referenced from: 
    -[MyDataBase createTable:] in MyDataBase.o 
    -[MyDataBase InsertTable:] in MyDataBase.o 
    -[MyDataBase UpdataTable:] in MyDataBase.o 
    -[MyDataBase querryTableByCallBack:] in MyDataBase.o 
"_sqlite3_close", referenced from: 
    -[MyDataBase closeDatabase] in MyDataBase.o 
"_sqlite3_get_table", referenced from: 
    -[MyDataBase querryTable:] in MyDataBase.o 
ld: symbol(s) not found for architecture i386 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

這裏有一些關鍵的方法:

創建數據庫:

-(BOOL)openOrCreateDatabase:(NSString*)dbName 
{ 
self.m_dbName = dbName; 
NSArray *path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
NSString *documentsDirectory = [path objectAtIndex:0]; 
if(sqlite3_open([[documentsDirectory stringByAppendingPathComponent:dbName] UTF8String],&m_sql) !=SQLITE_OK) 
{ 
NSLog(@"創建數據庫失敗"); 
return NO; 
} 
return YES; 
} 

創建表:

-(BOOL)createTable:(NSString*)sqlCreateTable 
{ 
if (![self openOrCreateDatabase:self.m_dbName]) { 
    return NO; 
} 
char *errorMsg; 
if (sqlite3_exec (self.m_sql, [sqlCreateTable UTF8String],NULL, NULL, &errorMsg) != 
SQLITE_OK) 
{ 
    NSLog(@"創建數據表失敗:%s",errorMsg); 
    return NO; 
} 
[self closeDatabase]; 
return YES; 
} 

關閉數據庫:

-(void)closeDatabase 
{ 
sqlite3_close(self.m_sql); 
} 

//insert 

-(BOOL)InsertTable:(NSString*)sqlInsert 
{ 
if (![self openOrCreateDatabase:self.m_dbName]) { 
    return NO; 
} 
char* errorMsg = NULL; 
if(sqlite3_exec(self.m_sql, [sqlInsert UTF8String],0, NULL, &errorMsg) ==SQLITE_OK) 
{ [self closeDatabase]; 
    return YES;} 
else { 
    printf("更新表失敗:%s",errorMsg); 
    [self closeDatabase]; 
    return NO; 
} 
return YES; 
} 

更新表:

-(BOOL)UpdataTable:(NSString*)sqlUpdata{ 
if (![self openOrCreateDatabase:self.m_dbName]) { 
    return NO; 
} 
char *errorMsg; 
if (sqlite3_exec (self.m_sql, [sqlUpdata UTF8String],0, NULL, &errorMsg) !=SQLITE_OK) 
{ 
    [self closeDatabase]; 
    return YES; 
}else { 
    return NO; 
} 

return YES; 
} 

選擇記錄:

-(NSArray*)querryTable:(NSString*)sqlQuerry 
{ 
if (![self openOrCreateDatabase:self.m_dbName]) { 
    return nil; 
} 
int row = 0; 
int column = 0; 
char* errorMsg = NULL; 
char** dbResult = NULL; 
NSMutableArray* array = [[NSMutableArray alloc] init]; 
if(sqlite3_get_table(m_sql,[sqlQuerry UTF8String], &dbResult, &row,&column,&errorMsg) == SQLITE_OK) 
{ 
    if (0 == row) { 
     [self closeDatabase]; 
     return nil; 
    } 
    int index = column; 
    for(int i =0; i < row ; i++) {  
     NSMutableDictionary* dic = [[NSMutableDictionary alloc] init]; 
     for(int j =0 ; j < column; j++) { 
      if (dbResult[index]) { 
       NSString* value = [[NSString alloc] initWithUTF8String:dbResult[index]]; 
       NSString* key = [[NSString alloc] initWithUTF8String:dbResult[j]]; 
       [dic setObject:value forKey:key]; 
       [value release]; 
       [key release]; 
      } 
      index ++; 
     } 
     [array addObject:dic]; 
     [dic release]; 
    } 
    }else { 
    printf("%s",errorMsg); 
    [self closeDatabase]; 
    return nil; 
    } 
    [self closeDatabase]; 
    return [array autorelease]; 
} 
+1

你鏈接了sqlite3.dylib嗎? –

回答

相關問題