然后添加一个实现连接mysql数据库及对数据库进行相关操作的方法的类。其实现连接mysql数据库及对数据库进行相关操作的方法的实现如下:
[objc] view plain copy//连接数据库 [objc] view plain copy-(void)connectHost:(NSString *)host connectUser:(NSString *)user connectPassWord:(NSString *)password connectName:(NSString *)name { myconnect = mysql_init(nil); //新增。设置编码(解决中文乱码问题) mysql_options(myconnect, MYSQL_SET_CHARSET_NAME, "gb2312"); myconnect = mysql_real_connect(myconnect,[host UTF8String],[user UTF8String],[password UTF8String],[name UTF8String],MYSQL_PORT,NULL,0); if(!myconnect) { PRintf("error code=%i",mysql_errno(myconnect)); return; } NSLog(@"connected to Mysql"); } [objc] view plain copy [objc] view plain copy//查询数据库中数据,参数sql既为SQL查询语句(@"select name from table1"),返回SQL语句查询到的结果,我理解的是recordArr中存放的是每组数据(每个column)中查询到的结果,而recordsArray中存放的是recordArr,即有几个column就有几个recorder。 -(NSMutableArray*)query:(NSString *)sql { if(!myconnect) { NSLog(@"Please connect first"); return nil; } NSMutableArray *recordsArray = [[[NSMutableArray alloc] init] autorelease]; mysql_query(myconnect, [sql UTF8String]); MYSQL_RES* result = mysql_store_result(myconnect); int num_rows = mysql_num_rows(result); int num_fields = mysql_num_fields(result); for(int i=0;i<num_rows;i++) { MYSQL_ROW row = mysql_fetch_row(result); NSMutableArray *recordArr = [[NSMutableArray alloc] init]; for(int j=0;j<num_fields;j++) { //最新修改,解决中文乱码问题 //中文简体编码:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingDOSChineseSimplif) //NSString* value= [NSString stringWithUTF8String:row[j]]; NSString* value= [NSString stringWithCString:row[j] encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingDOSChineseSimplif)]; [recordArr addObject:value]; } [recordsArray addObject:recordArr]; [recordArr release]; } return recordsArray; } [objc] view plain copy//断开数据库连接 [objc] view plain copy- (void)disconnect { mysql_close(myconnect); NSLog(@"Close From Mysql"); } [objc] view plain copy [objc] view plain copy//修改数据库中的数据(无返回值)。删除、增加数据都可以调用此方法。 [objc] view plain copy-(void)update:(NSString *)sql { if(myconnect) { mysql_query(myconnect, [sql UTF8String]); } else { NSLog(@"Please connect to DB first"); return; } } [objc] view plain copy//连接按钮处理方法 [objc] view plain copy-(void)btnConnectTapped:(id)sender { NSLog(@"%@",ipText.text); NSLog(@"%@",duankouTextField.text); NSLog(@"%@",nameTextField.text); NSLog(@"%@",passwordTextField.text); mysqlManager = [[MysqlManager alloc]init]; [mysqlManager connectHost:[NSString stringWithFormat:@"%@",ipText.text] connectUser:nameTextField.text connectPassword:passwordTextField.text connectName:@"wgjchm"]; } [objc] view plain copy [objc] view plain copy//查询按钮处理方法 [objc] view plain copy-(IBAction)queryButtonTapped:(id)sender { //插入数据 [objc] view plain copy[mysqlManager update:@"insert into fdgg values(5,'hc',1,80,0)"]; [objc] view plain copy//查询数据 NSMutableArray *res = [mysqlManager query:@"select name from fdgg"]; NSLog(@"%d",res.count); for(int i = 0; i < [res count]; ++i) { NSMutableArray *record = [res objectAtIndex:i]; for(int j = 0; j < [record count]; ++j) { //value既为查询到的name值 [objc] view plain copy NSString *value = [record objectAtIndex:j]; NSLog(@"value=%@",value); } [objc] view plain copy //断开连接 [mysqlManager disconnect]; } 注:如果是连本地数据库IP地址为:127.0.0.1或localhost 端口号默认为:3306(但不全是,要视情况而定)新闻热点
疑难解答