美文网首页
sqlite数据库

sqlite数据库

作者: heigo | 来源:发表于2017-01-10 00:08 被阅读0次

1、建立连接数据库

存储类型

integer:整型值

real:浮点值

text:文本字符串

blob:二进制数据(比如文件)

实际上SQLite是无类型的

2、DDL

创建表

create table"t_product"("pid"integer,"pName"text,"pPrice"real);

IF NOT EXISTS

数据库执行下面字符串,可创建表

@" create table if not exists t_illness (id integer primary key autoincrement,illnessType text,illnessDetail text,pinyin text,py text);delete from t_illness ;";

删除表使用的时候要注意不要瞎删

drop table"t_product"

3、DML

(1)insert插数据

insert into"t_student"(id,name,age,score) values (3,'基佬',17,2.0);

insert into t_student  values(9999,'jim3',11,222)

@"insert into t_person (pName,pAge) values ('%@',%d)"

如果需要往所有字段添加数据,那么可以省略字段名

(2)更新数据所有age和score都变成90和20

update"t_student"set score =90,age =20

指定条件

@"update t_person set pName='%@',pAge =%d where pid = %d"

update"t_student"set score =1001where age =20

update"t_student"set age =5where age >10and name!='jack'

(3)删除表的内容

delete from"t_student"

注意这样写把表中所有的数据都删光了

--删除某行数据

@"delete from t_person where pid=%d"

delete from"t_student"whereid=2

delete from"t_student"where age <=10or age>30

(4)查询

@"select * from t_person"

模糊搜索

@"select * from t_doctor where (doctor_name like '%%%@%%') or (doctor_title_name like '%%%@%%') or (doctor_hospital_name like '%%%@%%') or (pinyin like '%%%@%%') or (py like '%%%@%%') ",string,string,string,string,string]];

4、起别名

格式(字段和表都可以起别名)

select字段1别名,字段2别名,…from表名别名;

select字段1别名,字段2as别名,…from表名as别名;

select别名.字段1,别名.字段2, …from表名别名;

5、计算记录的数量

selectcount(字段)from表名;

selectcount(*)from表名;

6、排序

select*fromt_studentorder byagedesc;//降序

select*fromt_studentorder byageasc;//升序(默认)

select*fromt_studentorderbyageasc,heightdesc;

先按照年龄排序(升序),年龄相等就按照身高排序(降序

7、limit跳过最前面4条语句,然后取8条记录

select*from表名limit数值1,数值2;

select*fromt_studentlimit7;

相当于select*fromt_studentlimit0,7;

8、简单约束

not null:规定字段的值不能为null

unique:规定字段的值必须唯一

default:指定字段的默认值

createtablet_student(idinteger, nametextnotnullunique,ageintegernotnulldefault1);

name字段不能为null,并且唯一

age字段不能为null,并且默认为1

主键(PrimaryKey,简称PK)用来唯一地标识某一条记录

例如t_student可以增加一个id字段作为主键,相当于人的身份证

createtablet_student(idintegerprimary key,nametext,ageinteger);

//id" integer DEFAULT 1 PRIMARY KEY AUTOINCREMENT

只要声明为primarykey,就说明是一个主键字段

主键字段默认就包含了notnull和unique两个约束

如果想要让主键自动增长(必须是integer类型),应该增加autoincrement

外键约束

利用外键约束可以用来建立表与表之间的联系

外键的一般情况是:一张表的某个字段,引用着另一张表的主键字段

CONSTRAINT "class" FOREIGN KEY ("className") REFERENCES "t_className" ("clsID")

constraint

9、多表查询(面试一定会问)

1.嵌套查询

select className from t_className where clsID=(select className from t_student where name = 'xx')

2.自然连接如果数据量很大连接的时候耗费性能

select clsName from t_classroom ,t_student where classRoomid = clsID and name ='jack

10、sqlite3.h创建,yymodel

#import

(1)NSString*cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject];

NSString*dbFilePath = [cachePathstringByAppendingPathComponent:@"demo.db"];

创建数据库并打开1.db文件的路径2.sqlite3输出参数数据库句柄(对象)

(2)//通过读取本地的sql文件导入表结构

NSString*sqlFilePath = [[NSBundlemainBundle]pathForResource:@"t_person.sql"ofType:nil];

NSString*str = [NSStringstringWithContentsOfFile:sqlFilePathencoding:NSUTF8StringEncodingerror:nil];

执行sql,,,传入sql并调用这个可以增删改数据库,,一开始调用sql可以导入表格

sqlite3*,                                   An open databas个已经打开的数据库

// const char *sql,                           /* SQL to be evaluated sql语句

// int (*callback)(void*,int,char**,char**),  /* Callback function回调*/

// void *,                                    /* 1st argument to callback第一个参数*/

// char **errmsg                              /* Error msg written here错误*/

