資料持久化技術 二) 前進的火車 新浪部落格

2021-10-10 12:04:18 字數 2228 閱讀 7618

四、sqlite

libsqlite3.0提供api和引數,用sql語句對資料庫進行增、刪、改、查.無論是fmdb還是coredata都是對sql語句進行的封裝。

1.原生的sq語句

建立表:

create table person (name text, age integer);

create table people (id integer primary key, name text, age integer, height real);

primary key: 主鍵

自增/唯一性

插入資料(增):

insert into person (name, age) values ('jonny', 19);

insert into people (name,age,height) values ('jonny',19,1.85);

更新資料(改):

update person set name='bob';

update people set height=1.80 where name='bob' or height>1.6;

查詢資料(查):

select * from person;

select * from people where name='jonny' and id=20;

刪除資料(刪):

delete from person;

delete from people where name='bob' or height<1.75;

demo如下:

注意:不要忘記引入依賴庫libsqlite.3.0.tbd

//1.建立資料庫(檔案)xx/documents/database.sqlite

nsstring

*documentspath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask,

yes) firstobject];

//如果沒有資料庫檔案,自動建立; 如果有,就自動開啟

sqlite3 *db;

intret = sqlite3_open([databasepath cstringusingencoding:nsutf8stringencoding], &db);

if (ret != sqlite_ok)

//2.建立表 (sql語句)

const

char

*createtable = "create table if not exists people (id integer primary key, name text, age integer, height real)";

char *errmsg = null;

ret = sqlite3_exec(db, createtable, null, null, &errmsg);

if (ret != sqlite_ok)

//3.增/改/查

const

char

*insertdata = "insert into people (name, age, height) values ('jonny', 19, 1.85)";

ret = sqlite3_exec(db, insertdata, null, null, &errmsg);

if (ret != sqlite_ok)

const

char *updatedata = "update people set height=1.8 where name='jonny'";

ret = sqlite3_exec(db, updatedata, null, null, &errmsg);

if (ret != sqlite_ok)

//查詢

const

char

*query = "select * from people";

sqlite3_stmt *stmt;

ret = sqlite3_prepare_v2(db, query, -1, &stmt, null);

if (ret == sqlite_ok)

//釋放stmt變數的記憶體

sqlite3_finalize(stmt);}

//收尾工作

(斷開和資料庫的連線)

sqlite3_close(db);

udp 前進的火車 新浪部落格

udp通訊協議的特點 1.將資料極封裝為資料報,面向無連線。2.每個資料報大小限制在64k中 3.因為無連線,所以不可靠 4.因為不需要建立連線,所以速度快 5.udp 通訊是不分服務端與客戶端的,只分傳送端與接收端。比如 物管的對講機,遊戲.udp協議下的socket datagramsocket...

List Set 前進的火車 新浪部落格

collection 單列集合的根介面 list 如果實現了list介面的集合類,具備的特點 有序,可重複。arraylist arraylist 底層是維護了乙個object陣列實現 的,特點 查詢速度快,增刪慢。linkedlist linkedlist底層是使用了鍊錶資料結構實現的,特點 查詢...

Label Button 前進的火車 新浪部落格

1.label uilabel label uilabel alloc init 建立 self label label 關聯到當前屬性 label.text 顯示的內容 顯示的內容 cgfloat width self view.bounds.size.width 獲取螢幕的寬度 label.fr...