您好,登錄后才能下訂單哦!
小編給大家分享一下iOS中原生sqlite3怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
具體如下:
SQLite?
SQLit是一個開源、輕型嵌入式關系數據庫,誕生于2000年5月
占用資源非常的低,在嵌入式設備中,可能只需要幾百K的內存就夠了
能夠支持Windows/Linux/Unix等等主流的操作系統
比起Mysql、PostgreSQL這兩款開源的世界著名數據庫管理系統來講,它的處理速度比他們都快
SQL語句常用操作
增、刪、改、查,CRUD,Create[新建], Retrieve[檢索], Update[更新], Delete[刪除]。
SQL語法寫法特點
1、不區分大小寫(CREATE = create)
2、每條語句以分號(;)結尾
3、關鍵字建議大寫
SQL語句常用關鍵字
select、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、index等
數據定義語句(DDL):包括create和drop等操作,在數據庫中創建新表或刪除表(create table或 drop table)
數據操作語句(DML):包括insert、update、delete等操作,分別用于添加、修改、刪除表中的數據
數據查詢語句(DQL):可以用于查詢獲得表中的數據,關鍵字select是DQL(也是所有SQL)用得最多的操作,其他DQL常用的關鍵字有where,order by,group by和having
// ViewController.m // sqlite3 // // Created by 周玉 on 2017/12/13. // Copyright © 2017年 guidekj. All rights reserved. // #import "ViewController.h" #import <sqlite3.h> #define KUIScreenWidth [UIScreen mainScreen].bounds.size.width #define KUIScreenHeight [UIScreen mainScreen].bounds.size.height #define FILE_NAME @"saas.sqlite" static sqlite3 *db = nil; @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"sqlite3"; //DDB // [self openDB]; // [self closeDB]; //DDL // [self createTable]; // [self dropTable]; //DML // [self insertData]; // [self updateData]; // [self deleteData]; //DQL [self queryData]; } #pragma mark 查詢 - (void)queryData{ sqlite3 *newDB = [self openDB]; sqlite3_stmt *statement = nil; NSString *sqlStr = @"SELECT * FROM SAAS_PERSON"; int result = sqlite3_prepare_v2(newDB, sqlStr.UTF8String, -1, &statement, NULL); if (result == SQLITE_OK) { //遍歷查詢結果 if (!(sqlite3_step(statement) == SQLITE_DONE)) { while (sqlite3_step(statement) == SQLITE_ROW) { int ID = sqlite3_column_int(statement, 0); const unsigned char *name = sqlite3_column_text(statement, 1); const unsigned char *sex = sqlite3_column_text(statement, 2); int age = sqlite3_column_int(statement, 3); const unsigned char *description = sqlite3_column_text(statement, 4); NSLog(@"ID = %d , name = %@ , sex = %@ , age = %d , description = %@",ID,[NSString stringWithUTF8String:(const char *)name],[NSString stringWithUTF8String:(const char *)sex],age,[NSString stringWithUTF8String:(const char *)description]); } } else { NSLog(@"查詢語句完成"); } } else { NSLog(@"查詢語句不合法"); } sqlite3_finalize(statement); [self closeDB]; } #pragma mark 刪除數據記錄 - (void)deleteData{ sqlite3 *newDB = [self openDB]; sqlite3_stmt *statement = nil; NSString *sqlStr = @"DELETE FROM SAAS_PERSON WHERE NAME = '王鵬飛'"; int result = sqlite3_prepare_v2(newDB, sqlStr.UTF8String, -1, &statement, NULL); if (result == SQLITE_OK) { if (sqlite3_step(statement) == SQLITE_DONE) { NSLog(@"刪除操作完成"); } } else { NSLog(@"刪除操作不合法"); } sqlite3_finalize(statement); [self closeDB]; } #pragma mark 更新數據記錄 - (void)updateData{ sqlite3 *newDB = [self openDB]; sqlite3_stmt *statement = nil; NSString *sqlStr = @"UPDATE SAAS_PERSON SET DESCRIPTION = '喜歡運動,旅游' WHERE NAME = '周玉'"; int result = sqlite3_prepare_v2(newDB, sqlStr.UTF8String, -1, &statement, NULL); if (result == SQLITE_OK) { if (sqlite3_step(statement) == SQLITE_DONE) { NSLog(@"更新信息完成"); } } else { //[logging] no such column: DESCRIPT (KEY拼寫錯誤) --- 更新信息不合法 NSLog(@"更新信息不合法"); } sqlite3_finalize(statement); [self closeDB]; } #pragma mark 新增數據記錄 - (void)insertData{ sqlite3 *newDB = [self openDB]; sqlite3_stmt *statement = nil; // NSString *sqlStr = @"INSERT INTO SAAS_PERSON (NAME , SEX , AGE , DESCRIPTION) VALUES('周玉','男',28,'開朗樂觀')"; // NSString *sqlStr = @"INSERT INTO SAAS_PERSON (NAME , SEX , AGE , DESCRIPTION) VALUES('王鵬飛','女',27,'開朗樂觀')"; // NSString *sqlStr = @"INSERT INTO SAAS_PERSON (NAME , SEX , AGE , DESCRIPTION) VALUES('李梅','女',20,'年輕可愛??')"; NSString *sqlStr = @"INSERT INTO SAAS_PERSON (NAME , SEX , AGE , DESCRIPTION) VALUES('sumit','男',15,'小朋友的年紀')"; //檢驗合法性 int result = sqlite3_prepare_v2(newDB, sqlStr.UTF8String, -1, &statement, NULL); if (result == SQLITE_OK) { //判斷語句執行完畢 if (sqlite3_step(statement) == SQLITE_DONE) { NSLog(@"插入的信息完成"); } } else { NSLog(@"插入的信息不合法"); } sqlite3_finalize(statement); [self closeDB]; } #pragma mark 創建表 - (void)createTable{ sqlite3 *newDB = [self openDB]; // char *sql = "CREATE TABLE IF NOT EXISTS t_person (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, NAME TEXT , SEX TEXT,AGE INTEGER,DESCRIPTION TEXT);"; NSString *sqlStr = @"CREATE TABLE IF NOT EXISTS SAAS_PERSON (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, NAME TEXT , SEX TEXT,AGE INTEGER,DESCRIPTION TEXT);"; char *error = NULL; int result = sqlite3_exec(newDB, sqlStr.UTF8String, NULL, NULL, &error); if (result == SQLITE_OK) { NSLog(@"創建表成功"); } else { NSLog(@"創建表失敗 = %s",error); } [self closeDB]; } #pragma mark 刪除表 - (void)dropTable{ sqlite3 *newDB = [self openDB]; NSString *sqlStr = @"DROP TABLE t_person"; char *error = NULL; int result = sqlite3_exec(newDB, sqlStr.UTF8String, NULL, NULL, &error); if (result == SQLITE_OK) { NSLog(@"刪除表成功"); } else { NSLog(@"刪除表失敗 = %s",error); } [self closeDB]; } #pragma mark 打開或者創建數據庫 - (sqlite3 *)openDB { if (!db) { //1.獲取document文件夾的路徑 //參數1:文件夾的名字 參數2:查找域 參數3:是否使用絕對路徑 NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; //獲取數據庫文件的路徑 NSString *dbPath = [documentPath stringByAppendingPathComponent:@"saas.sqlite"]; NSLog(@"%@",dbPath); //判斷document中是否有sqlite文件 int result = sqlite3_open([dbPath UTF8String], &db); if (result == SQLITE_OK) { NSLog(@"打開數據庫"); }else{ [self closeDB]; NSLog(@"打開數據庫失敗"); } } return db; } #pragma mark 關閉數據庫 - (void)closeDB{ int result = sqlite3_close(db); if (result == SQLITE_OK) { NSLog(@"數據庫關閉成功"); db = nil; } else { NSLog(@"數據庫關閉失敗"); } } @end
以上是“iOS中原生sqlite3怎么用”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。