您的位置:首页 > 数据库

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: