SqlLite 基本操作
1.数据类型
● SQLite将数据划分为以下⼏几种存储类型:
● integer : 整型值
● real : 浮点值
● text : ⽂文本字符串
● blob : ⼆二进制数据(⽐比如⽂文件)
● 实际上SQLite是⽆无类型的
● 就算声明为integer类型,还是能存储字符串⽂文本(主键除外)
● 建表时声明啥类型或者不声明类型都可以,也就意味着创表语句可以这么写:
• create table t_student(name, age);
#import "ViewController.h"
#import <sqlite3.h>
@interface ViewController ()
//1.创建数据库 (保存路径)
@property (nonatomic,assign)sqlite3 *db;
@end
@implementation ViewController
- (IBAction)insertData:(UIButton *)sender {
//3.增加 数据 (100条 数据随机)
for (int i = 0; i <2; i++) {
NSString *strName = [NSString stringWithFormat:@"8mingyeuxin-%d",i];
NSString *sqlStr = [NSString stringWithFormat:@"INSERT INTO t_student (name ,score)VALUES('%@',%.02f)",strName,arc4random_uniform(1000)/10.0];
//执行
int success = sqlite3_exec(_db, sqlStr.UTF8String, NULL, NULL, NULL);
if (success == SQLITE_OK) {
NSLog(@"添加成功!");
}else{
NSLog(@"添加失败!");
}
}
}
- (IBAction)deleteData:(UIButton *)sender {
//4.删除 数据 (70 - 80 分数)
NSString *sqlStr = @"DELETE FROM t_student WHERE score > 80.0";
//执行
int success = sqlite3_exec(_db, sqlStr.UTF8String, NULL, NULL, NULL);
if (success == SQLITE_OK) {
NSLog(@"删除成功!");
}else{
NSLog(@"删除失败!");
}
}
- (IBAction)updateData:(UIButton *)sender {
//5.修改 数据 (修改分数小于60.0为60.0)
NSString *sqlStr = @"UPDATE t_student SET score = 60.0 WHERE score < 60.0";
//执行
int success = sqlite3_exec(_db, sqlStr.UTF8String, NULL, NULL, NULL);
if (success == SQLITE_OK) {
NSLog(@"修改成功!");
}else{
NSLog(@"修改失败!");
}
}
- (IBAction)selectData:(UIButton *)sender {
//6.查询数据 ( score >= 60)
//NSString *sqlStr = @"SELECT * FROM t_student WHERE score > 60.0 ORDER BY score DESC;";
//查询数据 ( name 中带 8 数字)
NSString *sqlStr = @"SELECT * FROM t_student WHERE name LIKE '%ming%'";
//查询之后 把结果放在 stmt对象
// 保存所有的结果集
sqlite3_stmt *stmt = nil;
sqlite3_prepare_v2(_db, sqlStr.UTF8String, -1, &stmt, NULL);
//获取到所有结果 每一步 查询到一条记录
while (sqlite3_step(stmt) == SQLITE_ROW) {
//取出一条记录
// name TEXT column
const unsigned char * name = sqlite3_column_text(stmt, 1);
NSString *strName = [NSString stringWithCString:(const char *)name encoding:NSUTF8StringEncoding];
//score REAL
double score = sqlite3_column_double(stmt, 2);
NSLog(@"name = %@ score = %f",strName,score);
}
}
- (void)viewDidLoad {
[super viewDidLoad];
//打开数据库 如果没有就创建
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"data.sqlite"];
NSLog(@"%@",path);
int success = sqlite3_open(path.UTF8String, &_db);
if (success == SQLITE_OK) {
NSLog(@"创建数据库成功!");
//2.创建表 (指定字段 -> 需求: 保存 学生信息 id name score)
//用sql语句 来创建
//编写sql语句
NSString *str = @"CREATE TABLE IF NOT EXISTS t_student (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, score REAL NOT NULL)";
//执行sql语句
int success_t = sqlite3_exec(_db, str.UTF8String, NULL, NULL, NULL);
if (success_t == SQLITE_OK) {
NSLog(@"创建表成功!");
}else{
NSLog(@"创建表失败!");
}
}else{
NSLog(@"创建数据库失败!");
}
//关闭数据库
sqlite3_close(_db);
}
@end
SqlLite 基本操作的更多相关文章
- Python操作SQLLite(基本操作)
SQLite 是一个软件库,实现了自给自足的.无服务器的.零配置的.事务性的 SQL 数据库引擎.SQLite 是在世界上最广泛部署的 SQL 数据库引擎.SQLite 源代码不受版权限制. Pyth ...
- crawler碎碎念4 关于python requests、Beautiful Soup库、SQLlite的基本操作
Requests import requests from PIL import Image from io improt BytesTO import jason url = "..... ...
- Django/MySql数据库基本操作&ORM操作
数据库配置: #第一步在settings里面 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME':'dbna ...
- Key/Value之王Memcached初探:二、Memcached在.Net中的基本操作
一.Memcached ClientLib For .Net 首先,不得不说,许多语言都实现了连接Memcached的客户端,其中以Perl.PHP为主. 仅仅memcached网站上列出的语言就有: ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
- 三、Redis基本操作——List
小喵的唠叨话:前面我们介绍了Redis的string的数据结构的原理和操作.当时我们提到Redis的键值对不仅仅是字符串.而这次我们就要介绍Redis的第二个数据结构了,List(链表).由于List ...
- 二、Redis基本操作——String(实战篇)
小喵万万没想到,上一篇博客,居然已经被阅读600次了!!!让小喵感觉压力颇大.万一有写错的地方,岂不是会误导很多筒子们.所以,恳请大家,如果看到小喵的博客有什么不对的地方,请尽快指正!谢谢! 小喵的唠 ...
- 一、Redis基本操作——String(原理篇)
小喵的唠叨话:最近京东图书大减价,小喵手痒了就买了本<Redis设计与实现>[1]来看看.这里权当小喵看书的笔记啦.这一系列的模式,主要是先介绍Redis的实现原理(可能很大一部分会直接照 ...
随机推荐
- 百度编辑器ueditor插入表格没有边框颜色的解决方法
附:从word excel 中 复制的表格提交后无边框,参考这个同学的,写的很详细: http://blog.csdn.net/lovelyelfpop/article/details/51678 ...
- sublime 中 pyv8 binary 报错怎么处理?
,sublime text 在安装插件的时候,尤其是Emmet的时候,经常会报一个错误--please wait a bit whilePyV8 binary is being downloaded, ...
- sql 批量更新某个字段的值
UPDATE Tabel1 t1 set t1.col1= ( SELECT col2 from Tabel2 t2 WHERE t1.col1=t2.col2) where exists ( SEL ...
- [译]Writing Custom Middleware in ASP.NET Core 1.0
原文: https://www.exceptionnotfound.net/writing-custom-middleware-in-asp-net-core-1-0/ Middleware是ASP. ...
- Xms Xmx PermSize MaxPermSize 区别
Eclipse崩溃,错误提示: MyEclipse has detected that less than 5% of the 64MB of Perm Gen (Non-heap memory) s ...
- [老文章搬家] [翻译] 深入解析win32 crt 调试堆
09 年翻译的东西. 原文见: http://www.nobugs.org/developer/win32/debug_crt_heap.html 在DeviceStudio的Debug编译模式下, ...
- HDU5934 强连通分量
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5934 根据距离关系建边 对于强连通分量来说,只需引爆话费最小的炸弹即可引爆整个强连通分量 将所有的强连通分 ...
- JS数组求最大值和最小值
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Win10搭建Linux开发环境之网络连接设定
一直想在家自己搭建一个LINUX服务器,好在上面安装个ORACLE数据库玩玩. 上次用了Ubuntu,结果ORACLE没装成功,现在换个思路,采用CentOS 7作为Linux服务器, 之后再进行构建 ...
- CentOS7下安装配置MariaDB
参考: http://www.2cto.com/os/201504/394141.html http://outofmemory.cn/code-snippet/2533/mysql-create-d ...