安装环境:
苦逼挣扎了几天,一直没有充足的时间来研究mysql
Mysql服务安装
Snip20170616_51.png
下载地址https://dev.mysql.com/downloads/mysql/
Snip20170616_52.png
- mysql下载地址建议选择dmg安装包,不要选择压缩包,因为压缩包需要自己手动配置,比较的麻烦
- 下载好之后,安装,下一步>下一步>完成
- 在安装之后,请一定截图,会弹出提示,这里是个坑(找了好久的错误)
- 安装之后的mysql服务默认密码随机的,下图密码就是 ,sVPCpo&f1Mh
- 一直以为都是root和123456为默认密码,排除好久的错位都是提示
access denied for user root @loca...
- 安装之后在终端输入
mysql --version
Last login: Fri Jun 16 11:19:19 on ttys000
jinliangqideMac-mini:~ jinliangqi$ mysql --version
mysql Ver 14.14 Distrib 5.7.18, for macos10.12 (x86_64) using EditLine wrapper
jinliangqideMac-mini:~ jinliangqi$
B2EC8B3B-617C-4F45-98F5-4D6233A7594B.png
Snip20170616_54.png
Snip20170616_53.png
Navicat安装
- 安装mysql之后,后面就简单多了,同样的就最后一个工具,客户端Navicat
- 下载地址https://www.waitsun.com/navicat-premium-11-2-18.html
Snip20170616_55.png
Snip20170616_56.png
测试环境
Snip20170616_57.png
密码复杂了怎么修改?
步骤
- 在终端输入:mysql -uroot -p
- 这时候提示你输入密码
,sVPCpo&f1Mh
- 进入/usr/local/mysql/include文件夹
Snip20170616_58.png
- 输入以下三条命令
mysql>SET PASSWORD = PASSWORD('123456');
mysql>ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
mysql>flush privileges;
可以用123456密码去navicat登陆了。
Xcode编写代码
- 关于怎么建立数据库,怎么建表,这里也不作描述,百度上搜索一大堆。我这里创建了一个叫"text"的数据库,其中只有一个表。表包括id,name,sex,age,tel几个字段,如图所示:
Snip20170616_60.png
Snip20170616_59.png
- sql语句
create table students
(
id int unsigned not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "-"
);
- Xcode项目创建
Snip20170616_61.png由于,mysql驱动文件放在,这个目录下:/usr/local/mysql/include,所以需要在你的项目中配置头文件搜索路径,操作如图:
Snip20170616_62.png同时,你需要告诉编译器编译参数,在other linker flags添加:-L/usr/local/mysql/lib -lmysqlclient,如图所示:
代码编写
- 项目配置好了,接下来上代码:
- 新建两个类DBManager和Person,用于管理数据库操作,代码如下:
Person类
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,copy) NSString *ID;
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *sex;
@property (nonatomic,copy) NSString *age;
@property (nonatomic,copy) NSString *tel;
@end
#import "Person.h"
@implementation Person
@end
DBManager类
#import <Foundation/Foundation.h>
@class Person;
@interface DBManager : NSObject
+ (DBManager *)sharedManager;
- (NSArray *)getAllPersons;
- (void)addPerson:(Person *)p;
- (void)deletaPerson:(Person *)p;
@end
#import "DBManager.h"
#import "Person.h"
#import "mysql.h"
/* 本地的主机名字默认是localhost */
static const char *host_name = "localhost";
/* 本地mysql数据库用户名默认root */
static const char *user_name = "root";
/* 本地mysql链接的密码 */
static const char *password = "123456";
/* 创建数据库 “库”的名字 */
static const char *db_name = "text";
@interface DBManager(){
MYSQL *_myconnect;
}
@end
@implementation DBManager
+(DBManager *)sharedManager {
static DBManager *sharedSingleton = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^(void) {
sharedSingleton = [[self alloc] init];
});
return sharedSingleton;
}
- (instancetype)init {
if (self = [super init]) {
//初始化工作
_myconnect = mysql_init(_myconnect);
_myconnect = mysql_real_connect(_myconnect,host_name,user_name,password,db_name,MYSQL_PORT,NULL,0);
mysql_set_character_set(_myconnect, "utf8");
if (_myconnect != NULL) {
NSLog(@"连接成功");
} else {
NSLog(@"连接失败");
}
}
return self;
}
- (NSArray *)getAllPersons {
if (_myconnect == NULL) {
return nil;
}
//status=0,表示query成功
int status = mysql_query(_myconnect, "select * from students");
if (status != 0) {
NSLog(@"查询数据失败");
}
//拿到结果集
MYSQL_RES *result = mysql_store_result(_myconnect);
//一共查询到多少行
long long rows =result->row_count;
//创建数组保存数据
NSMutableArray *rowArray = [NSMutableArray arrayWithCapacity:rows];
//字段个数
unsigned int fieldCount = mysql_field_count(_myconnect);
//用于保存一行的结果,其实就是一个字符串数组
MYSQL_ROW col;
for (int row = 0; row < rows; row++) {
NSMutableString *strM = [NSMutableString string];
if((col = mysql_fetch_row(result))){
for(int i = 0; i < fieldCount; i++){
//这里为了方便看结果,只返回字符串,而不是返回模型数组
[strM appendString:[NSString stringWithUTF8String:col[i]]]; [strM appendString:@" "];
}
}
[rowArray addObject:strM];
}
return rowArray;
}
- (void)addPerson:(Person *)p {
NSString *sql = [NSString stringWithFormat:@"insert into students (name, sex, age, tel) values('%@', '%@', '%@','%@')",p.name,p.sex,p.age,p.tel];
int status = mysql_query(_myconnect, [sql UTF8String]);
if (status == 0) {
NSLog(@"插入数据成功");
} else {
NSLog(@"插入数据失败");
}
}
- (void)deletaPerson:(Person *)p {
NSString *sql = [NSString stringWithFormat:@"delete from students where id=%@",p.ID];
int status = mysql_query(_myconnect, [sql UTF8String]);
if (status == 0) {
NSLog(@"删除数据成功");
} else {
NSLog(@"删除数据失败");
}
}
实现效果
#import "ViewController.h"
#import "DBManager.h"
#import "Person.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self test];
// Do any additional setup after loading the view.
}
- (void) test {
DBManager *mgr = [DBManager sharedManager];
//查询数据
NSArray *array = [mgr getAllPersons];
for (int i = 0; i < array.count; i++) {
NSLog(@"%@",array[i]);
}
//添加数据
Person *p = [[Person alloc] init];
p.name = @"Mrs Wang";
p.age = @"30";
p.sex = @"man";
p.tel = @"11111111111";
// [mgr addPerson:p];
//删除数据
// p.ID = @"1";
// [mgr deletaPerson:p];
}
效果如图
Snip20170616_63.png
Snip20170616_64.png
Snip20170616_65.png
网友评论