数据库 __ 浅谈
数据库特征 :
1. 以一定方式存储在一起
2. 能为多个用户共享
3. 具有尽可能少的冗余代码
4. 与程序批次独立的数据集合
SQLite 字段约束条件 :
(一) NOT NULL (非空) : 表示必填选项
(二) UNQUE(唯一)
(三) PRIMARY KEY (主键)
1. NOT NULL :包含不能为空
2. 不重复
3. 唯一标识
注意: primary key 不是必须存在的,但当它存在时,则必须遵循以上三个原则
(四) FOREIGN KEY (外键) : 外边一张表的主键
(五) AUTOINCREATE :当吧自增型变量设置为NSTER 时,会根据变量的增多而自增
数据库使用流程:
(一) 打开数据库
(二) 对数据库进行操作
SQL 语句 :
1 . Create table (建) :
create table if not exists 表名 (字段 约束 , 字段 约束 )
注意 : 字段类型一定要填上
2 . Insert (增) :
insert into 表名 (字段名1 , 字段名2) values ( 值 )
注意 : 值如果是字符串或者字符类型时,用 ' ' 括起来
3 . Update (改) :
update 表名 set 字段名 = 修改的值 where 条件 ang (or) 条件
4 . Delete (删) :
delete from 表名 where 条件;
5 . Select (查) :
select 要查找的字段(一般用 * ) from 表名 where 条件;
(三) 关闭数据库
代码演示
--------ViewController.m ----
#import "ViewController.h"
#import "DataBaseHandle.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
DataBaseHandle *dataBase = [DataBaseHandle sharedDataBaseHandle];
[dataBase openDB];
[dataBase createTable];
[dataBase insertName:@"黄菊" gender:@"女" age:20];
[dataBase updateWithUID:1];
[dataBase deleteWithUID:1];
[dataBase searchAll];
[dataBase searchWithName:@"黄菊"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
-------------- DataBaseHandle.h-----
#import <Foundation/Foundation.h>
@interface DataBaseHandle : NSObject
+ (DataBaseHandle *)sharedDataBaseHandle;
//打开数据库
- (void)openDB;
//关闭数据库
- (void)cloceDB;
//创建表
- (void)createTable;
//插入一条数据
- (void)insertName:(NSString *)name
gender:(NSString *)gender
age:(NSInteger)age;
//通过UID更新一个数据
- (void)updateWithUID:(NSInteger)uid;
//通过UID删除一条数据
- (void)deleteWithUID:(NSInteger)uid;
//搜索全部
- (void)searchAll;
//根据name查询一条数据
- (void)searchWithName:(NSString *)name;
@end
---------------DataBaseHandle.m----
#import "DataBaseHandle.h"
#import <sqlite3.h>
@interface DataBaseHandle()
//documents文件夹下的一个叫做person.sqlite的路径
@property(nonatomic,copy)NSString *dbPath;
@end
static DataBaseHandle *dataBase = nil;
@implementation DataBaseHandle
+ (DataBaseHandle *)sharedDataBaseHandle
{
if (dataBase == nil) {
dataBase = [[DataBaseHandle alloc] init];
}
return dataBase;
}
- (NSString *)dbPath
{
if (!_dbPath ) {
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
_dbPath = [document stringByAppendingPathComponent:@"person.sqlite"];
}
return _dbPath;
}
static sqlite3 *db = nil;
- (void)openDB
{
//打开数据库的函数
//在数据库里边 所有的字符串都要变成utf-8的编码格式
int result = sqlite3_open(self.dbPath.UTF8String, &db);
if (result == SQLITE_OK) {
NSLog(@"打开成功");
}else
{
NSLog(@"打开失败");
}
}
- (void)cloceDB
{
int result = sqlite3_close(db);
if (result == SQLITE_OK) {
NSLog(@"关闭成功");
}else
{
NSLog(@"关闭失败 %d",result);
}
}
- (void)createTable
{
// 创建一个person表, 要求字段:UID integer 主键,自增 name text, gender text, age integer
NSString *createString = @"create table if not exists person (uid integer primary key autoincrement not null , name text, gender text, age integer)";
//第一个参数: 数据库
//第二个参数: sql语句,要用utf-8 的格式
//第三个参数: 结果的回调函数
//第四个参数: 回调函数的参数
//第五个参数: 错误信息
int result = sqlite3_exec(db, createString.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"创表成功");
}else
{
NSLog(@"创表失败 %d",result);
}
//打印地址
NSLog(@"%@",self.dbPath);
}
- (void)insertName:(NSString *)name gender:(NSString *)gender age:(NSInteger)age
{
//插入数据的sql语句,数据不确定,所以在value里面使用 ? 代替,之后向里面绑定
NSString *insertString = @"insert into person (name, gender, age) values (?, ?, ?)";
//sqlite的伴随指针
sqlite3_stmt *stmt = nil;
//预执行sql语句
//第一个参数: 数据库
//第二个参数: sql语句
//第三个参数: 如果为正,例如 : 1 ,表示在去参数的时候,只取一个字节;使用负数表示取值取到碰到结束符号('\000','u000') .
//第四个参数: 伴随指针, 会伴随着数据库的操作,获取值或绑定值
//第五个参数: 取值的时候如果取得不全,那么剩下的都存在这里.
int result = sqlite3_prepare(db, insertString.UTF8String, -1, &stmt, NULL);
//如果预执行成功的话,那么就要往里面放数据了
if (result == SQLITE_OK) {
//想预执行的sql语句里面插入参数(取代 '?' 的位置)
//第一个参数: 伴随指针
//第二个参数: '?'的位置,从1开始
//第三个参数: 插入的数据
//第四个参数: 和上面的-1是一样的
//第五个参数: 回调函数
sqlite3_bind_text(stmt, 1, name.UTF8String, -1, NULL);
sqlite3_bind_text(stmt, 2, gender.UTF8String, -1, NULL);
sqlite3_bind_int64(stmt, 3, age);
//sql语句已经全了
//执行伴随指针,如果为SQLITE_DONE 代表执行成功,并且成功的插入数据
if (sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(@"插入成功");
}
}
//一定要记得释放掉伴随指针
sqlite3_finalize(stmt);
}
- (void)updateWithUID:(NSInteger)uid
{
NSString *updateString = @"update person set name = '黄日女' where uid = ?";
//伴随指针
sqlite3_stmt *stmt = nil;
int result = sqlite3_prepare(db, updateString.UTF8String, -1, &stmt, NULL);
if (result == SQLITE_OK) {
sqlite3_bind_int64(stmt, 1, uid);
if (sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(@"修改成功");
}
}
sqlite3_finalize(stmt);
}
//简单的操作方式
- (void)deleteWithUID:(NSInteger)uid
{
NSString *deleteString = [NSString stringWithFormat:@"delete from person where uid = %ld",uid];
int result = sqlite3_exec(db, deleteString.UTF8String, NULL, NULL, NULL);
if (result == SQLITE_OK) {
NSLog(@"删除成功");
}
}
- (void)searchAll
{
NSString *searchString =@"select *from person";
sqlite3_stmt *stmt = nil;
int result = sqlite3_prepare(db, searchString.UTF8String, -1, &stmt, NULL);
if (result == SQLITE_OK) {
//当sqlite3_step(stmt) == SQLITE_ROW 的时候,代表还有下一条数据
while (sqlite3_step(stmt) == SQLITE_ROW) {
//第二个参数: 表示参数的位置,从0开始
int uid = sqlite3_column_int(stmt, 0);
NSLog(@"%d",uid);
NSString *name = [NSString stringWithUTF8String:( const char *) sqlite3_column_text(stmt, 1)];
NSLog(@"%@",name);
NSString *gender = [NSString stringWithUTF8String:( const char *) sqlite3_column_text(stmt, 2)];
NSLog(@"%@",gender);
int age = sqlite3_column_int(stmt, 3);
NSLog(@"%d",age);
}
}
}
- (void)searchWithName:(NSString *)name
{
NSString *searchStr = @"select uid, gender, age from person where name = ?";
sqlite3_stmt *stmt = nil;
int result = sqlite3_prepare(db, searchStr.UTF8String, -1, &stmt, NULL);
if (result == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, name.UTF8String, -1, NULL);
while (sqlite3_step(stmt) == SQLITE_ROW) {
int uid = sqlite3_column_int(stmt, 0);
NSLog(@"uid ==== %d", uid);
NSString *gender = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
NSLog(@"gender === %@", gender);
int age = sqlite3_column_int(stmt, 3);
NSLog(@"age === %d", age);
}
} else {
NSLog(@"result ==== %d", result);
}
sqlite3_finalize(stmt);
}
@end