1.基本概念
- 什么是数据库 1.以一定方式储存在一起; 2.能为多个用户共享; 3.具有尽可能小得冗余度; 4.与程序彼此独立的数据集合;
- 常见数据库1.SQL 2.My SQL 3.Oracle
- 关系型数据库使用数据表管理和存储数据 1) 创建新的数据库 2) 创建新的数据库表
- SQL语句
1>>>- 数据插入命令(Insert)—————————————————————————
INSERT INTO stuTable(name, age) VALUES (‘张三’,’22’)
1) insert into …… values ……–关键字
2) stuTable–表名
3) name和age–表中的字段名称
4) ‘张三’和’22’–要插入的数据
2>>>- 数据更新命令(Update)————————————————————————–
UPDATE stuTable SET name = ‘马云’,age = ‘33’ WHERE name = ‘张三’
1) update …… set ……where……–关键字
2) stuTable–表名
3) name和age–表中的字段名称
4) ‘马云’和’33’–要更新的数据内容
5) ‘张三’–要更新的数据检索条件
3>>>- 数据删除命令(Delete)—————————————————————————
DELETE FROM stuTable WHERE name = ‘张阳阳’
1) delete from ……where……–关键字
2) stuTable–表名
3) name—————表中的字段名称
4) ‘张阳阳’–要删除的数据检索条件
4>>>- 数据查询命令(Select)—————————————————————————
//select * from stuTable 所有学生信息
//select name from stuTable 所有姓名
//select (name || age) from stuTable 所有姓名和年龄
SELECT * FROM stuTable WHERE name = ‘张阳阳’
1) select from ……where……–关键字
2) stuTable–表名
3) name–表中的字段名称
4) ‘张阳阳’–要查询的数据检索条件 - iOS的数据库技术的实现 , SQLite嵌入式数据库
1) 支持事件,不需要配置,不需要安装,不需要管理员;
2) 支持大部分SQL92;
3) 完整的数据库保存在磁盘上⾯一个文件,同一个数据库文件可以在不同机器上面使用,最大支持数据库到2T
4) 整个系统少于3万行代码,少于250KB的内存占用;
5) 源代码开发,代码95%有较好的注释,简单易用的API; - 开始使用SQLite : Linux系统级的SQLite技术实现框架libsqlite3.0.dylib 1.引入系统sqlite3.h头⽂文件 2.打开数据库 3.执⾏行SQL命令 4.关闭数据库
- SQLite常用系列函数 1.sqlite3_exec() 2.sqlite3_prepare_v2(),sqlite3_step(),sqlite3_finalize() 3.sqlite3_bind() 4.sqlite3_column()
- 简单SQL语句
create table if not exists stu(number integer primary key autoincrement,
name text, sex text, age integer, hobby text)
insert into stu(name,sex,hobby,age) values ('张三','男','学习',22)
update stu set name = '马云', age= '33', hobby = '电商', sex = 男' where name = '张三'
delete from stu where name = '张阳阳'
在语句里*是通配符,通过*相当于代替了表里的所有的字段名
select * from stu
select name from stu
select name,age from stu
select * from stu where name = '张阳阳'
2.简单运用
Student文件
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *sex;
@property(nonatomic,copy)NSString *hobby;
@property(nonatomic,assign)NSInteger age;
- (instancetype)initWithName:(NSString *)name sex:(NSString *)sex hobby:(NSString *)hobby age:(NSInteger)age;
+ (instancetype)studentWithName:(NSString *)name sex:(NSString *)sex hobby:(NSString *)hobby age:(NSInteger)age;
@end
#import "Student.h"
@implementation Student
- (void)dealloc{
[_name release];
[_hobby release];
[_sex release];
[super dealloc];
}
- (instancetype)initWithName:(NSString *)name sex:(NSString *)sex hobby:(NSString *)hobby age:(NSInteger)age{
self = [super init];
if (self) {
self.name = name;
self.sex = sex;
self.hobby = hobby;
_age = age;
}
return self;
}
+ (instancetype)studentWithName:(NSString *)name sex:(NSString *)sex hobby:(NSString *)hobby age:(NSInteger)age{
Student *student = [[Student alloc] initWithName:name sex:sex hobby:hobby age:age];
return [student autorelease];
}
- (NSString *)description{
return [NSString stringWithFormat:@"name = %@, age = %ld, sex = %@, hobby = %@", _name,_age,_sex,_hobby];
}
@end
DataBaseTool文件