原文地址:http://blog.csdn.net/wally1985/article/details/21241431
http://www.jianshu.com/p/e2dbd69b6db2
- (void)viewDidLoad {
[super viewDidLoad];
//创数据库
NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];
NSString *database_path = [doc stringByAppendingPathComponent:@"db.sqlite"];
db = [FMDatabase databaseWithPath:database_path];
//创建表
NSString *TABLENAME = @"TableName", *ID = @"Id", *NAME = @"Name", *AGE = @"Age", *ADDRESS = @"Address";
if ([db open]) {
NSString *sqlCreateTable = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' INTEGER PRIMARY KEY AUTOINCREMENT, '%@' TEXT, '%@' INTEGER, '%@' TEXT)",TABLENAME,ID,NAME,AGE,ADDRESS];
BOOL res = [db executeUpdate:sqlCreateTable];
if (!res) {
NSLog(@"error when creating db table");
} else {
NSLog(@"success to creating db table");
}
[db close];
}
//添加数据
if ([db open]) {
NSString *insertSql1 = [NSString stringWithFormat:@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')", TABLENAME, NAME, AGE, ADDRESS, @"YS", @"13", @"JINAN"];
BOOL res = [db executeUpdate:insertSql1];
NSString *insertSql2 = [NSString stringWithFormat:@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')", TABLENAME, NAME, AGE, ADDRESS, @"ZZ", @"0", @"HH"];
BOOL res2 = [db executeUpdate:insertSql2];
if (!res2) {
NSLog(@"error when insert db table");
} else {
NSLog(@"success to insert db table");
}
[db close];
NSLog(@"🙃");
}
//修改数据
if ([db open]) {
NSString *updateSql = [NSString stringWithFormat:@"UPDATE '%@' SET '%@' = '%@' WHERE '%@' = '%@'",TABLENAME, AGE, @"15", AGE, @"13"];
BOOL res3 = [db executeUpdate:updateSql];
if (!res3) {
NSLog(@"error when update db table");
} else {
NSLog(@"success to update db table");
}
}
//删除数据
if ([db open]) {
NSString *deleteSql = [NSString stringWithFormat:@"delete from %@ where %@ = '%@'",TABLENAME, NAME, @"张三"];
BOOL res4 = [db executeUpdate:deleteSql];
if (!res4) {
NSLog(@"error when delete db table");
} else {
NSLog(@"success to delete db table");
}
[db close];
}
//数据库查询
if ([db open]) {
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM %@",TABLENAME];
FMResultSet *rs = [db executeQuery:sql];
while ([rs next]) {
int Id = [rs intForColumn:ID];
NSString *name = [rs stringForColumn:NAME];
NSString *age = [rs stringForColumn:AGE];
NSString *address = [rs stringForColumn:ADDRESS];
NSLog(@"id = %d, name = %@, age = %@, address = %@",Id, name, age, address);
}
[db close];
}
//数据库多线程操作
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:database_path];
dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);
dispatch_queue_t q2 = dispatch_queue_create("queue2", NULL);
dispatch_async(q1, ^{
for (int i = 0; i < 50; ++i) {
[queue inDatabase:^(FMDatabase *db2) {
NSString *insertSql1 = [NSString stringWithFormat:@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES (?,?,?)",TABLENAME, NAME, AGE, ADDRESS];
NSString *name = [NSString stringWithFormat:@"jack %d", i];
NSString *age = [NSString stringWithFormat:@"%d",10 + i];
BOOL res5 = [db2 executeUpdate:insertSql1, name, age, @"济南"];
if (!res5) {
NSLog(@"error to insert data:%@", name);
} else {
NSLog(@"success to insert data:%@", name);
}
}];
}
});
dispatch_async(q2, ^{
for (int i = 0; i < 50; ++i) {
[queue inDatabase:^(FMDatabase *db) {
NSString *insertSql2 = [NSString stringWithFormat:@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES (?, ?, ?)",TABLENAME, NAME, AGE, ADDRESS];
NSString *name = [NSString stringWithFormat:@"lilei %d",i];
NSString *age = [NSString stringWithFormat:@"%d", 10+i];
BOOL res = [db executeUpdate:insertSql2,name, age, @"北京"];
if (!res) {
NSLog(@"error to inster data: %@",name);
} else {
NSLog(@"succ to inster data: %@",name);
}
}];
}
});
//路径
NSString *path = [[NSBundle mainBundle] bundlePath];
NSLog(@"%@",path);
NSString *path1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
NSString *path2 = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
NSString *path3 = NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, YES).firstObject;
NSString *path4 = NSTemporaryDirectory();
NSString *path6 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
NSString *fileName = [path6 stringByAppendingPathComponent:@"123.plist"];
NSArray *array = @[@"123", @"456", @"789"];
[array writeToFile:fileName atomically:YES];
NSArray *result = [NSArray arrayWithContentsOfFile:fileName];
//Preference
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@"aaaaaaa" forKey:@"a"];
[userDefaults setBool:YES forKey:@"sex"];
[userDefaults setInteger:21 forKey:@"age"];
[userDefaults synchronize];
BOOL sex = [userDefaults boolForKey:@"sex"];
NSInteger age = [userDefaults integerForKey:@"age"];
}