一、 SQL数据库的建立,查询和删除等常用操作可以参考这个网址 http://www.w3school.com.cn/sql/
二、数据库的打开和关闭(iOS下要导入libsqlite3.dylib框架)
一般情况下会将其封装为一个单一的类,便于外界使用。
其 .h 中声明两个类方法,代码为
#import <Foundation/Foundation.h> #import <sqlite3.h> @interface DataBase : NSObject // 打开数据库 + (sqlite3 *)openDB; // 关闭数据库 + (void)closeDB; @end
在 .m 中实现两个方法
#import "DataBase.h" static sqlite3 *db; @implementation DataBase + (sqlite3 *)openDB{ // 如果已经打开就直接返回数据库 if (db) { return db; } // 获取document的路径 NSString *documentPath = [NSSearchPathForDirectoriesInDomains(9, 1, 1) lastObject]; // 拼接字符串 NSString *path = [documentPath stringByAppendingPathComponent:@"Students.sqlite"]; // 文件管理类对象(单例) NSFileManager *fileManger = [NSFileManager defaultManager]; // 判断数据库是否在document中,不在的话将数据库文件拷贝至document,因为在应用包中的数据库文件是无法读取的,只有放在沙盒下才可以读取 if ([fileManger fileExistsAtPath:path] == NO) { NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Students" ofType:@"sqlite"]; [fileManger copyItemAtPath:filePath toPath:path error:nil]; } sqlite3_open([path UTF8String], &db); return db; } // 关闭数据库 + (void)closeDB{ sqlite3_close(db); } @end
三、 一些具体的操作(取出所有数据, 删除, 更新,查找等), 最好定义一个单例类,方便外界使用(因为其使用较为频繁)
1,需要定义一个类,比如Student,其内部代码
#import <Foundation/Foundation.h> @interface Student : NSObject @property(nonatomic, assign)int number; @property(nonatomic, copy)NSString *name; @property(nonatomic, copy)NSString *sex; - (instancetype)initWithNumber:(int)number name:(NSString *)name sex:(NSString *)sex; @end
#import "Student.h" @implementation Student - (instancetype)initWithNumber:(int)number name:(NSString *)name sex:(NSString *)sex{ self = [super init]; if (self) { self.number = number; self.name = name; self.sex = sex; } return self; } @end
2,定义一个单例类,比如StudentHandle
现在.h 中声明几个方法
#import <Foundation/Foundation.h> #import "Student.h" @interface StudentHandle : NSObject // 单例方法 + (StudentHandle *)standardHandle; // 取出所有对象 - (NSArray *)allStudents; // 根据number取出某个对象 - (Student *)studentByNumber:(int)number; // 插入数据 - (BOOL)insertStudentWithName:(NSString *)name sex:(NSString *)sex; // 删除数据 - (BOOL)deleteStudentByNumber:(int)number; // 更新数据 - (BOOL)updateStudentWithName:(NSString *)name byNumber:(int)number; @end
#import "StudentHandle.h" #import "DataBase.h" @implementation StudentHandle // 创建单例 static StudentHandle *studentHandle = nil; + (StudentHandle *)standardHandle{ @synchronized(self){ if (studentHandle == nil) { studentHandle = [[StudentHandle alloc] init]; } return studentHandle; } } - (NSArray *)allStudents{ // 1,打开数据库 sqlite3 *db = [DataBase openDB]; // 2,准备SQL语句 sqlite3_stmt *stmt = nil; int result = sqlite3_prepare(db, "select * from StudentInfo", -1, &stmt, nil); NSMutableArray *arr = nil; if (result == SQLITE_OK) { // 3,执行stmt arr = [NSMutableArray arrayWithCapacity:0]; while (sqlite3_step(stmt) == SQLITE_ROW) { // 4,获取数据 int number = sqlite3_column_int(stmt, 0); const unsigned char *nameUTF = sqlite3_column_text(stmt, 1); const unsigned char *sexUTF = sqlite3_column_text(stmt, 2); Student *stu = [[Student alloc] initWithNumber:number name:[NSString stringWithUTF8String:(const char *)nameUTF] sex:[NSString stringWithUTF8String:(const char *)sexUTF]]; [arr addObject:stu]; } } // 释放stmt所占用的内存 sqlite3_finalize(stmt); return arr; } - (Student *)studentByNumber:(int)number{ sqlite3 *db = [DataBase openDB]; sqlite3_stmt *stmt = nil; int result = sqlite3_prepare_v2(db, "select name,number, sex from StudentInfo where number = ?", -1, &stmt, nil); Student *stu = nil; if (result == SQLITE_OK) { sqlite3_bind_int(stmt, 1, number); if (sqlite3_step(stmt) == SQLITE_ROW) { const unsigned char *nameUTF = sqlite3_column_text(stmt, 0); int number = sqlite3_column_int(stmt, 1); const unsigned char *sexUTF = sqlite3_column_text(stmt, 2); stu = [[Student alloc] initWithNumber:number name:[NSString stringWithUTF8String:(const char *)nameUTF] sex:[NSString stringWithUTF8String:(const char *)sexUTF]]; } } sqlite3_finalize(stmt); return stu; } - (BOOL)insertStudentWithName:(NSString *)name sex:(NSString *)sex{ sqlite3 *db = [DataBase openDB]; sqlite3_stmt *stmt = nil; int result = sqlite3_prepare_v2(db, "insert into StudentInfo (name, sex) values (?, ?)", -1, &stmt, nil); BOOL insertInfo = NO; if (result == SQLITE_OK) { sqlite3_bind_text(stmt, 1, [name UTF8String], -1, nil); sqlite3_bind_text(stmt, 2, [sex UTF8String], -1, nil); if (sqlite3_step(stmt) == SQLITE_DONE) { insertInfo = YES; } } sqlite3_finalize(stmt); return insertInfo; } - (BOOL)deleteStudentByNumber:(int)number{ sqlite3 *db = [DataBase openDB]; sqlite3_stmt *stmt = nil; int result = sqlite3_prepare_v2(db, "delete from StudentInfo where number = ?", -1, &stmt, nil); BOOL deleteInfo = NO; if (result == SQLITE_OK) { sqlite3_bind_int(stmt, 1, number); if (sqlite3_step(stmt) == SQLITE_DONE) { deleteInfo = YES; } } sqlite3_finalize(stmt); return deleteInfo; } - (BOOL)updateStudentWithName:(NSString *)name byNumber:(int)number{ sqlite3 *db = [DataBase openDB]; sqlite3_stmt *stmt = nil; int result = sqlite3_prepare_v2(db, "update StudentInfo set name = ? where number = ?", -1, &stmt, nil); BOOL updateInfo = NO; if (result == SQLITE_OK) { sqlite3_bind_text(stmt, 1, [name UTF8String], -1, nil); sqlite3_bind_int(stmt, 2, number); if (sqlite3_step(stmt) == SQLITE_DONE) { return updateInfo = YES; } } sqlite3_finalize(stmt); return updateInfo; } @end
时间: 2024-10-13 17:07:23