14

我创建了一个数据库类并打包了一些方法。但是,一旦构建项目......

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];  
}  
4

1 回答 1

37

您需要将 libsqlite 库添加到链接过程中。这可以在项目设置中完成,请参阅https://stackoverflow.com/a/7623043/1091195

于 2012-05-02T11:34:02.887 回答