//獲得數據庫文件路徑
NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *fileName=[doc stringByAppendingPathComponent:@"wktest.sqlite"];
//獲得數據庫
FMDatabase *db=[FMDatabase databaseWithPath:fileName];
//打開數據庫
if ([db open]){
//創表
BOOL result=[db executeUpdate:@"CREATE TABLE IF NOT EXISTS customer_users (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, password text NOT NULL,phone text NOT NULL);"];
if(result){
NSLog(@"創表users成功");
}
else{
NSLog(@"創表失敗");
}
}
//插入數據
-(void)insert
{
for (int i = 0;i<10;i++){
NSString *name = [NSString stringWithFormat:@"user-%d",arc4random_uniform(100)];
[self.db executeUpdate:@"INSERT INTO customer_user (name, password,phone) VALUES (?,'123456', '13524568745');", name];
}
}
//刪除數據
-(void)delete
{
[self.db executeUpdate:@"DROP TABLE IF EXISTS customer_user;"];
[self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS customer_user (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, password text NOT NULL,phone text NOT NULL);"];
}
//查詢數據
-(void)query
{
//執行查詢語句
FMResultSet *resultSet = [self.db executeQuery:@"SELECT *FROM customer_user"];
//遍歷結果
while([resultSet next]){
int ID = [resultSet intForColumn:@"id"];
NSString *name = [resultSet stringForColumn:@"name"];
NSString *password = [resultSet stringForColumn:@"password"];
NSString *phone = [resultSet stringForColumn:@"phone"];
NSLog(@"%d %@ %@ %@",ID,name,password,phone);
}
}
4種使用等價:
/* 1. 直接使用完整的SQL更新語句 */
[database executeUpdate:@"insert into mytable(num,name,sex) values(0,'liuting','m');"];
NSString *sql = @"insert into mytable(num,name,sex) values(?,?,?);";
/* 2. 使用不完整的SQL更新語句,裏面含有待定字符串"?",需要後面的參數進行替代 */
[database executeUpdate:sql,@0,@"liuting",@"m"];
/* 3. 使用不完整的SQL更新語句,裏面含有待定字符串"?",需要數組參數裏面的參數進行替代 */
[database executeUpdate:sql
withArgumentsInArray:@[@0,@"liuting",@"m"]];
/* 4. SQL語句字符串可以使用字符串格式化 */
[database executeUpdateWithFormat:@"insert into mytable(num,name,sex) values(%d,%@,%@);",0,@"liuting","m
處理結果FMResultSet的常用方法:
/* 獲取下一個記錄 */
- (BOOL)next;
/* 獲取記錄有多少列 */
- (int)columnCount;
/* 通過列名得到列序號,通過列序號得到列名 */
- (int)columnIndexForName:(NSString *)columnName;
- (NSString *)columnNameForIndex:(int)columnIdx;
/* 獲取存儲的整形值 */
- (int)intForColumn:(NSString *)columnName;
- (int)intForColumnIndex:(int)columnIdx;
/* 獲取存儲的長整形值 */
- (long)longForColumn:(NSString *)columnName;
- (long)longForColumnIndex:(int)columnIdx;
/* 獲取存儲的布爾值 */
- (BOOL)boolForColumn:(NSString *)columnName;
- (BOOL)boolForColumnIndex:(int)columnIdx;
/* 獲取存儲的浮點值 */
- (double)doubleForColumn:(NSString *)columnName;
- (double)doubleForColumnIndex:(int)columnIdx;
/* 獲取存儲的字符串 */
- (NSString *)stringForColumn:(NSString *)columnName;
- (NSString *)stringForColumnIndex:(int)columnIdx;
/* 獲取存儲的日期數據 */
- (NSDate *)dateForColumn:(NSString *)columnName;
- (NSDate *)dateForColumnIndex:(int)columnIdx;
/* 獲取存儲的二進制數據 */
- (NSData *)dataForColumn:(NSString *)columnName;
- (NSData *)dataForColumnIndex:(int)columnIdx;
/* 獲取存儲的UTF8格式的C語言字符串 */
- (const unsigned cahr *)UTF8StringForColumnName:(NSString *)columnName;
- (const unsigned cahr *)UTF8StringForColumnIndex:(int)columnIdx;
/* 獲取存儲的對象,只能是NSNumber、NSString、NSData、NSNull */
- (id)objectForColumnName:(NSString *)columnName;
- (id)objectForColumnIndex:(int)columnIdx;
使用實例:
- (NSArray *)getResultFromDatabase{
//執行查詢SQL語句,返回查詢結果
FMResultSet *result = [_database executeQuery:@"select * from mytable"];
NSMutableArray *array = [NSMutableArray array];
//獲取查詢結果的下一個記錄
while ([result next]) {
//根據字段名,獲取記錄的值,存儲到字典中
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
int num = [result intForColumn:@"num"];
NSString *name = [result stringForColumn:@"name"];
NSString *sex = [result stringForColumn:@"sex"];
dict[@"num"] = @(num);
dict[@"name"] = name;
dict[@"sex"] = sex;
//把字典添加進數組中
[array addObject:dict];
}
return array;
}