国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

IOS開發(fā)之?dāng)?shù)據(jù)sqlite使用

2019-11-14 19:28:15
字體:
供稿:網(wǎng)友
一、引入工具包

  引入工具包libsqlite3.dylib,該工具包為C語言工具包。

二、代碼操作數(shù)據(jù)庫

1、創(chuàng)建并且鏈接數(shù)據(jù)庫 

- (void) _connectDB{     //1>獲取沙盒路徑作為數(shù)據(jù)庫創(chuàng)建時(shí)候的初始化路徑    NSString * path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];    path=[path stringByAppendingPathComponent:@"new.sqlite"];    NSLog(@"%@",path);    //2>如果存在則打開當(dāng)前鏈接,如果不存在則創(chuàng)建    if(SQLITE_OK==sqlite3_open(path.UTF8String, &sqlite)){        NSLog(@"數(shù)據(jù)庫創(chuàng)建成功!");    }else {        NSLog(@"數(shù)據(jù)庫創(chuàng)建失敗!");    }    }

2、操作數(shù)據(jù)庫 

/** *  創(chuàng)建表 */- (void) _createTable{    NSString *create=@" create table if not exists t_Person (id integer PRimary key autoincrement,name text,age integer,tel text)";    [self _execSql:create andTip:@"創(chuàng)建表操作"];    }/** *  插入數(shù)據(jù)操作 * *  @param name 姓名 *  @param age  年齡 *  @param tel  電話 */- (void) _insertName:(NSString *) name andAge:(int )age andTel:(NSString *) tel{        NSString * insert=[NSString stringWithFormat:@" insert into  t_Person(name,age,tel) values('%@',%d,'%@')",name,age,tel];    [self _execSql:insert andTip:@"插入操作"];    }/** *  執(zhí)行數(shù)據(jù)庫操作 * *  @param sql 要執(zhí)行的sql *  @param tip 要執(zhí)行的操作標(biāo)題 */- (void) _execSql:(NSString *) sql andTip:(NSString *) tip{    char * result;    if(SQLITE_OK==sqlite3_exec(sqlite, sql.UTF8String, NULL, NULL, &result)){        NSLog(@"%@成功!",tip);    }else{        NSLog(@"%@失??!",tip);    }}

3、查詢數(shù)據(jù)庫 

/** *  讀取數(shù)據(jù) */- (void)_readData{    //1> 定義sql語句    NSString * sql=@"select id,name,age,tel from t_person ";        sqlite3_stmt * stmt=NULL;    //2> 檢查語法的正確性    if(SQLITE_OK==sqlite3_prepare_v2(sqlite, sql.UTF8String, -1, &stmt, NULL)){            //3> 循環(huán)結(jié)果集取數(shù)據(jù)        while(sqlite3_step(stmt)==SQLITE_ROW){            //4>注意:取出來數(shù)據(jù)可以封裝到集合中備用            int ID=sqlite3_column_int(stmt,0);            const unsigned char *name=sqlite3_column_text(stmt, 1);            int age=sqlite3_column_int(stmt, 2);            const unsigned char *tel=sqlite3_column_text(stmt, 3);            NSString * names=[NSString stringWithUTF8String:(const char *)name];            NSString * tels=[NSString stringWithUTF8String:(const char *)tel];            NSLog(@"%d,%@,%d,%@",ID,names,age,tels);                }        }}

 

作者:杰瑞教育
出處:http://m.survivalescaperooms.com/jerehedu/ 
本文版權(quán)歸煙臺(tái)杰瑞教育科技有限公司和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 隆安县| 霍山县| 砀山县| 新昌县| 方山县| 甘泉县| 巴彦淖尔市| 新营市| 武隆县| 宁南县| 吕梁市| 大余县| 紫阳县| 高雄县| 米脂县| 江孜县| 麻城市| 莱西市| 布拖县| 新平| 澄城县| 陈巴尔虎旗| 酉阳| 丰城市| 天柱县| 抚松县| 南溪县| 淮阳县| 开原市| 石棉县| 安新县| 固镇县| 布拖县| 兴仁县| 南充市| 扶沟县| 岳池县| 梁山县| 右玉县| 博白县| 曲阳县|