iOS下 FMDB 简单的代码例子


iOS下 FMDB  简单的代码例子


首先,导入libsqlite3.0.dylib库

 我们,来看一下.h文件


#import <sqlite3.h>
@interface ViewController : UIViewController
@property (assign,nonatomic)sqlite3* database;
-(IBAction)openDB;
-(IBAction)createTestList;
-(IBAction)insertTable;
-(IBAction)queryTable;
-(IBAction)deleteTable;
-(IBAction)updateTable;
@end




//获取document沙箱路径
-(NSString*)dataFilePath{
           
    NSArray *myPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *myDocPath = [myPaths objectAtIndex:0];
    NSString *filename = [myDocPath  stringByAppendingPathComponent:@"sqlite.db"];
    NSLog(@"%@",filename);
    return filename;//这里很神奇,可以定义成任何类型的文件,也可以不定义成.db文件,任何格局都行,定义成.sb文件都行,达到了很好的数据隐秘性
}



//创建,打开数据库
-(IBAction)openDB
{
    //获取数据库路径
    NSString* path = [self dataFilePath];
    //若是数据库存在,则用sqlite3_open直接打开(若是数据库不存在sqlite3_open会主动创建)
    sqlite3_open([path UTF8String], &_database);
}



//创建表
-(IBAction)createTestList
{
                                               
      const char *createSql="create table if not exists testTable(ID INTEGER PRIMARY KEY AUTOINCREMENT, testID int,testValue text,testName text)";
    sqlite3_exec(_database, createSql, NULL, NULL, NULL);
}





//插入数据
-(IBAction)insertTable
{
    const char *insertSql="INSERT INTO testTable(testID, testValue,testName) VALUES(120,'ew','45')";
    sqlite3_exec(_database, insertSql, NULL, NULL, nil);
                                         
}



//查看数据
-(IBAction)queryTable
{
    [self openDB];
    const char *selectSql="select testID,testName from testTable";
    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(_database, selectSql, -1, &statement, nil)==SQLITE_OK)
    {
        NSLog(@"select ok.");
        while (sqlite3_step(statement)==SQLITE_ROW)//SQLITE_OK SQLITE_ROW
        {
            int _id=sqlite3_column_int(statement, 0);
            NSString *name=[[NSString alloc] initWithCString:(char *)sqlite3_column_text(statement, 1) encoding:NSUTF8StringEncoding];
            NSLog(@"row>>id %i, name>> %@",_id,name);
        }
                                         
    }
                                     
}



//删除数据
-(IBAction)deleteTable
{
    [self openDB];
                                 
    const char *sql = "DELETE FROM testTable where testName='45'";
    sqlite3_exec(_database, sql, NULL, NULL, nil);
                              
}



//更改数据
-(IBAction)updateTable
{
                             
    [self openDB];
                             
    const char *sql = "update testTable set testName ='444' WHERE testID =120";
                           
    sqlite3_exec(_database, sql, NULL,NULL,NULL);
     sqlite3_close(_database);
                             
}



最后,在XIB中拖四个按钮与增、删、改、查 四个方法相关联


Hi,推荐文件给你 "Sqlite例子.zip" http://vdisk.weibo.com/s/HqLZq


数据库查看工具: http://vdisk.weibo.com/s/BDn59yfnBVMAJ  


http://pan.baidu.com/share/link?shareid=3059825140&uk=3189484501


本文出自 “7087095” 博客,请务必保留此出处http://7097095.blog.51cto.com/7087095/1229932

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杜甲同学

感谢打赏,我会继续努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值