sqlite3学习
2015-10-14 14:15
288 查看
</pre><pre name="code" class="objc">
</pre><pre name="code" class="objc">
</pre><pre name="code" class="objc">#import "ViewController.h" #import "sqlite3.h" @interface ViewController () - (IBAction)insert:(id)sender; - (IBAction)delete:(id)sender; - (IBAction)update:(id)sender; @property (nonatomic,assign) sqlite3 *db; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // NSString *name = [NSString stringWithFormat:@""]; //NSString *sql = [NSString stringWithFormat:@"insert into t_class (name) valus] // Do any additional setup after loading the view, typically from a nib. NSString *path = NSHomeDirectory();//主目录 NSLog(@"NSHomeDirectory:%@",path); NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSString *filePath = [cachePath stringByAppendingString:@"student.sqlite"]; sqlite3 *db = nil; if (sqlite3_open(filePath.UTF8String,&db)== SQLITE_OK) { NSLog(@"success"); }else{ NSLog(@"failed"); } _db = db; NSString *sql = @"create table t_makeup (id integer primary key autoincrement,name text)"; [self execWithsql:sql]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)insert:(id)sender { NSString *sql = @"insert into t_makeup (name) values ('zhangsan');"; [self execWithsql:sql]; } - (IBAction)delete:(id)sender { NSString *sql = @"delete from t_makeup"; [self execWithsql:sql]; } - (IBAction)update:(id)sender { NSString *sql = @"update t_makeup set name = 'zhangsan'"; [self execWithsql:sql]; } - (IBAction)select:(id)sender { NSString *sql = @"select * from t_makeup"; sqlite3_stmt *stmt; if(sqlite3_prepare_v2(_db, sql.UTF8String, -1, &stmt, NULL)==SQLITE_OK) { NSLog(@"success"); if (sqlite3_step(stmt) == SQLITE_ROW) { NSString *name = [NSString stringWithUTF8String: sqlite3_column_text(stmt, 1) ]; NSLog(@"%@",name); } } } -(void)execWithsql:(NSString *)sql//抽取的方法 { char *errmsg; sqlite3_exec(_db, sql.UTF8String, NULL, NULL, &errmsg); if (errmsg) { NSLog(@"failed"); }else { NSLog(@"success"); } } @end
以上是基本的在xcode中实现sqlite的insert create update delete 等基本操作。
PS 今天遇到的几个小问题:
1,xcode nslog打印不出来是因为没把控制台调出来orz。。
解决方案:command shift +c
2,数据库无法同步问题
解决方案:不要写table。。直接上表名
3.db赋值为0 必须将_db = db写在创建了表的代码之后!!!
</pre><pre name="code" class="objc"> sqlite3 *db = nil; if (sqlite3_open(filePath.UTF8String,&db)== SQLITE_OK) { NSLog(@"success"); }else{ NSLog(@"failed"); } _db = db;
相关文章推荐
- MySQL查看、创建和删除索引的方法
- Redis主从复制和主从切换
- SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)
- oracle数据库之间的导入、导出,复制表数据,复制表结构 telnet 192.168.88.205 1521
- 数据库查询行转列 PIVOT
- mysql排名实现
- dede织梦调用顶级二级栏目及下三级栏目方法(数据库实
- ODBC与ADO 连SQL Server 2005
- SQLServer 表字段为 NULL 而视图为NOT NULL 问题
- mysql数据迁移
- MySQL配置笔记
- MSSQL, MYSQL 字符串拆分
- MYSQL 字符串函数
- C#实现对SQL数据库中的表的查询、添加、修改、删除数据
- MySQL关键字(保留字)汇总
- php 安装redis扩展 for window
- 数据库初始化
- Oracle的配置问题
- PowerDesigner逆向工程导入MYSQL数据库总结
- window 下安装redis