原生代码操作SQLite数据库(不使用第三方包)

例子主要实现在添加单词页面sqlite数据库添加单词,单词页面sqlite数据库取数据显示页面

例子使用的xib实现,使用了导航栏Navigation

前期准备就是在AppDelegate.m 里该实现的实现


YcwViewController.h

#import "sqlite3.h"
@interface YcwViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic) IBOutlet UITableView *myTableView;
@end


YcwViewController.m

@interface YcwViewController ()
{
sqlite3 *db;
NSMutableArray *array;
}
@end

@implementation YcwViewController



- (void)viewWillAppear:(BOOL)animated //从添加单词页面返回单词页面时,重新加载myTableView
{
[self.myTableView reloadData];
}

- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"单词";

self.myTableView.delegate = self;
self.myTableView.dataSource = self;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(goToSecondView)];

[self.navigationItem setRightBarButtonItem:rightButton];

array = [NSMutableArray array];

NSLog(@"%@",[self getDBPath]);

NSFileManager *fileMgr = [NSFileManager defaultManager];
if ([fileMgr fileExistsAtPath:[self getDBPath]]==NO) {

NSLog(@"------------------");
//将NSString对象转换成const char * 的C类型数据
const char *dbPath = [[self getDBPath] UTF8String];
//sqlite的策略是如果有该文件就打开,若果没有就创建文件
if (sqlite3_open(dbPath,&db)==sqlITE_OK) {

char *errMsg;
const char *sql_str = "CREATE TABLE Words (danci VARCHAR,chinese VARCHAR)";
//errMsg传的是地址,因为该函数通过地址引用来写报错字符信息
if (sqlite3_exec(db,sql_str,NULL,&errMsg)) {
NSLog(@"fail to create db");
}
sqlite3_close(db);

}
else{
NSLog(@"Failed to open/create database");
}


}

[self initializeDataToDisplay];


}

- (void)initializeDataToDisplay //从数据库中取数据
{
const char *dbPath = [[self getDBPath] UTF8String];
sqlite3_stmt *statement;
if(sqlite3_open(dbPath,&db)==sqlITE_OK)
{
NSString *querysql = [NSString stringWithFormat:@"SELECT * FROM Words"];

const char *query_stmt = [querysql UTF8String];

if (sqlite3_prepare_v2(db,query_stmt,-1,&statement,NULL)==sqlITE_OK) {
while (sqlite3_step(statement)== sqlITE_ROW) { //一行一行的读取查询得到的数据
Word *word = [[Word alloc]init];
word.danci = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,0)];
word.chinese = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,1)];

[array addObject:word];

}
}else{
NSLog(@"%s",sqlite3_errmsg(db));
}

sqlite3_finalize(statement);
}

}


- (NSString *)getDBPath //数据库的存储路径
{
NSString *string = [NSString stringWithFormat:@"%@/Documents/db2.sqlite",NSHomeDirectory()];

return string;
}

- (void)goToSecondView
{
YcwSecondViewController *secondView = [[YcwSecondViewController alloc]initWithNibName:@"YcwSecondViewController" bundle:nil];

secondView.array = array;

[self.navigationController pushViewController:secondView animated:YES];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return [array count];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIden = @"myCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden];

if (cell==nil) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIden];


}
Word *word = (Word *)array[indexPath.row]; //另外申明的一个Word类,方便存储数据

cell.textLabel.text = word.danci;
cell.detailTextLabel.text = word.chinese;

return cell;


}


YcwSecondViewController.h文件
#import "sqlite3.h"
@interface YcwSecondViewController : UIViewController
@property (strong,nonatomic) IBOutlet UITextField *danciTextfield;
@property (strong,nonatomic) IBOutlet UITextField *chineseTextField;
- (IBAction)addWord:(id)sender;

@property NSMutableArray *array;
@end

YcwSecondViewController.m文件

@interface YcwSecondViewController () { sqlite3 *db; } @end @implementation YcwSecondViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"添加单词"; NSLog(@"%@",self.array); } - (IBAction)addWord:(id)sender { char *errMsg; const char *dbPath = [[self getDBPath] UTF8String]; if (sqlite3_open(dbPath,&db)== sqlITE_OK) { NSString *insertsql = [NSString stringWithFormat:@"INSERT INTO Words values('%@','%@')",self.danciTextfield.text,self.chineseTextField.text]; const char *insert_stmt = [insertsql UTF8String]; if (sqlite3_exec(db,insert_stmt,&errMsg)==sqlITE_OK) { Word *word = [[Word alloc]init]; word.danci = self.danciTextfield.text; word.chinese = self.chineseTextField.text; [self.array addObject:word]; self.danciTextfield.text = @""; self.chineseTextField.text = @""; NSLog(@"添加成功"); [self.danciTextfield becomeFirstResponder]; NSLog(@"%@",self.array); }else{ NSLog(@"插入数据错误%s",errMsg); sqlite3_free(errMsg); } sqlite3_close(db); } } - (NSString *)getDBPath { NSString *string = [NSString stringWithFormat:@"%@/Documents/db2.sqlite",NSHomeDirectory()]; return string; } @end

相关文章

安装 在Windows上安装SQLite。 访问官网下载下Precompliled Binaries for Windows的两个压缩包。 创建s...
一、安装 下载地址:http://www.sqlite.org/download.html 将Precompiled Binaries for Windows下的包下...
实例: 会员信息管理 功能:1.查看数据库 2.清空数据库 3.增加会员 4.删除会员 5.更新会员 6.查找会员  ...
关于SQLite SQLite是一个轻量的、跨平台的、开源的数据库引擎,它的在读写效率、消耗总量、延迟时间和整...