在IOS中應用SQLite來處置數據。假如你曾經懂得了SQL,那你可以很輕易的控制SQLite數據庫的操作。IOS關於數據庫的操作:增長、刪除、查找、修正詳細引見以下所示:
起首須要創立一個數據庫:本法式的數據庫是在火狐閱讀器裡的插件裡寫的微量型數據庫
火狐找查找SQLite Manager的步調:
第一步:在對象欄找到附加組件,點擊進入
第二步:搜刮 SQP,找到並下載,裝置完成以後須要重啟閱讀器
第三步:在對象只悲觀找到SQLite Manager,點擊翻開
SQLite Manager界面如圖所示
注:SQLite Manager是微量型的數據庫編程軟件,所以一次只能履行一句代碼!!!
•創立數據庫
--數據庫的樹立 create table team ( -- 創立名字為team的table stu_id integer primary key autoincrement, stu_name varchar(100), stu_password varchar(100), stu_login varchar(100) )--添加信息 insert into team(stu_name,stu_password,stu_login) values('xiaming','123456','xm') insert into team(stu_name,stu_password,stu_login) values('zhangsan','123456',' zs') --查詢信息select *from team --刪除信息delete from team where stu_id=3
工程目次文件以下:
這裡須要導入一個體系自帶的文件libsqlite3.0.tbd
步調如圖:
•完成工程
ViewController.h
#import <UIKit/UIKit.h> #import <sqlite3.h> @interface ViewController : UIViewController @property(strong,nonatomic)UIButton *showbtn; @property(strong,nonatomic)UIButton *insertbtn; @property(strong,nonatomic)UIButton *updatebtn; @property(strong,nonatomic)UIButton *deletebtn; @end
ViewController.h
#import "ViewController.h" #define PATH [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"team.sqlite"] @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 獲得沙盒 Documents文件途徑 NSLog(@"%@",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]); [self button]; } -(void)button { self.showbtn=[UIButton buttonWithType:UIButtonTypeSystem]; self.showbtn.frame=CGRectMake(100, 50, 200, 50); [self.showbtn setTitle:@"數據庫顯示" forState:UIControlStateNormal]; [self.showbtn addTarget:self action:@selector(showSql) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.showbtn]; self.insertbtn=[UIButton buttonWithType:UIButtonTypeSystem]; self.insertbtn.frame=CGRectMake(100, 100, 200, 50); [self.insertbtn setTitle:@"數據庫添加" forState:UIControlStateNormal]; [self.insertbtn addTarget:self action:@selector(insertSql) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.insertbtn]; self.updatebtn=[UIButton buttonWithType:UIButtonTypeSystem]; self.updatebtn.frame=CGRectMake(100, 150, 200, 50); [self.updatebtn setTitle:@"數據庫修正" forState:UIControlStateNormal]; [self.updatebtn addTarget:self action:@selector(updateSql) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.updatebtn]; self.deletebtn=[UIButton buttonWithType:UIButtonTypeSystem]; self.deletebtn.frame=CGRectMake(100, 200, 200, 50); [self.deletebtn setTitle:@"數據庫刪除" forState:UIControlStateNormal]; [self.deletebtn addTarget:self action:@selector(deleteSql) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.deletebtn]; } #pragma mark - 顯示數據表中的一切信息 -(void)showSql { NSLog(@"顯示數據庫信息"); // 數據庫 sqlite3 *db; // 依據指定的數據庫文件存儲途徑翻開數據庫 int result=sqlite3_open([PATH UTF8String], &db); // 創立履行敕令對象 sqlite3_stmt *stmt; // 翻開數據庫勝利 if (result==SQLITE_OK) { NSLog(@"銜接勝利"); // 履行預處置敕令 int res=sqlite3_prepare_v2(db, "select *from team", -1, &stmt, nil); if (res==SQLITE_OK) { // 輪回遍歷數據表的行信息 while (sqlite3_step(stmt)==SQLITE_ROW) { // 獲得數據表中整型列的信息 int stu_id=sqlite3_column_int(stmt, 0); NSLog(@"stu_id is %d",stu_id); // 獲得數據表中字符型的列的信息 NSLog(@"%@",[NSString stringWithFormat:@"%s",sqlite3_column_text(stmt, 1)]); NSLog(@"%@",[NSString stringWithFormat:@"%s",sqlite3_column_text(stmt, 2)] ); NSLog(@"%@",[NSString stringWithFormat:@"%s",sqlite3_column_text(stmt, 3)] ); } } } } #pragma mark -增長信息 -(void)insertSql { sqlite3 *db; sqlite3_stmt *stmt; sqlite3_open([PATH UTF8String], &db); int rst=sqlite3_prepare_v2(db, "insert into team(stu_name,stu_password,stu_login) values(?,?,?)", -1, &stmt, nil); sqlite3_bind_text(stmt, 1, "wangwu", -1, nil); sqlite3_bind_text(stmt, 2, "123456", -1, nil); sqlite3_bind_text(stmt, 3, "ww", -1, nil); // 斷定能否增長勝利 if (rst==SQLITE_OK) { if (SQLITE_DONE==sqlite3_step(stmt)) { NSLog(@"add ok"); }else{ NSLog(@"add fail"); } } } #pragma mark - 修正數據庫 -(void)updateSql { sqlite3 *db; sqlite3_stmt *stmt; sqlite3_open([PATH UTF8String], &db); // 辦法一 /* int res = sqlite3_prepare_v2(db, "update team set stu_name=(?),stu_password=(?),stu_login=(?) where stu_id=2" , -1, &stmt, nil); sqlite3_bind_text(stmt, 1, "xiaoming", -1, nil); sqlite3_bind_text(stmt, 2, "123456", -1, nil); sqlite3_bind_text(stmt, 3, "xm", -1, nil); */ // 辦法二 int rst=sqlite3_prepare_v2(db, "update team setstu_name='zl',stu_password='zl123',stu_login='zhangsan' where stu_id=4", -1, &stmt, nil); // 斷定能否修正勝利 if (rst==SQLITE_OK) { if (SQLITE_DONE == sqlite3_step(stmt)) { NSLog(@" update ok"); }else{ NSLog(@"update fail"); } } } -(void)deleteSql { sqlite3 *db; sqlite3_stmt *stmt; sqlite3_open([PATH UTF8String], &db); int rst=sqlite3_prepare_v2(db, "delete from team where stu_id=9", -1, &stmt, nil); // 斷定能否刪除勝利 if (rst==SQLITE_OK) { if (SQLITE_DONE==sqlite3_step(stmt)) { NSLog(@" delete ok"); }else{ NSLog(@"delete fail"); } } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
以上內容是小編給年夜家平常搜集整頓的IOS sqlite對數據庫的各類操作,願望對年夜家有所贊助,假如年夜家想懂得更多有關ios sqlite相干常識請登錄本站網站懂得概況,同時也異常感激年夜家一向以來對本站網站的支撐!
【iOS sqlite對數據庫的各類操作(平常整頓全)】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!