数据库Sqlite-(Xcode7,ios9,objective-C)
来源:互联网 发布:自己的社会网络 编辑:程序博客网 时间:2024/05/17 02:23
首先,纪录一下学习的注意点:获取地址的方式和之前的版本其实没有变化,但是要注意一点
NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docsDir = [dirPath objectAtIndex:0];
NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); NSString *docsDir = [dirPath objectAtIndex:0];这两段代码只差了其中一个很相似的参数,一开始的时候一直没搞懂为什么路径从之前的/documents变成了/documentation,这是很大差异的,因为暂时没有从模拟器的沙箱中找到这样一个路径,所以刚开始的时候也许会没整理好导致错误。
另外基本的操作:(增、查)剩下的删、改其实没有其他 区别了就是语句的问题了,注意bug主要会出现在sql语句的错误中,其他么就没什么注意点了。
- (IBAction)save:(id)sender { sqlite3_stmt *statement; const char *dbPath = [databasePath UTF8String]; if (sqlite3_open(dbPath, &database)==SQLITE_OK) { if ([stuid.text isEqualToString:@""]) { UIAlertView * av = [[UIAlertView alloc] initWithTitle:@"msg" message:@"Sorry! id nil" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:nil, nil]; [av show]; }else { NSString * insertSQL = [NSString stringWithFormat:@"INSERT INTO INFO (num,classname,name) VALUES(\"%@\",\"%@\",\"%@\")",stuid.text,classname.text,name.text]; const char *insertStatement = [insertSQL UTF8String]; NSLog(@"%@",insertSQL); sqlite3_prepare_v2(database, insertStatement, -1, &statement, NULL); if (sqlite3_step(statement) == SQLITE_DONE) { stuid.text=@""; classname.text=@""; name.text=@""; UIAlertView * av = [[UIAlertView alloc] initWithTitle:@"msg" message:@"YES!Saved" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:nil, nil]; [av show]; } else { UIAlertView * av = [[UIAlertView alloc] initWithTitle:@"msg" message:@"Sorry!Saved failed" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:nil, nil]; [av show]; } sqlite3_finalize(statement); sqlite3_close(database); } }else{NSLog(@"failed in Saving!");}}- (IBAction)query:(id)sender { const char *dbpath = [databasePath UTF8String]; sqlite3_stmt *statement; NSLog(@"query"); if(sqlite3_open(dbpath, &database)==SQLITE_OK){ NSString *querySQL = [NSString stringWithFormat:@"SELECT classname,name from info where num=\"%@\"",stuid.text]; const char *queryStatement = [querySQL UTF8String]; if (sqlite3_prepare_v2(database, queryStatement, -1, &statement, NULL)==SQLITE_OK) { if (sqlite3_step(statement)==SQLITE_ROW) { NSString *classnameField = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_text(statement, 0)]; classname.text = classnameField; NSString *nameField = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_text(statement, 1)]; name.text = nameField; UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"we" message:@"query good" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *one =[UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:nil]; [controller addAction:one]; [self presentViewController:controller animated:YES completion:nil]; }else{ NSLog(@"NONONONONO"); } sqlite3_finalize(statement); }else{NSLog(@"tag1");} sqlite3_close(database); }}- (IBAction)clear:(id)sender { stuid.text = @""; classname.text = @""; name.text = @"";}
0 0
- 数据库Sqlite-(Xcode7,ios9,objective-C)
- Objective-C SQLite数据库封装FMDB的介绍
- Objective-C SQLite数据库封装FMDB的介绍
- Objective-C SQLite数据库封装FMDB的介绍
- Objective-c通过FMDB操作SQLite数据库的步骤
- XCode7.0 Objective-C 三大新特性
- Xcode7 ios9 集成 融云
- iOS问题(iOS9 + Xcode7)
- iOS9 Xcode7 https解决方法
- Objective-c简单操作SQLite
- xcode7,ios9 部分兼容设置
- IOS9之Xcode7允许Http
- xcode7,ios9 部分兼容设置
- UIPickView 的高度 iOS9 Xcode7
- xcode7编译ios9 报错
- UIPickView 的高度 iOS9 Xcode7
- Xcode7 iOS9 大坑知多少
- Xcode7\iOS9添加启动页
- mongodb查询的语法(大于,小于,大于或等于,小于或等于等等)
- Linux 硬链接与软链接(符号链接)
- 关于json解析
- ARM GPU 架构简介
- 链表的相交以及链表的循环的判断以及查找
- 数据库Sqlite-(Xcode7,ios9,objective-C)
- 求n以内的素数个数问题
- xcode7 launchImage设置启动图无效问题
- AVL树的简单实现
- soap使用实例
- android fragment hidenChanged方法分析
- 接入支付宝时遇到的问题
- spark helloworld (wordCount实现并按照词频排序)
- Lucene创建索引与搜索索引试手