Objective-C 使用MySQL数据库的教程

首先要确定系统里已经安装了mysql,我的mysql安装在/usr/local/mysql
新建一个工程,名字叫 mysqlTest
修 改工程的设置,修改Orther Link Flags,加入-L/usr/local/mysql/lib -lmysqlclient -lz -lm -lmygcc,如图 
Objective-C 使用MySQL数据库的教程_第1张图片

这个"-L/usr/local/mysql/lib -lmysqlclient -lz -lm -lmygcc" 是通过执行 /usr/local/mysql/bin/mysql_config --libs得到的,不同版本的mysql会有不通的输出
修改头文件和库文件的路径Header Search Path和Library Search Path,如图 
Objective-C 使用MySQL数据库的教程_第2张图片


新建一个类,名字叫mysqlDB,扩展名修改成.mm,不是.m, 这个非常重要,如果是.m的文件,不能在文件内部写c的代码,使用mysql api,其实是用c写的
mysqlDB.h
  1. #import
  2. #import
  3.  
  4. @interface mysqlDB : NSObject {
  5.     MYSQL *myconnect;
  6. }
  7. // 连接mysql
  8. -(void)connectHost:(NSString *)host connectUser:(NSString *)user connectPassword:(NSString *)password connectName:(NSString *)name;
  9. // 关闭连接
  10. -(void)disconnect;
  11. // 执行query
  12. -(int)query:(NSString *)sql;
  13. // 返回结果集
  14. -(MYSQL_RES *)store_result;
  15. // fetch结果集
  16. -(NSMutableArray *)fetchRow:(MYSQL_RES *)result;
  17. -(NSMutableArray *)fetchAllRows:(MYSQL_RES *)result;
  18. @end


mysqlDB.mm

  1. #import "mysqlDB.h"
  2.  
  3.  
  4. @implementation mysqlDB
  5. -(id)init
  6. {
  7.     [super init];
  8.     return self;
  9. }
  10.  
  11. -(void)connectHost:(NSString *)host connectUser:(NSString *)user connectPassword:(NSString *)password connectName:(NSString *)name
  12. {
  13.     myconnect = mysql_init(myconnect);
  14.     myconnect = mysql_real_connect(myconnect,[host UTF8String],[user UTF8String],[password UTF8String],[name UTF8String],MYSQL_PORT,NULL,0);
  15. }
  16.  
  17. -(void)disconnect
  18. {
  19.     mysql_close(myconnect);
  20.     NSLog(@"Close From Mysql");
  21. }
  22.  
  23. -(int)query:(NSString *)sql
  24. {
  25.     return mysql_query(myconnect, [sql UTF8String]);
  26. }
  27.  
  28. -(MYSQL_RES *)store_result
  29. {
  30.     return mysql_store_result(myconnect);
  31. }
  32.  
  33. -(NSMutableArray *)fetchRow:(MYSQL_RES *)result
  34. {
  35.     MYSQL_ROW row;
  36.     NSMutableArray *rowArray = [[NSMutableArray alloc] init];
  37.     int fieldCount = mysql_field_count(myconnect);
  38.     if(row = mysql_fetch_row(result))
  39.     {
  40.         for(int i = 0; i < fieldCount; i++)
  41.         {
  42.             /*
  43.             NSString *tmpStr = [[NSString alloc] initWithUTF8String:row[i]];
  44.             [rowArray addObject:tmpStr];
  45.             [tmpStr autorelease];
  46.              */
  47.             [rowArray addObject:[[NSString stringWithUTF8String:row[i]] autorelease]];
  48.         }
  49.     }
  50.     row = NULL;
  51.     
  52.     [rowArray autorelease];
  53.     return rowArray;
  54. }
  55.  
  56. -(NSMutableArray *)fetchAllRows:(MYSQL_RES *)result
  57. {
  58.     NSMutableArray *resultArray = [[NSMutableArray alloc] init];
  59.     int rowCount = mysql_num_rows(result);
  60.     for(int i = 0; i < rowCount; i++)
  61.     {
  62.         //printf("%d\n",i);
  63.         [resultArray addObject:[self fetchRow:result]];
  64.     }
  65.     [resultArray autorelease];
  66.     return resultArray;
  67. }
  68. @end


在 mysqlTest.m文件中加入

  1.     mysqlDB *newMysql = [[mysqlDB alloc] init];
  2.     [newMysql connectHost:@"localhost" connectUser:@"root" connectPassword:@"" connectName:@"mysql"];
  3.     [newMysql query:@"SELECT * FROM user"];
  4.     
  5.     NSMutableArray *result = [newMysql fetchAllRows:[newMysql store_result]];
  6.     //NSLog(@"%d",[result count]);
  7.     for(int i = 0; i < [result count]; i++)
  8.     {
  9.         //NSMutableArray *row = [result objectAtIndex:i];
  10.         NSMutableArray *row = [[NSMutableArray alloc] initWithArray:[result objectAtIndex:i]];
  11.         NSLog(@"\n%d:\n\t host:%@ \n\t user:%@",(i+1),[row objectAtIndex:0],[row objectAtIndex:1]);
  12.         [row release];
  13.     }
  14.     [result release];
  15.     [newMysql disconnect];


编译执行,就看到结果了

Objective-C 使用MySQL数据库的教程_第3张图片

你可能感兴趣的:(iOS,mysql,数据库,string,query,user,search)