SQLite :輕量級數(shù)據(jù)庫讲衫,與其他數(shù)據(jù)庫管理系統(tǒng)不同,其安裝和運行非常簡單孵班,我們以創(chuàng)建Student類為例 通過SQLite數(shù)據(jù)庫實現(xiàn)對Student實例變量的增涉兽,刪,改篙程,查等操作
Student類
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *gender;
@property(nonatomic,assign)int age;
@property(nonatomic,assign)int Stu_ID;
-(instancetype)initWithName:(NSString *)name
age:(int)age
gender:(NSString *)gender
stu_ID:(int)stu_ID;
@end
#import "Student.h"
@implementation Student
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
//重寫description
-(NSString *)description{
return [NSString stringWithFormat:@"%@,%@,%d,%d",_name,_gender,_age,_Stu_ID];
}
//初始化
-(instancetype)initWithName:(NSString *)name age:(int)age gender:(NSString *)gender stu_ID:(int)stu_ID{
if (self = [super init]) {
_name = name;
_age = age;
_gender = gender;
_Stu_ID = stu_ID;
}return self;
}
@end
SQLite創(chuàng)建Database 聲明和實現(xiàn)基本的方法 代碼如下:
#import <Foundation/Foundation.h>
@class Student;
@interface Database : NSObject
//創(chuàng)建單例
+(instancetype)sharedDataBase;
-(void)openDB;
-(void)closeDB;
//添加
-(void)insertStudent:(Student *)stu;
//刪除
-(void)deleteStudent:(int )stu_ID;
//修改
-(void)updataStudentWithGender:(NSString *)gender andStu_ID:(int)stu_ID;
//選擇全部
-(NSArray *)selectAll;
//選擇學生單個
-(Student *)selectStudentWithStu_ID:(int)stu_ID;
@end
Database.m 以下對數(shù)據(jù)操作的各個方法做了較為詳細的介紹
#import "Database.h"
#import <sqlite3.h>
#import "Student.h"
@implementation Database
static Database *dataBase = nil;
+(instancetype)sharedDataBase{
//加鎖
@synchronized(self) {
if (nil == dataBase) {
dataBase = [[Database alloc]init];
[dataBase openDB];//打開數(shù)據(jù)庫
}
}
return dataBase;
}
#pragma mark--------//創(chuàng)建數(shù)據(jù)庫對象
static sqlite3 *db = nil;
-(void)openDB{
//如果數(shù)據(jù)庫已經(jīng)打開 則不需要執(zhí)行后面的操作 直接return
if (db!= nil) {
return;
}
//創(chuàng)建保存數(shù)據(jù)庫的路徑
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
documentPath = [documentPath stringByAppendingString:@"/LOClass.sqlite"];
NSLog(@"%@",documentPath);
//如果改數(shù)據(jù)庫存在 則直接打開 否則 自動創(chuàng)建一個數(shù)據(jù)庫
int result = sqlite3_open([documentPath UTF8String], &db);
if (result == SQLITE_OK) {
// NSLog(@"數(shù)據(jù)庫成功打開");
//建表
NSString *sql = @"CREATE TABLE Class43 (Stu_ID INTEGER PRIMARY KEY NOT NULL UNIQUE, name TEXT NOT NULL, gender TEXT NOT NULL DEFAULT M, age INTEGER NOT NULL);";
//執(zhí)行語句
sqlite3_exec(db, [sql UTF8String], NULL, NULL, NULL);
}else{
NSLog(@"%d",result);
}
}
//關(guān)閉數(shù)據(jù)庫
-(void)closeDB{
int result = sqlite3_close(db);
if (result == SQLITE_OK) {
NSLog(@"數(shù)據(jù)庫關(guān)閉成功");
//當關(guān)閉數(shù)據(jù)庫的時候?qū)?shù)據(jù)庫置為nil 是因為打開數(shù)據(jù)庫的時候需要使用nil做判斷 循環(huán)的理論
db = nil; //數(shù)據(jù)庫是在靜態(tài)去
}else{
NSLog(@"數(shù)據(jù)庫關(guān)閉失敗%d",result);
}
}
//插入單個student對象
-(void)insertStudent:(Student *)stu{
// 1 打開數(shù)據(jù)庫
[self openDB];
// 2 創(chuàng)建跟隨指針(也叫伴隨指針)
sqlite3_stmt *stmt = nil;
// 3 準備SQL語句 問好就是占位 枷畏??虱饿?拥诡??
NSString *sqlString = @"insert into Class43 (Stu_ID, name, gender, age) values (?, ?, ?, ?)";
// 4 驗證SQL語句的正確性
int result = sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &stmt, NULL);
// 5 綁定
if (result == SQLITE_OK) {
NSLog(@"數(shù)據(jù)庫添加成功");
//一旦SQL語句沒有問題就要開始綁定數(shù)據(jù)替換 氮发?
#pragma mark ---第一個參數(shù)是跟隨指針 第二個參數(shù)是 袋倔?的順序 是從1開始的-第三個是要綁定的值--
sqlite3_bind_int(stmt, 1, stu.Stu_ID);
sqlite3_bind_text(stmt, 2, [stu.name UTF8String], -1, NULL);
sqlite3_bind_text(stmt, 3, [stu.gender UTF8String], -1, NULL);
sqlite3_bind_int(stmt, 4, stu.age);
//6 單步執(zhí)行 將伴隨指針傳進去
sqlite3_step(stmt);
}else{
NSLog(@"數(shù)據(jù)庫添加失敗%d",result);
}
//7 釋放跟隨指針占用的內(nèi)存
sqlite3_finalize(stmt);
}
//刪除學生對象
-(void)deleteStudent:(int)stu_ID{
[self openDB];
sqlite3_stmt *stmp = nil;
NSString *sqlString = @"delete from Class43 where Stu_ID = ?";
int result = sqlite3_prepare_v2(db, [sqlString UTF8String], -1, &stmp, NULL);
if (result == SQLITE_OK) {
NSLog(@"數(shù)據(jù)庫是刪除成功");
//開始綁定
sqlite3_bind_int(stmp, 1, stu_ID);
sqlite3_step(stmp);
}else{
NSLog(@"數(shù)據(jù)庫刪除失敗");
}
sqlite3_finalize(stmp);
}
//更新學生信息 在此根據(jù)學號改變學生性別
-(void)updataStudentWithGender:(NSString *)gender andStu_ID:(int)stu_ID{
[self openDB];
sqlite3_stmt *stmt = nil;
NSString *sql = @"update Class43 set gender = ? where Stu_ID = ?";
int result = sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, NULL);
if (result == SQLITE_OK) {
NSLog(@"修改成功");
sqlite3_bind_text(stmt, 1,[gender UTF8String], -1, nil);
sqlite3_bind_int(stmt, 2, stu_ID);
sqlite3_step(stmt);
}else{
NSLog(@"修改失敗");
}
sqlite3_finalize(stmt);
}
//查詢所有數(shù)據(jù)庫中
-(NSArray *)selectAll{
[self openDB];
sqlite3_stmt *stmt = nil;
NSString *sql = @"select * from Class43";
int result = sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, NULL);
if (result == SQLITE_OK) {
NSLog(@"查詢?nèi)砍晒?);
//創(chuàng)建可變數(shù)組用來存放查詢到的學生
NSMutableArray *muArray = [NSMutableArray array];
while (sqlite3_step(stmt) == SQLITE_ROW) {
//根據(jù)SQL語句將搜索到的符合條件的值取出來
//0 代表數(shù)據(jù)庫表的第一列
int stu_id =sqlite3_column_int(stmt, 0);
NSString *name = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
NSString *gender = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
int age = sqlite3_column_int(stmt, 3);
//將取出來的信息 賦值給學生的Model
Student *stu = [[Student alloc]initWithName:name age:age gender:gender stu_ID:stu_id];
//將學生添加到可變數(shù)組里面
[muArray addObject:stu];
}
sqlite3_finalize(stmt);
return muArray;
}else{
NSLog(@"查詢?nèi)渴?d",result);
}
sqlite3_finalize(stmt);
return nil;
}
//根據(jù)學生的學號來進行查詢單個學生對象
-(Student *)selectStudentWithStu_ID:(int)stu_ID{
[self openDB];
sqlite3_stmt *stmt = nil;
NSString *str = @"select * from Class43 where Stu_ID =?";
int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, NULL);
if (result == SQLITE_OK) {
NSLog(@"查詢單個學生完成");
sqlite3_bind_int(stmt, 1, stu_ID);
Student *stu = [Student new];
while (sqlite3_step(stmt)== SQLITE_ROW) {
int stu_id = sqlite3_column_int(stmt, 0);
NSString *name = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];
NSString *gender = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)];
int age = sqlite3_column_int(stmt, 3);
stu.Stu_ID = stu_id;
stu.name = name;
stu.age = age;
stu.gender = gender;
sqlite3_finalize(stmt);
return stu;
}
}
else{
NSLog(@"查詢數(shù)據(jù)庫單個學生失敗%d",result);
}
sqlite3_finalize(stmt);
return nil;
}
@end
下面我們在ViewController.m 中實現(xiàn)數(shù)據(jù)的 增 刪 改 查 的功能
#import "ViewController.h"
#import "Database.h"
#import "Student.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// [[Database sharedDataBase]openDB];
}
- (IBAction)add:(id)sender {
NSLog(@"增加");
Student *stu = [Student new];
stu.name = @"劉高見";
stu.gender = @"男";
stu.Stu_ID = 1;
stu.age = 18;
Student *stu1 = [[Student alloc]initWithName:@"王" age:21 gender:@"女" stu_ID:2];
Student *stu2 = [[Student alloc]initWithName:@"劉" age:21 gender:@"男" stu_ID:3];
Student *stu3 = [[Student alloc]initWithName:@"趙" age:21 gender:@"女" stu_ID:4];
Student *stu4 = [[Student alloc]initWithName:@"謝" age:21 gender:@"男" stu_ID:5];
Database *database =[Database sharedDataBase];
[database insertStudent:stu];
[database insertStudent:stu1];
[database insertStudent:stu2];
[database insertStudent:stu3];
[database insertStudent:stu4];
}
- (IBAction)delete:(id)sender {
// NSLog(@"刪除");
[[Database sharedDataBase]deleteStudent:3];
}
- (IBAction)selectAll:(id)sender {
// NSLog(@"查詢所有");
for (Student *stu in [[Database sharedDataBase]selectAll]) {
NSLog(@"%@",stu);
}
}
- (IBAction)selectSingle:(id)sender {
// NSLog(@"查詢單個");
Student *stu =[Student new];
stu = [[Database sharedDataBase]selectStudentWithStu_ID:1];
NSLog(@"%@",stu);
}
- (IBAction)updata:(id)sender {
// NSLog(@"修改");
Database *database = [Database sharedDataBase];
[database updataStudentWithGender:@"女" andStu_ID:5];
NSLog(@"%@",[[Database sharedDataBase]selectStudentWithStu_ID:5]);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
以上就是本文的全部內(nèi)容 期待大家的批評指導~~~~