資料庫操作 SQLite

2021-06-23 09:26:12 字數 1782 閱讀 4886

sqlite (

) 是乙個輕量級的關聯式資料庫。

sqlite最初的設計目標是用於嵌入式系統,它占用資源非常少,在嵌入式裝置中,只需要幾百k的記憶體就夠了,目前應用於android、ios、windows phone等智慧型手機。

ios 使用時sqlite,只需要加入 libsqlite3.dylib 依賴以及引入 sqlite3.h 標頭檔案即可。

資料庫操作包含開啟資料庫、建立表,表的增、刪、改、查。下面**給出了資料庫操作。

建立並開啟資料庫:

[cpp]view plain

copy

print?

-(bool

) opendbelse

}  ios中沒有提供建立資料庫的命令,當使用

sqlite3_open時,如果資料庫檔案不存在會自行建立資料庫,如果存在會開啟資料庫。開啟資料庫後就可以建立表並操作表內容了,ios中的sqlite3使用

sqlite3_exec來建立表、插入表內容、修改表內容、刪除表內容等操作,使用

sqlite3_prepare_v2來查詢表。下面給給出了sqlite3_exec的封裝:

[cpp]view plain

copy

print?

-(void

)execsql:(nsstring *)sql  

else

sqlite3_close(db);  

}      

}  

建立表:

[cpp]view plain

copy

print?

nsstring *sqlcreatetable =  [nsstring stringwithformat:@

"create table if not exists '%@' ('%@' integer primary key autoincrement, '%@' text, '%@' integer, '%@' text)"

,tablename,id,name,age,address];  

[self execsql:sqlcreatetable];  

插入資料:

[cpp]view plain

copy

print?

-(void

) insertdata  

修改表:

[cpp]view plain

copy

print?

-(void

) updatedata  

刪除表內容:

[cpp]view plain

copy

print?

-(void

) deletedata  

上面實現了表內容的增、改、刪,下面實現表內容查詢。

[cpp]view plain

copy

print?

-(void

) selectdata  

}else

sqlite3_close(db);  

}  

好了,這就是全部了,ios中資料庫操作的類封裝的並不太好用,希望大家盡量封裝自己的類,。

資料庫操作 SQLite

sqlite 是乙個輕量級的關聯式資料庫。sqlite最初的設計目標是用於嵌入式系統,它占用資源非常少,在嵌入式裝置中,只需要幾百k的記憶體就夠了,目前應用於android ios windows phone等智慧型手機。ios 使用時sqlite,只需要加入 libsqlite3.dylib 依賴...

SQLite資料庫操作

建立資料庫需要使用的api sqliteopenhelper 必須定義乙個構造方法 arg1 資料庫的名字 people.db arg2 游標工廠 通常直接傳人null,則系統會使用預設的工廠 arg3 資料庫版本號 從1開始 方便公升級使用,不斷設定更大的值會呼叫,onupgrade方法 publ...

SQLite資料庫操作

特點 輕量級 只用乙個動態的庫,是以單個檔案的形式進行訪問 跨平台 支援多個作業系統 零配置 無需安裝,直接使用 嵌入式 內嵌到手機中 3.在程式的內部可以通過資料庫的名稱訪問,其他應用不能訪問 4.路徑 data data 應用程式包名 database db 5.存放的型別 null 空值 in...