ios - tutorial - swift database programming



Undefined symbols for architecture i386: “_sqlite3_open”, referenced from: error (1)

I have create a database class and packaged some methods. However, once build the project...

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)

here are some critical methods:

create the database:

-(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; 
}

create tables:

-(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;  
}  

close the database:

-(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;  
}  

update tables:

-(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;  
}  

select records:

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

You need to add the libsqlite library into linking process. This can be done in the Project Settings, see https://.com/a/7623043/1091195.





database