有关Sqlite数据库的相关使用

一.在iOS中使用Sqlite的条件:

<1>.导入FMDB第三方库。

<2>.导入libsqlite3.tbd系统库。

二.使用步骤:

<1>.FMDB导入一个封装的多线程类(LosDatabaseHelper.h)和FMDB.h

 在LosDatabaseHelper类中的代码

在.h文件中

#import#import "FMDatabaseQueue.h"

@interface LosDatabaseHelper : NSObject

+(FMDatabaseQueue *)getSharedDatabaseQueue;

@end

在.m文件中

#import "LosDatabaseHelper.h"

@implementation LosDatabaseHelper{

FMDatabaseQueue *queue;

}

+(FMDatabaseQueue *)getSharedDatabaseQueue{

static FMDatabaseQueue *my_FMDatabaseQueue=nil;

if (!my_FMDatabaseQueue) {

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"db_CMBCC.sqlite"];

my_FMDatabaseQueue = [FMDatabaseQueue databaseQueueWithPath:path];

}

return my_FMDatabaseQueue;

}

@end


<2>.创建一个FMDatabaseQueue多线程管理类的对象,调用对象方法,对数据库进行判断是否存在,若存在则直接进行增、删、改、查操作,若不存在则创建数据库,然后进行增、删、改、查操作。

FMDatabaseQueue *queue = [LosDatabaseHelper getSharedDatabaseQueue];

[queue inDatabase:^(FMDatabase *db) {

if ([db open]) {

[db setShouldCacheStatements:YES];

if (![db tableExists:@"userinfo"]) {

[db executeUpdate:@"CREATE TABLE userinfo(userinfo_id INTEGER PRIMARY KEY, name TEXT, avatar TEXT, email TEXT, phone TEXT, token TEXT, sex INTEGER, height INTEGER, weight INTEGER, goal INTEGER, max_heart_rate INTEGER, birthday INTEGER)"];

NSLog(@"用户信息数据库创建完成!");

}

NSString *sex_Str1 = [NSString stringWithFormat:@"%ld", (long)[userinfo.sex intValue]];

NSString *height_Str1 = [NSString stringWithFormat:@"%ld", (long)[userinfo.height integerValue]];

NSString *weight_Str1 = [NSString stringWithFormat:@"%ld", (long)[userinfo.weight integerValue]];

NSString *goal_Str1 = [NSString stringWithFormat:@"%ld", (long)[userinfo.goal integerValue]];

NSString *max_heart_rate_Str1 = [NSString stringWithFormat:@"%ld", (long)[userinfo.max_heart_rate integerValue]];

NSString *birthday_Str1 = [NSString stringWithFormat:@"%ld", (long)[userinfo.birthday integerValue]];

[db executeUpdate:@"insert into userinfo(name, avatar, email, phone, token, sex, height, weight, goal, max_heart_rate, birthday) values(?,?,?,?,?,?,?,?,?,?,?)", userinfo.name, userinfo.avatar, userinfo.email, userinfo.phone, userinfo.token, sex_Str1, height_Str1, weight_Str1, goal_Str1, max_heart_rate_Str1, birthday_Str1];

}

else{

NSLog(@"打开数据库失败!");

}

}];


<3>.一下是数据库语句

创建数据库: CREATE TABLE 创建的数据库名

[db executeUpdate:@"CREATE TABLE userinfo(userinfo_id INTEGER PRIMARY KEY, name TEXT, avatar TEXT, email TEXT, phone TEXT, token TEXT, sex INTEGER, height INTEGER, weight INTEGER, goal INTEGER, max_heart_rate INTEGER, birthday INTEGER)"];

注释:CREATE TABLE userinfo创建数据库语句

INTEGER,TEXT为变量类型,email,phone,token为变量字段。

增:insert into 数据库名(字段名) values(?)     一个字段名对应一个?

[db executeUpdate:@"insert into health(calorie, date, distance, end_time, is_sync, start_time, steps) values(?,?,?,?,?,?,?)", calorie_Str1, health.date, distance_Str1, health.end_time, is_sync_Str1, health.start_time, steps_Str1];

删:delete from 数据库名 where 后面加条件

[db executeUpdate:@"delete from health"];

改:update 数据库名 set 字段名 = ?,字段名 = ? where 加条件  后面跟着字段名的真实值。

[db executeUpdate:@"update health set calorie = ?, distance = ?, end_time = ?, is_sync = ?, steps = ? where date = ? and start_time = ?", calorie_Str, distance_Str, health.end_time, is_sync_Str, steps_Str, health.date, health.start_time];

查: select 字段名(*代表查询全部) 数据库名 where 条件

[db executeQuery:@"select * from health where is_sync = 0"]

注释: 增删改都是用update,查是用query

你可能感兴趣的:(有关Sqlite数据库的相关使用)