iOS APP与数据库交互

图片1.jpg

应用程序直接去连接数据库,并对数据库进行增删改查
准备工作:
1.连接数据库的第三方工具:https://github.com/oleghnidets/OHMySQL
2.数据库:数据库地址、数据库名称、端口号、用户名、用户密码
第一步:连接数据库
连接数据库的测试参数:
用户名:wangsan
密码:123456
服务器域名:ceshi.cn
数据库名字:ceshi
端口号:10000

     //初始化一个连接用户对象
    OHMySQLUser *auser = [[OHMySQLUser alloc] initWithUserName:@"wangsan"
                                                      password:@"123456"
                                                    serverName:@"ceshi.cn"
                                                        dbName:@"ceshi"
                                                          port:10000
                                                        socket:@"ceshi.cn"];
    OHMySQLStoreCoordinator *coordinator = [[OHMySQLStoreCoordinator alloc] initWithUser:auser];

     //连接
    [coordinator connect];

//判断是否连接成功我们可以ping一下

 if (coordinator.isConnected == YES) {
        switch ([coordinator pingMySQL]) {
            case OHResultErrorTypeNone:
            {
               //数据库连接成功
            }
                break;
            case OHResultErrorTypeLost:
            {
                 //与服务器的连接丢失
            }
                break;
            case OHResultErrorTypeGone:
            {
             //MySQL服务器已经消失了。
            }
                break;
            case OHResultErrorTypeUnknown:
            {
              //未知错误
            }
                break;
                
            default:
                break;
        }
          }else{
             
          }
 

判断连接成功后获取对象并将对象存起来,这样就不用每次查询数据连接一次

 //获取上下文
OHMySQLQueryContext *queryContext = [OHMySQLQueryContext new];
queryContext.storeCoordinator = coordinator;
[OHMySQLManager sharedManager].mainQueryContext = queryContext;

对数据库操作的使用方法:
增:在表userTable新增一条数据,我们将要传的值以NSDictionary形式传输

NSDictionary *dic = @{@"addTime" :nowTime,
                          @"name": self.nameView.textFile.text,
                          @"phoneNumber": self.phoneView.textFile.text,
                          @"password": self.passwordView.textFile.text };
 OHMySQLQueryRequest *query = [OHMySQLQueryRequestFactory INSERT:@"userTable" set:dic];
 NSError * error = nil ;
 NSArray * tasks = [[OHMySQLManager sharedManager].mainQueryContext executeQueryRequestAndFetchResult: query error:&error];
    NSLog(@"%@, %@", tasks,error.description);
 if (!error) {
        NSLog(@"添加成功");
  }else{
    NSLog(@"%@",error.description);
 }

删:删除userTable表中name叫王三的人,condition表示条件等于数据库while

OHMySQLQueryRequest *query = [OHMySQLQueryRequestFactory DELETE:@"userTable" condition:@"name='王三'"];
NSError error = nil
NSArray * tasks = [[OHMySQLManager sharedManager].mainQueryContext  executeQueryRequestAndFetchResult:query error:&error];
  NSLog(@"%@, %@", tasks,error.description);
if (!error) {
        NSLog(@"删除成功");
}else{
    NSLog(@"%@",error.description);
}

改:修改表userTable中名字叫王三的手机号

//要修改的手机号以键值对的形式(phoneNumber是数据库存储的字段)
NSDictionary *dic = @{@"phoneNumber" : @“18862033568”}; 
//条件是名字叫王三
NSString *sqltStr1 = [NSString stringWithFormat:@"name = '%@'", @“王三”];
 OHMySQLQueryRequest *query = [OHMySQLQueryRequestFactory UPDATE:@"userTable" set:dic condition:sqltStr1];
NSError error = nil
NSArray * tasks = [[OHMySQLManager sharedManager].mainQueryContext  executeQueryRequestAndFetchResult:query error:&error];
  NSLog(@"%@, %@", tasks,error.description);
    if (!error) {
        NSLog(@"修改成功");
}else{
    NSLog(@"%@",error.description);
}

查:查询表userTable中名字叫'王五',手机号是'18895322310'的人,并以ID进行降序排列

NSString *sqltStr =@"name = '王五' AND phoneNumber = '18895322310'";
//orderBy:依据什么排序
//ascending:是否为升序,YES为升序,NO为降序
OHMySQLQueryRequest *query = [OHMySQLQueryRequestFactory SELECT:@"userTable" condition:sqltStr orderBy:@[@"id"] ascending:YES];
NSError error = nil
NSArray * tasks = [[OHMySQLManager sharedManager].mainQueryContext  executeQueryRequestAndFetchResult:query error:&error];
  NSLog(@"%@, %@", tasks,error.description);
    if (!error) {
        NSLog(@"查询成功");
        NSLog(@"%@", tasks);
}else{
    NSLog(@"%@",error.description);
}

查的扩展:
查:查询表userTable中名字叫'王五',手机号是'18895322310'的人的password,并以ID进行降序排列(注释:这个方法OHMySQL里面没有是我自己添加的,根据自己需要查询内容可以自己添加查询方法)

NSString *sqltStr =@"name = '王五' AND phoneNumber = '18895322310'";
//orderBy:依据什么排序
//ascending:是否为升序,YES为升序,NO为降序
//field:只查询password
OHMySQLQueryRequest *query = [OHMySQLQueryRequestFactory SELECT:@"userTable" field:@"password" condition:sqltStr orderBy:@[@"id"] ascending:YES];
NSError error = nil
NSArray * tasks = [[OHMySQLManager sharedManager].mainQueryContext  executeQueryRequestAndFetchResult:query error:&error];
  NSLog(@"%@, %@", tasks,error.description);
    if (!error) {
        NSLog(@"查询成功");
        NSLog(@"%@", tasks);
}else{
    NSLog(@"%@",error.description);
}

后续将上Demo

问题:手动导入OHMySQL框架时可能报错(错误截图如下),可能是缺少了libc++,所以要添加libc++,设置->target-->build phase->Link Binary With Libraries->添加 搜索libc++,选择libc++.tbd文件。


错误截图.png

你可能感兴趣的:(iOS APP与数据库交互)