您的位置:首页 > 数据库

SQLite数据库

2015-07-24 16:44 309 查看
1.创建数据库

// 声明数据库
static sqlite3 *db = nil;

#pragma mark 打开数据库
- (void)openDataBase
{

if (db != nil) {
return;
}
// 创建数据库

//  1.保存数据库的路径
NSString *path = [self getDataBasePath];
NSLog(@"%@",path);

// 2.拼接路径(数据库具体的文件)
path = [path stringByAppendingString:@"/data.sqlite"];

// 3.根据路径去打开数据库(如果数据库不存在---自动创建数据库)
int result = sqlite3_open([path UTF8String], &db);

// 4.判断数据库是否打开成功
if (SQLITE_OK == result) {
NSLog(@"数据库打开成功");
}else{

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


2.创建表格

#pragma mark 创建表格
- (void)createTable:(NSString *)tableName
{

// 创建表格 --- SQL语句                                            table不能少---很重要(没有会失败)

NSString *creatTableString = [NSString stringWithFormat:@"CREATE TABLE '%@'('id' INT PRIMARY KEY,'name' TEXT,'sex' TEXT)",tableName];

// 执行SQL语句
int result = sqlite3_exec(db, [creatTableString UTF8String], NULL, NULL, NULL);

if (result == SQLITE_OK) {
NSLog(@"创建%@表成功",tableName);
}else{

NSLog(@"创建%@表失败",tableName);
}

}


3.插入数据

#pragma makr 插入数据
- (void)insetIntoTableWithID:(int)nameID withName:(NSString *)name withSex:(NSString *)sex
{

// 插入语句
NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO 'user_hh'('id','name','sex')VALUES('%d', '%@', '%@')",nameID,name,sex];
// 执行SQL语句
int result = sqlite3_exec(db, [insertSql UTF8String], NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"插入成功");
}else{

NSLog(@"插入失败");
}
}


4.删除数据

#pragma mark 删除数据
- (void)deleteDataFromTableWithID:(int)nameID
{

// 写sql语句
NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM 'user_hh' WHERE id = '%d'",nameID];
int result = sqlite3_exec(db, [deleteSql UTF8String], NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"删除成功");
}else{

NSLog(@"删除失败");
}
}


5.更新数据

#pragma mark 更新数据
- (void)updateDataFromTableWithID:(int)nameID
{

NSString *updateSql = [NSString stringWithFormat:@"UPDATE 'user_hh' set sex = '不男不女' where id = %d",nameID];

int resquest = sqlite3_exec(db, [updateSql UTF8String], NULL, NULL, NULL);

if (resquest == SQLITE_OK) {
NSLog(@"更新成功");
}else{

NSLog(@"更新失败");
}
}


6.查询数据库

#pragma mark 查询数据库
- (void)selectDataFromTable
{

NSString *selectSql = [NSString stringWithFormat:@"SELECT *FROM 'user_hh'"];

// 保存查询到的结果集
sqlite3_stmt *stmt = nil;
// 准备查询数据(预存取)
int result = sqlite3_prepare(db, [selectSql UTF8String], -1, &stmt, NULL);
if (result == SQLITE_OK) {

// 判断是否是最后一行,有没有必要继续下去
// 这里用while循环 一行一行执行 ******不用if******
while(sqlite3_step(stmt) == SQLITE_ROW) {
// 拿出各列的数据

// 1.拿出id列的数据
int numberID = sqlite3_column_int(stmt, 0);

// 2.拿出name列的数据
const unsigned char *nameChar = sqlite3_column_text(stmt, 1);
NSString *name = [NSString stringWithUTF8String:(const char *)nameChar];

// 3.拿出sex列的数据
const unsigned char *sexChar = sqlite3_column_text(stmt, 2);
NSString *sex = [NSString stringWithUTF8String:(const char *)sexChar];

NSLog(@"%d %@ %@",numberID,name,sex);
}

// 结束查询 --- 重要 ****** 否则无法关闭数据库******
sqlite3_finalize(stmt);
}

}


7.关闭数据库

#pragma mark 关闭数据库
- (void)closeDataBase
{

// 关闭数据库
int result = sqlite3_close(db);
if (result == SQLITE_OK) {
NSLog(@"关闭数据库成功");
}else{

NSLog(@"关闭数据库失败");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: