IOS 开发学习33 使用sqlite3
来源:互联网 发布:韩国电影《招待》 知乎 编辑:程序博客网 时间:2024/06/08 18:51
sqlite3 命令行简单使用
sqlite3 路径 //打开数据库路径连接
select * from sqlite_master where type=”table”; //显示所有表结构
select * from testable; //显示某张表数据
.tables //查看表
.help //查看帮助
.quit //退出
xcode使用sqlite3步骤
1.添加libsqlite3.dylib
2.头文件
//// DbUtils.h// smart//// Created by 谢厂节 on 15/5/12.// Copyright (c) 2015年 WHR. All rights reserved.//#import <Foundation/Foundation.h>#import "sqlite3.h"@interface DbUtils : NSObject{ sqlite3 *db; //声明一个sqlite3数据库}- (NSString *)filePath;//数据库文件的路径。一般在沙箱的Documents里边操作-(void)openDB;-(void)closeDB;-(NSMutableArray *)getAllTypes;@end
.m文件
//// DbUtils.m// smart//// Created by 谢厂节 on 15/5/12.// Copyright (c) 2015年 WHR. All rights reserved.//#import "DbUtils.h"#import "KMTypes.h"#import "KMContents.h"@implementation DbUtils//打开数据库的方法- (void)openDB{ ///文件是否存在 NSFileManager* fileManager = [NSFileManager defaultManager]; NSString *dbpath=[self filePath]; NSLog(@"database path:%@",dbpath); BOOL success = [fileManager fileExistsAtPath:dbpath]; if (!success) { NSString *resourcePath=[[NSBundle mainBundle]resourcePath]; //自动复制 NSString *sourceDBPath=[resourcePath stringByAppendingPathComponent:@"app.bundle/datas.sqlite"]; NSError *error; success = [fileManager copyItemAtPath:sourceDBPath toPath:dbpath error:&error]; if(!success) NSAssert1(0,@"数据库附加失败!'%@'.", [error localizedDescription]); else NSLog(@"数据库附加成功:%@",dbpath); } if (sqlite3_open([[self filePath] UTF8String], &db) != SQLITE_OK) { sqlite3_close(db); NSAssert(0, @"数据库打开失败。"); }}- (void)closeDB{ sqlite3_close(db);}//该方法用于返回数据库在Documents文件夹中的全路径信息- (NSString *)filePath{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0]; return [documentsDir stringByAppendingPathComponent:@"datas.sqlite"];}////查询数据所有类别- (NSMutableArray *)getAllTypes{ [self openDB]; NSMutableArray *array=[NSMutableArray arrayWithCapacity:6 ]; NSString *sql = @"SELECT * FROM km_types"; sqlite3_stmt *statement; if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK) { while (sqlite3_step(statement) == SQLITE_ROW) { KMTypes* k= [[KMTypes alloc]init]; int type_id = (int)sqlite3_column_int(statement,0); int parent_id = (int)sqlite3_column_int(statement,1); char *type_title = (char *)sqlite3_column_text(statement, 2); int type_order = (int)sqlite3_column_int(statement,3); int topic_count=(int)sqlite3_column_int(statement,4); NSString *type_titleStr = [[NSString alloc] initWithUTF8String:type_title]; k.type_title = type_titleStr; k.type_id=type_id; k.parent_id = parent_id; k.type_order = type_order; k.topic_count=topic_count; [array addObject:k]; } sqlite3_finalize(statement); } [self closeDB]; return array;}@end这里只实现一个简单的查询功能。
0 0
- IOS 开发学习33 使用sqlite3
- ios学习--SQLite3的使用
- iOS开发中 SQLite3的使用案例
- iOS开发—sqlite3的使用
- ios学习--sqlite3数据库的使用
- iOS: 学习笔记, 使用FMDatabase操作sqlite3
- 学习iPhone开发中 sqlite3的使用
- 学习iPhone开发中 sqlite3的使用
- 学习iPhone开发中 sqlite3的使用
- 学习iPhone开发中 sqlite3的使用
- 学习iPhone开发中 sqlite3的使用
- 学习iPhone开发中 sqlite3的使用
- 学习iPhone开发中 sqlite3的使用
- 学习iPhone开发中 sqlite3的使用
- 学习iPhone开发中 sqlite3的使用
- ios简单使用sqlite3
- ios简单使用sqlite3
- ios SQLite3 使用命令
- 白平衡
- 浅谈C++编译原理 ------ C++编译器与链接器工作原理
- C++ 数据结构、算法笔试题(1)
- 去除114网址导航
- css3中浏览器厂商前缀
- IOS 开发学习33 使用sqlite3
- 【ITOO高校云平台】——EF框架:DatabaseFirst
- 辛星浅析一次ajax的实现过程
- TCP优化几点
- VS2010 C++ MFC框架学习笔记4 - C++面向对象编程基础2
- Ext.form
- 5.4.7 Sum Root to Leaf Numbers
- Android开发:使用序列化接口Parcelable、Serializable实现Activity间传递复杂数据类型参数
- JavaScript学习第一节-JS初步及调试。