天天看點

C語言資料庫程式設計

----摘自個人C語言資料庫項目報告

3.4邏輯結構的SQL語句實作

建立基本表:

3.4-1建立商品表:

create table goods(goods_id int primary key,goods_types varchar,goods_name varchar,goods_price varchar,appraise text);

3.4-2建立賣家表:

create table seller(seller_id int primary key,seller_name varchar,seller_sex varcahr,phone int,address text)

3.4-3建立買家表:

create table buyer(buyer_id int primary key,buyer_name varchar,buyer_sex varchar,buyer_phone int,buyer_address text

3.4-4建立賬單視圖表:

Create view bill as select buyer_id,buyer_name,buyer_address,goods_id,goods_name,goods_price from buyer,goods;

3.4-5建立店鋪基本表:

create table store(store_id int primary key, store_name text);

3.4-6建立賣家具體詳情視圖表:

create view seller_message as select seller_id, seller_name,seller_sex,goods_name,store_name,store_address from seller,store,goods;

3.4-7插入資料:

本系統有使用者自定義插入資料,無需在C語言裡面寫入要插入的資料,用了本書上的sqlite3_mprintf格式化輸入函數來實作。

Sql=sqlite3_mprintf(“insert into  表名 values(%格式化)“, 格式化的變量); 此處%d整型無需引号,%s字元型加單引号。

例如本系統中插入資料:char *pSQL=sqlite3_mprintf("insert into store values(%d,'%s' )",store_id,store_name);

3.4-8查找資料:

自定義查找:sqlite3_mprintf(“select 列名 from 表名 where 列名= 格式化”,格式變量);

例如本系統中查找資料:

char  *pSQL= sqlite3_mprintf("select store_id, store_name from store where store_id =  %d ",col_value);

3.4-9删除資料:

自定義删除:sqlite3_mprintf(“delete from 表名 where 列名=格式化”,格式化變量);

例如本系統中自定義删除資料:char *pSQL=sqlite3_mprintf("delete from goods where goods_id = %s ",col_value) ;

删除表:drop table 表名。

項目中代碼: