IOS Foundation库相关知识

IOS 数据库

2017-08-19  本文已影响2人  小暖风

数据库DataBase

就是很多表。

只能存放 NSString,NSNumber,NSData 这三种类型的数据。

黄金组合:

MSSQL + Windows + iis + .net(收费的)

MySQL + Linux + php + Apache (免费的)

轻量级数据库,SQLite3Access

数据库里不能存放c类型,只能存放OC类型,只存放NSString,NSNumber 和data (二进制)数据。例如 存放int行数据的话,要 (int age = 5; NSNumber numberWithInt:age])

终端中:

创建文件夹: mkdir 文件夹名

创建表:

create table 表名(字段一,字段二);

create table USER(uid,name);

create table if not exists USER(uid,name);

自增的表:

create table if not exists WUGONG(wid integer primary key autoincrement,,name,uid);

创建 WUGONG表,sid类型为integer,自增。

插入信息:

insert into 表名(字段一,字段二)values(值一,值二);

insert into USER(uid,name)values(0,'江志磊');

insert into USER values(1,'吕志轩');

insert into USER(uid) values(1);

(在Xcode中如果数据库表的某一项为空时,给它一个空的字符串,不能给空指针,会崩)

删除表:

delete from USER;删除表的所有信息

delete from 表名 where 条件;

delete from USER where uid=1;删除 uid=1的项。 where后面跟的是条件,可以灵活的写。

drop table 表名;删除表。

修改信息:

update 表名 set 字段='新值' where 条件;

update USER set name='吕志轩' where uid=2;

查询表:

查询全部字段

select * from 表名;

select * from USER;

select * from 表名 limit n;拿出前n条信息

select * from USER limit 2;拿出前两条信息

select 字段 from 表名;查询表中该字段的所有信息。

select uid,name from USER;查询 uid和name字段。

条件查询

select * from USER where uid>0;

select * from USER where uid>0 and uid<2;

select 字段from 表名 order by uid;查询并且按照hid进行pai'xu排序

select * from USER order by uid asc;查询表,顺序排序。desc,倒序。

select count(*)from USER;查询个数

select sum(uid) from USER;求 hid的和

多表查询:

select USER.uid,USER.name,WUGONG.name from USER,WUGONG where USER.uid=WUGONG.uid;

NSString * path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data.db"];NSHomeDirectory() stringByAppendingPathComponent: 拼接路径,若NSHomeDirectory()后面没有/,系统会自动加上。

使用数据库:

//先判断数据库是不是有空指针

if (_nameField.text.length < 1 || _scoreField.text.length < 1 || _imageView.image == nil) {

return;

}

//打开数据库

BOOL res = [_db open];

if (res == NO) {

return;

}

//如果表不存在,创建表

res = [_db executeUpdate:@"create table if not exists USER(name,score,image)"];

if (res == NO) {

[_db close];

NSLog(@"创建失败");

return;

}

增;

删;

改;

查;

//关闭数据库

[_db close];

上一篇下一篇

猜你喜欢

热点阅读