iOS_資料庫3_sqlite3基本操作

來源:互聯網
上載者:User

標籤:ios   sqlite3   資料庫   











控制器中直接寫SQL語句,未封裝
////  ViewController.m//  1.sqlite3基本操作////  Created by xss on 14-11-2.//  Copyright (c) 2014年 beyond. All rights reserved.//#import "ViewController.h"// 1.匯入庫,2.添加主標頭檔#import <sqlite3.h>@interface ViewController (){    // db代表著整個資料庫,db是資料庫執行個體    sqlite3 *_db;}@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    // 0.獲得沙箱中的資料庫檔案名    NSString *filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"student.sqlite"];    NSLog(@"filePath:%@",filename);    /*     2014-11-02 18:32:38.895 1.sqlite3基本操作[85356:245858] filePath:/Users/juns/Library/Developer/CoreSimulator/Devices/3198E002-E3C9-4523-983E-AC3E1283A654/data/Containers/Data/Application/E1150608-3EB8-4B9D-87AF-33EDF9FB6FF3/Documents/student.sqlite     2014-11-02 18:32:38.896 1.sqlite3基本操作[85356:245858] 成功開啟資料庫     2014-11-02 18:32:38.897 1.sqlite3基本操作[85356:245858] 成功建立t_student表     */        // 1.建立(開啟)資料庫(如果是首次開啟,則資料庫檔案不存在,那麼會自動建立)    // OC字串,直接轉成C語言字串,通過 UTF8String方法    int result = sqlite3_open(filename.UTF8String, &_db);    if (result == SQLITE_OK) {        NSLog(@"成功開啟資料庫");                // 2.創表        const char *sql = "create table if not exists t_student (id integer primary key autoincrement, name text, age integer);";        // 凡是要傳地址的,最好先清空,防止出現野指標,C語言中空是NULL        char *errorMesg = NULL;        // 參數3和4是回調時用        int result = sqlite3_exec(_db, sql, NULL, NULL, &errorMesg);        if (result == SQLITE_OK) {            NSLog(@"成功建立t_student表");        } else {            NSLog(@"建立t_student表失敗:%s", errorMesg);        }    } else {        NSLog(@"開啟資料庫失敗");    }}// 完全仿造createTable操作- (IBAction)insertBtnClicked:(UIButton *)sender{    for (int i = 0; i<30; i++) {        NSString *name = [NSString stringWithFormat:@"beyond-%d", arc4random()%100];        int age = arc4random()%100;        NSString *sql = [NSString stringWithFormat:@"insert into t_student (name, age) values('%@', %d);", name, age];                char *errorMesg = NULL;        int result = sqlite3_exec(_db, sql.UTF8String, NULL, NULL, &errorMesg);        if (result == SQLITE_OK) {            NSLog(@"成功添加資料");        } else {            NSLog(@"添加資料失敗:%s", errorMesg);        }    }}// 方法同上- (IBAction)deleteBtnClicked:(UIButton *)sender{}// 方法同上- (IBAction)updateBtnClicked:(UIButton *)sender{}//- (IBAction)queryBtnClicked:(UIButton *)sender{    // SQL注入漏洞        /**     登入功能          1.使用者輸入帳號和密碼     * 帳號:123' or 1 = 1 or '' = '     * 密碼:456654679          2.拿到使用者輸入的帳號和密碼去資料庫查詢(查詢有沒有這個使用者名稱和密碼)     select * from t_user where username = '123' and password = '456';               select * from t_user where username = '123' and password = '456';     */        // 1.定義sql語句    const char *sql = "select id, name, age from t_student where name = ?;";        // 2.定義一個stmt存放結果集,用於執行靜態 SQL 陳述式並返回它所產生結果的對象    sqlite3_stmt *stmt = NULL;        // 3.檢測SQL語句的合法性,參數3是sql語句的長度,只要寫-1,會自動計算,參數4是statement存放結果集    int result = sqlite3_prepare_v2(_db, sql, -1, &stmt, NULL);    if (result == SQLITE_OK)    {        NSLog(@"查詢語句是合法的");                // 設定預留位置的內容,參數2 指的是第幾個預留位置號,注意是從1開始;參數4是預留位置的長度,只要寫-1,會自動計算,        sqlite3_bind_text(stmt, 1, "beyond", -1, NULL);                // 4.step 執行SQL語句,從結果集中取出資料        // int stepResult = sqlite3_step(stmt);                // step的執行結果等於SQLITE_ROW,表示真的查詢到一行資料        while (sqlite3_step(stmt) == SQLITE_ROW)        {            // 獲得這行對應的資料,結果存放在statement中                        // 獲得第0列的id            int sid = sqlite3_column_int(stmt, 0);                        // 獲得第1列的name            const unsigned char *sname = sqlite3_column_text(stmt, 1);                        // 獲得第2列的age            int sage = sqlite3_column_int(stmt, 2);                        NSLog(@"%d %s %d", sid, sname, sage);        }    } else {        NSLog(@"查詢語句非合法");    }}@end

























iOS_資料庫3_sqlite3基本操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.