if(sqlite3_exec(_db, sql.UTF8String,NULL,NULL,NULL) ==SQLITE_OK) {

表的内容如下

IF NOT EXISTS   如果不存在,则创建

CREATE TABLEIF NOT EXISTS"t_person" (

"pid" integer DEFAULT 1 PRIMARY KEY AUTOINCREMENT,

"pName" text,

"pAge" integer

);

(3)执行代码

if(sqlite3_exec(_db, sql.UTF8String,NULL,NULL,NULL) ==SQLITE_OK) {

returnYES;

};

(4)查

思路:先通过预编译sqlite3_prepare_v2获取sqlite3_stmt,进行循环判断sqlite3_step(stmt) ==SQLITE_ROW,获取sqlite3_column_count(stmt)列的个数,循环获取列对应的名字和值,sqlite3_column_name(stmt, i)获取对应第i列的名字,sqlite3_column_type(stmt, i)获取第i列对应的值得类型,sqlite3_column_int(stmt, i);根据类型获取值,mdic[colNameStr] =@(colValue);以这个形式加入到可变数组中

//预编译sql

1.数据库的句柄2.要编译的语句3.sql的长度4.输出参数预编译后的对象5.指针指向sql中没有使用的部分

sqlite3_stmt*_stmt;NSArray*array;

if(sqlite3_prepare_v2(_db, sqlString.UTF8String, -1, &_stmt,NULL) ==SQLITE_OK) {

//根据预编译的sql获取执行后的结果集合

- (NSArray*)getRecordsWithStmt:(sqlite3_stmt*)stmt {

NSMutableArray*mArray = [NSMutableArrayarray];

获取key(列名)对应的值

//获取下一行的数据返回是否还有数据

while(sqlite3_step(stmt) ==SQLITE_ROW) {

//获取该行的信息    一行就是一个model,即一个字典,,列是键值对对应的值

NSMutableDictionary*mDic = [NSMutableDictionarydictionary];

//获取数据集合有几列

intcolCount =sqlite3_column_count(stmt);

for(inti =0; i < colCount; i++) {

//取列名字作为key

constchar*colNameChar =sqlite3_column_name(stmt, i);

//转换为nsstring

NSString*colNameStr = [NSStringstringWithUTF8String:colNameChar];

//获取数据类型

intcoltype =sqlite3_column_type(stmt, i);

switch(coltype) {

caseSQLITE_INTEGER: {

intcolValue =sqlite3_column_int(stmt, i);

mDic[colNameStr] =@(colValue);

}

break;

caseSQLITE_TEXT: {

constchar* colValue =(constchar*)sqlite3_column_text(stmt, i);

mDic[colNameStr] = [NSStringstringWithUTF8String:colValue];

}break;

caseSQLITE_FLOAT: {

doublecolValue =sqlite3_column_double(stmt, i);

mDic[colNameStr] =@(colValue);

}break;

default:

break;

}

}

[mArrayaddObject:mDic.copy];

}

11、//字典数组转模型数组

1.数组中的元素类型

2.数据源

[NSArrayyy_modelArrayWithClass:[selfclass]json:dicArray];

12、创建模型,json直接转模型

HMPersonModel*person = [HMPersonModelyy_modelWithJSON:jsonStr];

相关文章

  • SQLite 创建数据库

    SQLite 创建数据库 SQLite 的 sqlite3 命令被用来创建新的 SQLite 数据库。您不需要任何...

  • 第四篇:sqlite数据库与FMDB

    目录一、sqlite数据库二、FMDB 一、sqlite数据库 sqlite数据库是基于C实现的、移动端开发常用的...

  • Pycharm中连接数据库sqlite

    django 有个数据库 sqlite sqlite是小型关系数据库

  • Python数据分析基础----第二十二天

    数据库 Python内置的sqlite3模块 import sqlite3 创建sqlite3内存数据库 创建带有...

  • Sqlite 使用笔记

    1. 数据库基本操作封装 sqlite 数据操作 语句类型 sqlite 数据库的简单实用- 导入sqlite3数...

  • 我的电脑上装了些什么软件

    开发 Sqlite 工具 DB Browser for SQLite 专门查看sqlite数据库使用,比较方...

  • Android数据库

    一、SQLite 1、SQLite介绍 1.1、简介 SQLite是一款轻型的数据库,是遵守ACID的关联式数据库...

  • android笔记6

    本章学习目标: 了解SQLite数据库的特点和体系结构 掌握SQLite数据库的建立和操作方法 SQlite介绍 ...

  • sqlite基础

    SQLite 什么是SQLite SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在...

  • 四种数据存储方式(下)

    3.sqlite3 打开数据库: sqlite3 *database; int result = sqlite3_...

网友评论

      本文标题:sqlite数据库

      本文链接:https://www.haomeiwen.com/subject/wsfkbttx.html