iOS: 学习笔记, 使用FMDatabase操作sqlite3
来源:互联网 发布:天知地知你知我知 编辑:程序博客网 时间:2024/05/21 11:13
使用FMDatabase读写sqlite3数据库非常方便和简单.
//// main.m// iOSDemo0602_sqlite3//// Created by yao_yu on 14-6-2.// Copyright (c) 2014年 yao_yu. All rights reserved.//#import <UIKit/UIKit.h>#import "FMDatabase.h"void test01();BOOL isfile(NSString *path);BOOL isdir(NSString *path);int main(int argc, char * argv[]){ @autoreleasepool { test01(); } return 0;}void test01(){ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); NSString *docpath = [paths objectAtIndex:0]; NSString *dbpath = [docpath stringByAppendingPathComponent:@"YY/Data2"];// NSLog(@"%@", dbpath); if (!isdir(dbpath)) { NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager createDirectoryAtPath:dbpath withIntermediateDirectories:YES attributes:nil error:nil]; } NSString *dbfile = [dbpath stringByAppendingPathComponent:@"data.db"]; NSLog(@"%@", dbfile); //初始化 FMDatabase *db = [FMDatabase databaseWithPath:dbfile]; //打开数据库 [db open]; //删除表 [db executeUpdate:@"drop table persons"]; //添加表 [db executeUpdate:@"create table persons(id, name)"]; //删除原有数据 [db executeUpdate:@"delete from persons"]; //使用事务添加数据 [db beginTransaction]; for(int i=0; i< 10000; i++) { [db executeUpdate:@"insert into persons values(?,?)", [NSString stringWithFormat:@"%d", i], [NSString stringWithFormat:@"第%d", i]]; } [db commit]; //数据查询 FMResultSet *cursor = [db executeQuery:@"select * from persons"]; int nCols = cursor.columnCount; while ([cursor next]) { for (int i=0; i<nCols; i++) { printf("%s ", [[cursor stringForColumnIndex:i] UTF8String]); } printf("\n"); } cursor = nil; //关闭数据库 [db close]; //删除数据库测试文件 [[NSFileManager defaultManager] removeItemAtPath:dbfile error:nil]; }BOOL isfile(NSString *path){ NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL isdir = NO; if ([fileManager fileExistsAtPath:path isDirectory: &isdir]) { return isdir == NO; } return NO;}BOOL isdir(NSString *path){ NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL isdir = NO; if ([fileManager fileExistsAtPath:path isDirectory: &isdir]) { return isdir == YES; } return NO;}
0 0
- iOS: 学习笔记, 使用FMDatabase操作sqlite3
- [iOS学习笔记] 通过FMDataBase使用sqlite数据库
- IOS:FMDatabase的使用
- iOS开发 - FMDatabase 使用
- iOS FMDataBase split 使用
- iOS 修改FMdatabase,不使用FMDatabaseQueue支持多线程数据库操作
- [iOS进阶]FMDataBase 学习小结
- ios学习--SQLite3的使用
- Object-C使用笔记: FMDatabase使用
- 在iOS项目中使用FMDataBase
- IOS数据库操作SQLite3使用详解
- IOS数据库操作SQLite3使用详解
- IOS数据库操作SQLite3使用详解
- IOS数据库操作SQLite3使用详解
- IOS数据库操作SQLite3使用详解
- IOS中使用FMDB操作sqlite3
- IOS数据库操作SQLite3使用详解
- IOS数据库操作SQLite3使用详解
- CSDN举办开源夏令营活动 5000元奖金激励学生参与开源
- 我的敏捷观
- c++动态内存分配
- WINDOWS下安装MEMCACHE
- swift 雨燕 入门教程
- iOS: 学习笔记, 使用FMDatabase操作sqlite3
- 基于Servlet、JSP、JDBC、MySQL的一个简单的用户注册模块(附完整源码)
- 安卓实训第十一天--DOM、SAX解析XML
- erlang---启动参数学习/研究
- vim命令
- C/C++中容易造成内存溢出的函数
- 通过loop方式挂载raw格式镜像
- 区别getReadableDatabase()与getWritableDatabase()
- Android进阶之大话设计模式