欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS sqlite對數(shù)據(jù)庫的各種操作(日常整理全)

 更新時間:2016年03月25日 09:42:39   作者:徒步天涯  
在IOS中使用Sqlite來處理數(shù)據(jù)。如果你已經(jīng)了解了SQL,那你可以很容易的掌握SQLite數(shù)據(jù)庫的操作。本文給大家介紹iOS sqlite對數(shù)據(jù)庫的各種操作,需要的朋友參考下吧

在IOS中使用Sqlite來處理數(shù)據(jù)。如果你已經(jīng)了解了SQL,那你可以很容易的掌握SQLite數(shù)據(jù)庫的操作。iOS對于數(shù)據(jù)庫的操作:增加、刪除、查找、修改具體介紹如下所示:

首先需要創(chuàng)建一個數(shù)據(jù)庫:本程序的數(shù)據(jù)庫是在火狐瀏覽器里的插件里寫的微量型數(shù)據(jù)庫

火狐找查找SQLite Manager的步驟:

第一步:在工具欄找到附加組件,點擊進入


第二步:搜索 SQP,找到并下載,安裝完成之后需要重啟瀏覽器


第三步:在工具只樂觀找到SQLite Manager,點擊打開


SQLite Manager界面如圖所示


注:SQLite Manager是微量型的數(shù)據(jù)庫編程軟件,所以一次只能執(zhí)行一句代碼?。?!

•創(chuàng)建數(shù)據(jù)庫

--數(shù)據(jù)庫的建立
create table team
( -- 創(chuàng)建名字為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 

工程目錄文件如下:

這里需要導入一個系統(tǒng)自帶的文件libsqlite3.0.tbd

步驟如圖:


•實現(xiàn)工程

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:@"數(shù)據(jù)庫顯示" 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:@"數(shù)據(jù)庫添加" 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:@"數(shù)據(jù)庫修改" 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:@"數(shù)據(jù)庫刪除" forState:UIControlStateNormal];
[self.deletebtn addTarget:self action:@selector(deleteSql) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.deletebtn];
}
#pragma mark - 顯示數(shù)據(jù)表中的所有信息
-(void)showSql
{
NSLog(@"顯示數(shù)據(jù)庫信息");
// 數(shù)據(jù)庫
sqlite3 *db;
// 根據(jù)指定的數(shù)據(jù)庫文件存儲路徑打開數(shù)據(jù)庫
int result=sqlite3_open([PATH UTF8String], &db);
// 創(chuàng)建執(zhí)行命令對象
sqlite3_stmt *stmt;
// 打開數(shù)據(jù)庫成功
if (result==SQLITE_OK) {
NSLog(@"連接成功");
// 執(zhí)行預處理命令
int res=sqlite3_prepare_v2(db, "select *from team", -1, &stmt, nil);
if (res==SQLITE_OK) {
// 循環(huán)遍歷數(shù)據(jù)表的行信息
while (sqlite3_step(stmt)==SQLITE_ROW) {
// 獲取數(shù)據(jù)表中整型列的信息
int stu_id=sqlite3_column_int(stmt, 0);
NSLog(@"stu_id is %d",stu_id);
// 獲取數(shù)據(jù)表中字符型的列的信息
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 - 修改數(shù)據(jù)庫
-(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

以上內(nèi)容是小編給大家日常收集整理的iOS sqlite對數(shù)據(jù)庫的各種操作,希望對大家有所幫助,如果大家想了解更多有關ios sqlite相關知識請登錄腳本之家網(wǎng)站了解詳情,同時也非常感謝大家一直以來對腳本之家網(wǎng)站的支持!

相關文章

  • 詳解iOS - ASIHTTPRequest 網(wǎng)絡請求

    詳解iOS - ASIHTTPRequest 網(wǎng)絡請求

    本篇文章主要介紹了iOS - ASIHTTPRequest 網(wǎng)絡請求 ,詳細的介紹了 ASIHTTPRequest的使用,具有一定的參考價值,有興趣的可以了解一下。
    2016-12-12
  • iOS實現(xiàn)不規(guī)則Button點擊效果實例代碼

    iOS實現(xiàn)不規(guī)則Button點擊效果實例代碼

    這篇文章主要給大家介紹了關于iOS實現(xiàn)不規(guī)則Button點擊的相關資料,文中通過示例代碼介紹的非常詳細,對各位iOS開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-04-04
  • iOS開發(fā)之APP相關

    iOS開發(fā)之APP相關

    本文給大家介紹的是IOS開發(fā)系列文章的第一篇,給大家分享一些APP相關的知識點,非常的實用,有需要的小伙伴可以參考下
    2016-04-04
  • iOS支付寶使用方法詳解

    iOS支付寶使用方法詳解

    這篇文章主要為大家詳細介紹了iOS支付寶的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • IOS中手風琴表不能移動的解決方法

    IOS中手風琴表不能移動的解決方法

    本文給大家分享的是IOS開發(fā)中遇到的一個手風琴特效無法正常工作的問題的解決方法,經(jīng)過度娘了很久才找到解決方法,這里推薦給大家,有需要的小伙伴可以參考下。
    2015-05-05
  • iOS 高效的分頁加載實現(xiàn)示例

    iOS 高效的分頁加載實現(xiàn)示例

    本篇文章主要介紹了iOS 高效的分頁加載實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • safari cookie設置中文失敗的解決方法

    safari cookie設置中文失敗的解決方法

    下面小編就為大家?guī)硪黄猻afari cookie設置中文失敗的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08
  • 支持Xcode10和適配iPhone XS Max、iPhone XR的方法

    支持Xcode10和適配iPhone XS Max、iPhone XR的方法

    這篇文章主要介紹了支持Xcode10和適配iPhone XS Max、iPhone XR的方法,文中通過示例代碼以及圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-10-10
  • 基于iOS Realm數(shù)據(jù)庫的使用實例詳解

    基于iOS Realm數(shù)據(jù)庫的使用實例詳解

    下面小編就為大家分享一篇基于iOS Realm數(shù)據(jù)庫的使用實例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • 詳解IOS圖層轉(zhuǎn)場動畫

    詳解IOS圖層轉(zhuǎn)場動畫

    這篇文章主要為大家詳細介紹了IOS圖層轉(zhuǎn)場動畫, CATransition類實現(xiàn)層的轉(zhuǎn)場動畫,能夠為層提供移出屏幕和移入屏幕的動畫效果,感興趣的小伙伴們可以參考一下
    2016-02-02

最新評論