//document文件夹路径
#definePATH_AT_Document(name) [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:name]//
temp文件夹路径
#definePATH_AT_Tmp(name) [NSTemporaryDirectory() stringByAppendingPathComponent:name]
//cache文件夹路径
#definePATH_AT_Cache(name) [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:name]
//Libary文件夹路径
#definePATH_AT_Library(name) [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:name]
1、Plist
NSString*docuPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;
NSString*plistPath=[docuPath stringByAppendingPathComponent:@"hooyking.plist"];
2、NSUserDefaults
//存
NSUserDefaults*ud=[NSUserDefaults standardUserDefaults];
[ud setObject:self.textField.text forKey:@"userDefaultKey"];
[ud synchronize];
//取
NSString*text=[ud objectForKey:userDefaultKeyStr];
3.SQLite
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
NSLog(@"documentPath:%@",path);
NSString *dbpath = [path stringByAppendingPathComponent:@"hooyking.db"];
FMDatabase *db = [FMDatabase databaseWithPath:dbpath];
self.myDb = db;
- (void)createTable {
if ([_myDb open]) {
//这儿创建了一个列有 name|sex|age|nickname|phoneNum|nativePlace|photo 的名字为personTable的表
BOOL result = [_myDb executeUpdate:@"create table if not exists personTable (name text, sex integer, age integer, nickname text, phoneNum text, nativePlace text, photo blob)"];
if (result) {
NSLog(@"创建表成功");
}
else {
NSLog(@"创建表失败");
}
[_myDb close];
}
}
- (void)insertData {
if ([_myDb open]) {
BOOL result = [_myDb executeUpdate:@"insert into personTable (name, sex, age, nickname, phoneNum, nativePlace, photo) values (?,?,?,?,?,?,?)",@"JDX",[NSNumber numberWithInteger:1],[NSNumber numberWithInteger:18], @"hooyking", [NSNumber numberWithInteger:13888888888],@"sichuan",[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"picture" ofType:@"jpg"]]];
if (result) {
NSLog(@"添加数据成功");
}
else {
NSLog(@"添加数据失败");
}
[_myDb close];
}
}
- (void)deleteData {
//删除这个表里nickname为hooyking的所有数据,列没有删除,依然存在
if ([_myDb open]) {
BOOL result = [_myDb executeUpdate:@"delete from personTable where nickname = ?",@"hooyking"];
if (result) {
NSLog(@"删除数据成功");
}
else {
NSLog(@"删除数据失败");
}
[_myDb close];
}
}
- (void)updateData {
if ([_myDb open]) {
BOOL result = [_myDb executeUpdate:@"update personTable set age = ? where nickname = ?",[NSNumber numberWithInteger:25],@"hooyking"];
if (result) {
NSLog(@"修改数据成功");
}
else {
NSLog(@"修改数据失败");
}
[_myDb close];
}
}
- (void)selectData {
if ([_myDb open]) {
//查询多条数据
FMResultSet *res = [_myDb executeQuery:@"select name, age from personTable"];
while ([res next]) {
NSString *name = [res stringForColumn:@"name"];
NSInteger age = [res intForColumn:@"age"];
NSLog(@"姓名:%@----年龄:%ld",name,age);
}
//查询一条数据
NSLog(@"年龄为25的人:%@",[_myDb stringForQuery:@"select name from personTable where age = ?",@25]);
[_myDb close];
}
}
网友评论