sqlite3資料庫基礎語句

2021-07-05 07:45:21 字數 3523 閱讀 2996

// 建立乙個全域性的靜態的資料庫 (資料庫是乙個檔案)

// 為什麼要用static?

// 因為要保證的資料物件之有乙個

static sqlite3 *db = nil;

@implementation sqlmanager

#pragma  開啟資料庫

+(sqlite3 *)opendb

// 二、沒有開啟的情況

// 1.先建立乙個(document)路徑

nsstring *docpath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes)objectatindex:0];

// 2.在docpath路徑下面建立乙個資料庫路徑

// 通常資料庫檔案都要以 .sqlite 結尾,這樣用資料庫的圖形工具可以直接開啟

nslog(@"filepath = %@",filepath);

// 3.在filepath路徑下建立乙個資料庫檔案 (所有sqlite的操作都是基於 c語言的介面)

// 引數一:資料庫檔案的路徑

int state = sqlite3_open([filepath utf8string], &db);

if (state != sqlite_ok)

// 資料庫開啟成功了

// 4.在資料庫裡面建立儲存資料的表(資料庫的資料以表的形式來儲存)                       主鍵

nsstring *createtablestr = @"create table if not exists classa(id integer primary key,name text,phone text,age integer)"; // 本行字串的**乙個字母都不能錯

// 建立表的關鍵字 create table

// classa:表名

// primary key:主鍵

// integer:型別

// 5.使用sql語句建立表

char *errmsg;

// 引數一:要對哪乙個資料庫進行操作

// 引數二:要對資料庫做什麼樣的操作

// 引數三:系統預留引數

// 引數四:系統預留引數

// 引數五:錯誤資訊

int result = sqlite3_exec(db, [createtablestr utf8string], null, null, &errmsg);

if (result)

return db;

}#pragma  關閉資料庫(暫時沒用)

+(void)closedb

}#pragma  新增乙個學生

+(bool)addstudent:(studentmodel *)student

}return no;

}#pragma  查詢學生 (查詢)

+(nsmutablearray *)findallstudent

}return marray;

}#pragma  更改

+(bool)updatestudentname:(nsstring *)name age:(int)age phone:(nsstring *)phone whereidisequal:(int)id

}return no;

}#pragma  刪除

+(bool)deletebyid:(int)id

}return no;

}另附一些基礎知識:

real 浮點數

integer =int

text 字串型別

blob  二進位制

資料庫中資料是以表的形式儲存的

資料存在表中的,表又放在資料庫中

記錄一般是橫向看一行資訊

豎向看表,是乙個字段乙個欄位的,叫做字段

橫向看,是一條一條資訊,叫記錄

sqlite,是乙個輕型的關係型資料庫,它的設計目標是支援,嵌入式,它占用的資源非常低,在嵌入式裝置中,他可能只需要幾百k的記憶體,它能夠支援windows/linux/等等主流的作業系統,目前版本是sqlite3

主鍵:唯一標示一條記錄的字段(它會隨著)

外來鍵:在本表中存在其他表中的主鍵字段

關係型資料庫:就是根據主鍵和外來鍵關聯起來的

多個表之間的聯絡

//建立表

create table 表名(欄位名1 字段型別 primary key,欄位名2 型別,price text,address text,stuid integer);

//新增資料(字元要有單引號)

insert into stubreakfast(fname,price

sqlite3的常用方法,address,stuid) values(?,?,?,?);//主鍵不用設定,會自動幫我們順序加一

insert into stubreakfast(fname,price,address,stuid) values(?,?,?,?);

//更新資料

update stubreakfast set fname='油條',price='4.0' where fid=1;

//刪除語法

delete from stubreakfast where fid=1;

//查詢語法

select 欄位一,欄位二,欄位三。。。from 表名 where 條件

select fame,price from stubreakfast; *表示所有的字段

select fname from stubreakfast where address='路邊';

select fname from stubreakfast where address='全家' and stuid;

模糊查詢

%,通配乙個

*統配多個

select fname from stubreakfast where fname like '酸%';

select count(*) from stubreakfast;

select * from stubreakfast;

工具類最好宣告稱類方法,不用建立物件檢視

+方法裡面只能使用靜態變數用static修飾的,不能使用例項變數

sqlite3_open:根據乙個路徑開啟乙個資料庫檔案

sqlite_ok:successful result

sqlite3_exec:對資料庫的表的操作

sqlite3_close:關閉資料庫

sqlite3_free:釋放指標

sqlite3_prepare_v2:準備乙個sql語句的

sqlite3_bind_***x:對資料庫裡面的字段進行賦值的函式

sqlite3_step:對sql語句的執行

sqlite3_stmt:資料庫管理員

sqlite_done:執行完畢

sqlite3_column_***:從資料庫裡面按照乙個字段乙個欄位的讀取資料

sqlite_row:sqlite3_step() has another row ready

sqlite3資料庫基本語句

sqlite3 student.db 開啟新建資料庫student.db quit 退出 schema 檢視 屬性 databases 檢視開啟的資料庫 table 檢視當前 create table stu id integer,name char,score integer 建立 insert ...

Sqlite3 資料庫使用

iphone本身是支援 sqlite3 資料庫的,在專案中匯入libsqlite3.dylib。並建立資料庫,在終端,建立資料庫的方式 mkdir sql 建立sql資料夾 cd sql 進入sql目錄下 sqlite3 student.sql 建立名為 student.sql的資料庫 建立表 插入...

sqlite3資料庫操作

1 開啟資料庫 1 需要制定資料庫的路徑 nsstring filepath nshomedirectory documents data.sqlite 2 建立資料庫的物件 sqlite3 qingyundb null 3 開啟命令 sqlite3 open dbfilepath utf8stri...