数据库:
存储数据的仓库, 数据以表的形式放在数据库中, 表中每一列都有一个名字(字段), 其中一个字段标识数据的唯一性, 称为主键, 主键唯一, 不为空
常见的数据库: access, MySQL, SQLServer, Oracle
移动开发常用的轻量级数据库: sqlite
数据库常见的操作: 创建表, 增删改查数据
//打开数据库
- (IBAction)open:(UIButton *)sender {
//判断数据库是否打开了, 如果打开了就不用再次执行打开操作
if (db) {
return;
}
//.sqlite文件的路径
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *dbPath = [NSString stringWithFormat:@"%@/database.sqlite", docPath];
NSLog(@"%@", dbPath);
//[dbPath UTF8String], 把字符串从OC转成C的类型
//创建一个数据库指针
db = nil;
//打开数据库, 并把给定 数据库指针 指向 打开的数据库
int result = sqlite3_open([dbPath UTF8String], &db);
if (result == SQLITE_OK) {
NSLog(@"打开数据库成功");
} else {
NSLog(@"打开数据库失败");
}
}
//创建表
- (IBAction)creat:(UIButton *)sender {
//创建sql语句
NSString *sqlString = @"CREATE TABLE \"Girl\" (\"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , \"name\" TEXT DEFAULT 傻逼, \"gender\" TEXT DEFAULT 女, \"age\" INTEGER DEFAULT 18)";
//创建表
char *error = NULL;
int result = sqlite3_exec(db, [sqlString UTF8String], NULL, NULL, &error);
if (result == SQLITE_OK) {
NSLog(@"创建表成功");
} else {
NSLog(@"创建表失败%s", error);
}
}
//加入数据
- (IBAction)add:(UIButton *)sender {
NSString *string = @"insert into Girl (name, age, gender) values ('奎因', 28, '女')";
char *error = nil;
int result = sqlite3_exec(db, [string UTF8String], NULL, NULL, &error);
if (result == SQLITE_OK) {
NSLog(@"插入成功");
} else {
NSLog(@"插入失败%s", error);
}
}
- (IBAction)update:(id)sender {
NSString *upDate = @"update Girl set name = '蛮王' where id = 2";
char *error = nil;
int result = sqlite3_exec(db, [upDate UTF8String], NULL, NULL, &error);
if (result == SQLITE_OK) {
NSLog(@"更改成功");
}else {
NSLog(@"更改失败%s", error);
}
}
//删除数据
- (IBAction)delete:(id)sender {
NSString *delete = @"delete from Girl where id = 4";
char *error = nil;
int result = sqlite3_exec(db, [delete UTF8String], NULL, NULL, &error);
if (result == SQLITE_OK) {
NSLog(@"删除成功");
}else {
NSLog(@"删除失败%s", error);
}
}
//查找方法
- (IBAction)select:(id)sender {
//sql语句
NSString *selectString = @"select * from Girl";
//sql语句指针, statement
sqlite3_stmt *stmt = nil;
//sql语句和指针联系起来
int result = sqlite3_prepare(db, [selectString UTF8String], -1, &stmt, NULL);
if (result == SQLITE_OK) {
NSLog(@"关联成功");
//单步执行, 把查询结果一条一条的找到
//判断有没有一行数据
while (sqlite3_step(stmt) == SQLITE_ROW) {
//找到某一列的数据
int ID = sqlite3_column_int(stmt, 0);
const unsigned char *name = sqlite3_column_text(stmt, 1);
const unsigned char *gender = sqlite3_column_text(stmt, 2);
int age = sqlite3_column_int(stmt, 3);
NSString *nameString = [[NSString alloc] initWithUTF8String:(const char *)name];
NSString *genderString = [[NSString alloc] initWithUTF8String:(const char *)gender];
NSLog(@"%d, %@, %@, %d", ID, nameString, genderString, age);
}
}else {
NSLog(@"关联失败");
}
//释放语句指针
sqlite3_finalize(stmt);
}
//关闭数据库
- (IBAction)close:(id)sender {
//判断数据库是否处于打开状态
if (db == NULL) {
return;
}
//关闭数据库
int result = sqlite3_close(db);
db = NULL;
if (result == SQLITE_OK) {
NSLog(@"关闭数据库成功");
} else {
NSLog(@"关闭数据库失败");
}
}