在代码中应用sqlite3
ViewController.h文件
#import <UIKit/UIKit.h>
#import "sqlite3.h"
@interface ViewController : UIViewController
{
sqlite3 * link;
NSString *path;
}
@end
ViewController.m文件:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//把person.db文件的地址给path
path [email protected]"Users/feifanjiaoyu/Desktop/person.db";
//确定好库文件,打开数据库的链接
sqlite3_open([path UTF8String],&link);
//创建
NSString *[email protected]" create * table student(name varcahr(20),age int)";
//执行SQL语句
sqlite3_exec(link, [creat UTF8String],nil,nil,nil);
//插入
NSString *insert = @"insert into student(name,age) values(‘zhangsuiping‘,17)";
sqlite3_exec(link, [insert UTF8String],nil,nil ,nil);
//更新
NSString *[email protected]"update student set age=19 where name=‘zhangsuiping‘";
sqlite3_exec(link, [update UTF8String],nil,nil,nil);
//删除
NSString *[email protected]"delete from student where age=19";
sqlite3_exec(link, [delete UTF8String],nil,nil,nil);
//查询
sqlite3_stmt *assss;
NSString *select = @"select *from student";
//预编译检查SQL语句
sqlite3_prepare_v2(link, [select UTF8String], -1, &assss, nil);
while (sqlite3_step(assss)==SQLITE_ROW)
{
NSLog(@"%s %s",sqlite3_column_text(assss, 0),sqlite3_column_text(assss, 1) );
//---打印每一行纪录的第一列和第二列的值
}
// 关闭
sqlite3_close(link);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end