sqlite3 OC 提供的方法
2015-11-20 10:09
288 查看
#import "HMViewController.h"
#import <sqlite3.h>
#import "HMShop.h"
@interface
HMViewController () <UITableViewDataSource,UISearchBarDelegate>
@property (weak,
nonatomic) IBOutletUITextField *nameField;
@property (weak,
nonatomic) IBOutletUITextField *priceField;
/** 数据库对象实例 */
@property (nonatomic,assign)
sqlite3 *db;
@property (weak,
nonatomic) IBOutletUITableView *tableView;
- (IBAction)insert;
@property (nonatomic,strong)
NSMutableArray *shops;
@end
@implementation HMViewController
- (NSMutableArray *)shops
{
if (!_shops) {
self.shops = [[NSMutableArrayalloc]
init];
}
return _shops;
}
- (<
15e3d
span style="color:#bb2ca2;">void)viewDidLoad
{
[superviewDidLoad];
//
增加搜索框
UISearchBar *searchBar = [[UISearchBaralloc]
init];
searchBar.frame =
CGRectMake(0,
0, 320, 44);
searchBar.delegate =
self;
self.tableView.tableHeaderView = searchBar;
//
初始化数据库
[self setupDb];
//
查询数据
[selfsetupData];
//
关闭数据库
// sqlite3_close();
}
#pragma mark - UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self.shopsremoveAllObjects];
NSString *sql = [NSStringstringWithFormat:@"SELECT
name,price FROM t_shop WHERE name LIKE '%%%@%%' OR price LIKE '%%%@%%' ;", searchText, searchText];
// stmt是用来取出查询结果的
sqlite3_stmt *stmt =
NULL;
//
准备
int status = sqlite3_prepare_v2(self.db, sql.UTF8String, -1,
&stmt, NULL);
if (status ==
SQLITE_OK) { // 准备成功 -- SQL语句正确
while (sqlite3_step(stmt) ==SQLITE_ROW) {
// 成功取出一条数据
const
char *name = (const
char *)sqlite3_column_text(stmt,
0);
const
char *price = (const
char *)sqlite3_column_text(stmt,
1);
HMShop *shop = [[HMShopalloc]
init];
shop.name = [NSStringstringWithUTF8String:name];
shop.price = [NSStringstringWithUTF8String:price];
[self.shopsaddObject:shop];
}
}
[self.tableViewreloadData];
}
/**
查询数据
*/
- (void)setupData
{
constchar *sql ="SELECT name,price FROM t_shop;";
// stmt是用来取出查询结果的
sqlite3_stmt *stmt =
NULL;
//
准备
int status = sqlite3_prepare_v2(self.db, sql, -1, &stmt,NULL);
if (status ==
SQLITE_OK) { // 准备成功 -- SQL语句正确
while (sqlite3_step(stmt) ==SQLITE_ROW) {
// 成功取出一条数据
const
char *name = (const
char *)sqlite3_column_text(stmt,
0);
const
char *price = (const
char *)sqlite3_column_text(stmt,
1);
HMShop *shop = [[HMShopalloc]
init];
shop.name = [NSStringstringWithUTF8String:name];
shop.price = [NSStringstringWithUTF8String:price];
[self.shopsaddObject:shop];
}
}
}
/**
初始化数据库
*/
- (void)setupDb
{
// 打开数据库(连接数据库)
NSString *filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES)lastObject]
stringByAppendingPathComponent:@"shops.sqlite"];
// 如果数据库文件不存在,系统会自动创建文件自动初始化数据库
int status = sqlite3_open(filename.UTF8String, &_db);
if (status ==
SQLITE_OK) { // 打开成功
NSLog(@"打开数据库成功");
// 创表
constchar *sql ="CREATE TABLE IF NOT EXISTS t_shop (id integer PRIMARY KEY,
name text NOT NULL, price real);";
char *errmsg =
NULL;
sqlite3_exec(self.db, sql,NULL,
NULL, &errmsg);
if (errmsg) {
NSLog(@"创表失败--%s", errmsg);
}
} else { //
打开失败
NSLog(@"打开数据库失败");
}
}
- (IBAction)insert {
NSString *sql = [NSStringstringWithFormat:@"INSERT
INTO t_shop(name, price) VALUES ('%@', %f);",self.nameField.text,self.priceField.text.doubleValue];
sqlite3_exec(self.db, sql.UTF8String,NULL,
NULL,NULL);
//
刷新表格
HMShop *shop = [[HMShopalloc]
init];
shop.name = self.nameField.text;
shop.price = self.priceField.text;
[self.shopsaddObject:shop];
[self.tableViewreloadData];
}
#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return
self.shops.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID =@"shop";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:ID];
cell.backgroundColor = [UIColorgrayColor];
}
HMShop *shop =
self.shops[indexPath.row];
cell.textLabel.text = shop.name;
cell.detailTextLabel.text = shop.price;
return cell;
}
#import <sqlite3.h>
#import "HMShop.h"
@interface
HMViewController () <UITableViewDataSource,UISearchBarDelegate>
@property (weak,
nonatomic) IBOutletUITextField *nameField;
@property (weak,
nonatomic) IBOutletUITextField *priceField;
/** 数据库对象实例 */
@property (nonatomic,assign)
sqlite3 *db;
@property (weak,
nonatomic) IBOutletUITableView *tableView;
- (IBAction)insert;
@property (nonatomic,strong)
NSMutableArray *shops;
@end
@implementation HMViewController
- (NSMutableArray *)shops
{
if (!_shops) {
self.shops = [[NSMutableArrayalloc]
init];
}
return _shops;
}
- (<
15e3d
span style="color:#bb2ca2;">void)viewDidLoad
{
[superviewDidLoad];
//
增加搜索框
UISearchBar *searchBar = [[UISearchBaralloc]
init];
searchBar.frame =
CGRectMake(0,
0, 320, 44);
searchBar.delegate =
self;
self.tableView.tableHeaderView = searchBar;
//
初始化数据库
[self setupDb];
//
查询数据
[selfsetupData];
//
关闭数据库
// sqlite3_close();
}
#pragma mark - UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self.shopsremoveAllObjects];
NSString *sql = [NSStringstringWithFormat:@"SELECT
name,price FROM t_shop WHERE name LIKE '%%%@%%' OR price LIKE '%%%@%%' ;", searchText, searchText];
// stmt是用来取出查询结果的
sqlite3_stmt *stmt =
NULL;
//
准备
int status = sqlite3_prepare_v2(self.db, sql.UTF8String, -1,
&stmt, NULL);
if (status ==
SQLITE_OK) { // 准备成功 -- SQL语句正确
while (sqlite3_step(stmt) ==SQLITE_ROW) {
// 成功取出一条数据
const
char *name = (const
char *)sqlite3_column_text(stmt,
0);
const
char *price = (const
char *)sqlite3_column_text(stmt,
1);
HMShop *shop = [[HMShopalloc]
init];
shop.name = [NSStringstringWithUTF8String:name];
shop.price = [NSStringstringWithUTF8String:price];
[self.shopsaddObject:shop];
}
}
[self.tableViewreloadData];
}
/**
查询数据
*/
- (void)setupData
{
constchar *sql ="SELECT name,price FROM t_shop;";
// stmt是用来取出查询结果的
sqlite3_stmt *stmt =
NULL;
//
准备
int status = sqlite3_prepare_v2(self.db, sql, -1, &stmt,NULL);
if (status ==
SQLITE_OK) { // 准备成功 -- SQL语句正确
while (sqlite3_step(stmt) ==SQLITE_ROW) {
// 成功取出一条数据
const
char *name = (const
char *)sqlite3_column_text(stmt,
0);
const
char *price = (const
char *)sqlite3_column_text(stmt,
1);
HMShop *shop = [[HMShopalloc]
init];
shop.name = [NSStringstringWithUTF8String:name];
shop.price = [NSStringstringWithUTF8String:price];
[self.shopsaddObject:shop];
}
}
}
/**
初始化数据库
*/
- (void)setupDb
{
// 打开数据库(连接数据库)
NSString *filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES)lastObject]
stringByAppendingPathComponent:@"shops.sqlite"];
// 如果数据库文件不存在,系统会自动创建文件自动初始化数据库
int status = sqlite3_open(filename.UTF8String, &_db);
if (status ==
SQLITE_OK) { // 打开成功
NSLog(@"打开数据库成功");
// 创表
constchar *sql ="CREATE TABLE IF NOT EXISTS t_shop (id integer PRIMARY KEY,
name text NOT NULL, price real);";
char *errmsg =
NULL;
sqlite3_exec(self.db, sql,NULL,
NULL, &errmsg);
if (errmsg) {
NSLog(@"创表失败--%s", errmsg);
}
} else { //
打开失败
NSLog(@"打开数据库失败");
}
}
- (IBAction)insert {
NSString *sql = [NSStringstringWithFormat:@"INSERT
INTO t_shop(name, price) VALUES ('%@', %f);",self.nameField.text,self.priceField.text.doubleValue];
sqlite3_exec(self.db, sql.UTF8String,NULL,
NULL,NULL);
//
刷新表格
HMShop *shop = [[HMShopalloc]
init];
shop.name = self.nameField.text;
shop.price = self.priceField.text;
[self.shopsaddObject:shop];
[self.tableViewreloadData];
}
#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return
self.shops.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID =@"shop";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:ID];
cell.backgroundColor = [UIColorgrayColor];
}
HMShop *shop =
self.shops[indexPath.row];
cell.textLabel.text = shop.name;
cell.detailTextLabel.text = shop.price;
return cell;
}
相关文章推荐
- SQL_多条记录中取最大日期的记录
- MySQL求差集
- oracle 备份与还原 exp/imp
- pid及socket文件丢失,数据库无法启动问题
- mysql delete删除记录数据库空间不减少问题解决方法(优化表)
- MySQL升5.6引发的问题
- 一千行MySQL学习笔记
- Oracle执行计划不走索引的原因总结
- 数据库某些要注意的问题(转的,侵权删除)
- MySQL5.6新特性之GTID、多线程复制 - 不知为何
- 看了此文,Oracle SQL优化文章不必再看(转)
- 正确修改MySQL最大连接数的三种好用方案
- 跨平台轻量级redis、ssdb代理服务器(C++ 11编写)
- Spring+Mybatis多数据源配置(一)——MySQL与Oracle通过配置切换
- Spring+Mybatis多数据源配置(一)——MySQL与Oracle通过配置切换
- Oracle 数据库日常巡检
- MYSQL 慢日志
- Sqlite语句和CoreData
- 15天玩转redis —— 第六篇 有序集合类型
- excel文件(xls)批量导入sqlserver数据库