FirebirdSQL/5/VFP
出自VFP Wiki
(修訂版本間差異)
小 (revert) |
|||
(2個中途的修訂版本沒有顯示) | |||
第1行: | 第1行: | ||
- | + | #Firebird不支援由ODBC建立資料庫所以使用ISQL建立. | |
- | + | #本篇文章中VFP8.0採用SPT方式存取資料庫. | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | === VFP&Firebird (1)--動態產生資料庫 === | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | [ | + | <pre> |
- | [ | + | *!* By Ruey |
- | [ | + | LOCAL lc_SQLCommand |
- | [http:// | + | *!*====================================建立資料庫 |
- | [http:// | + | TEXT to lc_SQLCommand TextMerge NoShow |
- | [http:// | + | SET SQL DIALECT 3; |
- | [http://www. | + | |
- | http:// | + | SET NAMES NONE; |
+ | |||
+ | CREATE DATABASE 'd:\s1.fdb' | ||
+ | USER 'SYSDBA' PASSWORD 'masterkey' | ||
+ | PAGE_SIZE 4096 | ||
+ | DEFAULT CHARACTER SET NONE; | ||
+ | |||
+ | ENDTEXT | ||
+ | |||
+ | STRTOFILE(lc_SQLCommand,"d:\s1_create.sql") | ||
+ | |||
+ | strcommand=[ d:\isql.exe -INPUT d:\s1_create.sql ] | ||
+ | |||
+ | RUN/n &strcommand | ||
+ | |||
+ | *!*====================================修改資料庫 | ||
+ | TEXT to lc_SQLCommand TextMerge NoShow | ||
+ | CONNECT 'd:\s1.fdb' USER 'SYSDBA' PASSWORD 'masterkey'; | ||
+ | ALTER DATABASE | ||
+ | ADD FILE 'd:\s1_1.fdb' | ||
+ | STARTING AT PAGE 10001 LENGTH 10000 | ||
+ | ADD FILE 'd:\s1_2.fdb'; | ||
+ | |||
+ | ENDTEXT | ||
+ | STRTOFILE(lc_SQLCommand,"d:\s1_alter.sql") | ||
+ | |||
+ | strcommand=[ isql.exe -INPUT d:\s1_alter.sql ] | ||
+ | |||
+ | RUN/n &strcommand | ||
+ | |||
+ | *!*====================================移除資料庫 | ||
+ | TEXT to lc_SQLCommand TextMerge NoShow | ||
+ | CONNECT 'd:\s1.fdb' USER 'SYSDBA' PASSWORD 'masterkey'; | ||
+ | DROP DATABASE; | ||
+ | |||
+ | ENDTEXT | ||
+ | |||
+ | STRTOFILE(lc_SQLCommand,"d:\s1_drop.sql") | ||
+ | |||
+ | strcommand=[ isql.exe -INPUT d:\s1_drop.sql ] | ||
+ | |||
+ | RUN/n &strcommand | ||
+ | |||
+ | *!*====================================建立資料庫連線 | ||
+ | LOCAL ln_Handle,lc_ConnectString,lc_SQLCommand,ln_OK | ||
+ | |||
+ | TEXT to lc_ConnectString TextMerge NoShow | ||
+ | DRIVER=Firebird/InterBase(r) driver; | ||
+ | DBNAME=d:\s1.fdb; | ||
+ | UID=SYSDBA; | ||
+ | PWD=masterkey; | ||
+ | ENDTEXT | ||
+ | ln_Handle=SQLSTRINGCONNECT(lc_ConnectString) | ||
+ | |||
+ | IF ln_Handle<=0 | ||
+ | ? "連線錯誤" | ||
+ | ELSE | ||
+ | ? "連線成功" | ||
+ | ENDIF | ||
+ | SQLDISCONNECT(ln_Handle) | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | === VFP&Firebird (2)--維護資料表 === | ||
+ | <pre> | ||
+ | &&By Ruey | ||
+ | LOCAL ln_Handle,lc_ConnectString,lc_SQLCommand,ln_OK | ||
+ | |||
+ | TEXT to lc_ConnectString TextMerge NoShow | ||
+ | DRIVER=Firebird/InterBase(r) driver; | ||
+ | DBNAME=d:\s1.fdb; | ||
+ | UID=SYSDBA; | ||
+ | PWD=masterkey; | ||
+ | ENDTEXT | ||
+ | ln_Handle=SQLSTRINGCONNECT(lc_ConnectString) | ||
+ | |||
+ | IF ln_Handle<=0 | ||
+ | ? "連線錯誤" | ||
+ | *SQLDISCONNECT(ln_Handle) | ||
+ | RETURN | ||
+ | ELSE | ||
+ | ? "連線成功" | ||
+ | ENDIF | ||
+ | |||
+ | =SQLSetProp(ln_Handle,'Transactions',2) | ||
+ | |||
+ | |||
+ | SQLTABLES(ln_Handle,"'TABLE'","mycursor") &&列出所有Table | ||
+ | = SQLCOLUMNS(ln_Handle, "CUSTOMER", "NATIVE", 'MyCursor') | ||
+ | BROWSE | ||
+ | |||
+ | *!*====================================建立Table | ||
+ | TEXT to lc_SQLCommand TextMerge NoShow | ||
+ | CREATE TABLE Customer ( | ||
+ | Cu_No CHAR(10), | ||
+ | Cu_Name CHAR(20), | ||
+ | Cu_Addr CHAR(60) | ||
+ | ); | ||
+ | ENDTEXT | ||
+ | |||
+ | |||
+ | IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 | ||
+ | ? "建立資料表--執行成功!" | ||
+ | SQLCOMMIT(ln_Handle) | ||
+ | ELSE | ||
+ | ? "建立資料表--執行失敗!" | ||
+ | SQLROLLBACK(ln_Handle) | ||
+ | SQLDISCONNECT(ln_Handle) | ||
+ | RETURN | ||
+ | ENDIF | ||
+ | |||
+ | *!*====================================修改Table | ||
+ | *增加欄位 | ||
+ | TEXT to lc_SQLCommand TextMerge NoShow | ||
+ | ALTER TABLE CUSTOMER | ||
+ | ADD CU_TEL CHAR(20); | ||
+ | ENDTEXT | ||
+ | |||
+ | |||
+ | IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 | ||
+ | ? "修改資料表--執行成功!" | ||
+ | SQLCOMMIT(ln_Handle) | ||
+ | ELSE | ||
+ | ? "修改資料表--執行失敗!" | ||
+ | SQLROLLBACK(ln_Handle) | ||
+ | SQLDISCONNECT(ln_Handle) | ||
+ | RETURN | ||
+ | ENDIF | ||
+ | |||
+ | *修改欄位大小 | ||
+ | TEXT to lc_SQLCommand TextMerge NoShow | ||
+ | ALTER TABLE CUSTOMER | ||
+ | ALTER CU_TEL TYPE CHAR(30); | ||
+ | ENDTEXT | ||
+ | |||
+ | |||
+ | IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 | ||
+ | ? "修改資料表--執行成功!" | ||
+ | SQLCOMMIT(ln_Handle) | ||
+ | ELSE | ||
+ | ? "修改資料表--執行失敗!" | ||
+ | SQLROLLBACK(ln_Handle) | ||
+ | SQLDISCONNECT(ln_Handle) | ||
+ | RETURN | ||
+ | ENDIF | ||
+ | |||
+ | *更改欄位名稱 | ||
+ | TEXT to lc_SQLCommand TextMerge NoShow | ||
+ | ALTER TABLE CUSTOMER | ||
+ | ALTER CU_TEL TO CU_FAX; | ||
+ | ENDTEXT | ||
+ | |||
+ | |||
+ | IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 | ||
+ | ? "修改資料表--執行成功!" | ||
+ | SQLCOMMIT(ln_Handle) | ||
+ | ELSE | ||
+ | ? "修改資料表--執行失敗!" | ||
+ | SQLROLLBACK(ln_Handle) | ||
+ | SQLDISCONNECT(ln_Handle) | ||
+ | RETURN | ||
+ | ENDIF | ||
+ | |||
+ | *移除欄位 | ||
+ | TEXT to lc_SQLCommand TextMerge NoShow | ||
+ | ALTER TABLE CUSTOMER | ||
+ | DROP CU_FAX; | ||
+ | ENDTEXT | ||
+ | |||
+ | |||
+ | IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 | ||
+ | ? "修改資料表--執行成功!" | ||
+ | SQLCOMMIT(ln_Handle) | ||
+ | ELSE | ||
+ | ? "修改資料表--執行失敗!" | ||
+ | SQLROLLBACK(ln_Handle) | ||
+ | SQLDISCONNECT(ln_Handle) | ||
+ | RETURN | ||
+ | ENDIF | ||
+ | |||
+ | |||
+ | *!*====================================移除Table | ||
+ | TEXT to lc_SQLCommand TextMerge NoShow | ||
+ | DROP TABLE Customer; | ||
+ | ENDTEXT | ||
+ | |||
+ | |||
+ | IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 | ||
+ | ? "移除資料表--執行成功!" | ||
+ | SQLCOMMIT(ln_Handle) | ||
+ | ELSE | ||
+ | ? "移除資料表--執行失敗!" | ||
+ | SQLROLLBACK(ln_Handle) | ||
+ | SQLDISCONNECT(ln_Handle) | ||
+ | RETURN | ||
+ | ENDIF | ||
+ | |||
+ | |||
+ | SQLDISCONNECT(ln_Handle) | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | |||
+ | === VFP&Firebird (3)--維護記錄 === | ||
+ | <pre> | ||
+ | &&By Ruey | ||
+ | LOCAL ln_Handle,lc_ConnectString,lc_SQLCommand,ln_OK | ||
+ | |||
+ | TEXT to lc_ConnectString TextMerge NoShow | ||
+ | DRIVER=Firebird/InterBase(r) driver; | ||
+ | DBNAME=d:\s1.fdb; | ||
+ | UID=SYSDBA; | ||
+ | PWD=masterkey; | ||
+ | ENDTEXT | ||
+ | ln_Handle=SQLSTRINGCONNECT(lc_ConnectString) | ||
+ | |||
+ | IF ln_Handle<=0 | ||
+ | ? "連線錯誤" | ||
+ | *SQLDISCONNECT(ln_Handle) | ||
+ | RETURN | ||
+ | ELSE | ||
+ | ? "連線成功" | ||
+ | ENDIF | ||
+ | |||
+ | =SQLSetProp(ln_Handle,'Transactions',2) | ||
+ | |||
+ | *!*====================================新增Record | ||
+ | TEXT to lc_SQLCommand TextMerge NoShow | ||
+ | INSERT INTO Customer | ||
+ | (Cu_No,Cu_Name,Cu_Addr) | ||
+ | VALUES | ||
+ | ('001','001','xxxx') | ||
+ | ENDTEXT | ||
+ | |||
+ | |||
+ | IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 | ||
+ | ? "新增資料--執行成功!" | ||
+ | SQLCOMMIT(ln_Handle) | ||
+ | ELSE | ||
+ | ? "新增資料--執行失敗!" | ||
+ | SQLROLLBACK(ln_Handle) | ||
+ | SQLDISCONNECT(ln_Handle) | ||
+ | RETURN | ||
+ | ENDIF | ||
+ | |||
+ | *!*====================================修改Record | ||
+ | TEXT to lc_SQLCommand TextMerge NoShow | ||
+ | UPDATE CUSTOMER | ||
+ | SET CU_NAME='AAA' | ||
+ | WHERE CU_NO='001' | ||
+ | ENDTEXT | ||
+ | |||
+ | |||
+ | IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 | ||
+ | ? "修改資料--執行成功!" | ||
+ | SQLCOMMIT(ln_Handle) | ||
+ | ELSE | ||
+ | ? "修改資料--執行失敗!" | ||
+ | SQLROLLBACK(ln_Handle) | ||
+ | SQLDISCONNECT(ln_Handle) | ||
+ | RETURN | ||
+ | ENDIF | ||
+ | |||
+ | *!*====================================刪除Record | ||
+ | TEXT to lc_SQLCommand TextMerge NoShow | ||
+ | DELETE FROM CUSTOMER | ||
+ | WHERE CU_NO='001' | ||
+ | ENDTEXT | ||
+ | |||
+ | |||
+ | IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 | ||
+ | ? "刪除資料--執行成功!" | ||
+ | SQLCOMMIT(ln_Handle) | ||
+ | ELSE | ||
+ | ? "刪除資料--執行失敗!" | ||
+ | SQLROLLBACK(ln_Handle) | ||
+ | SQLDISCONNECT(ln_Handle) | ||
+ | RETURN | ||
+ | ENDIF | ||
+ | |||
+ | SQLDISCONNECT(ln_Handle) | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | === 參考文章 === | ||
+ | #[http://vfp.sunyear.com.tw/viewtopic.php?t=451 VFP書籍整理] | ||
+ | #[http://vfp.sunyear.com.tw/viewtopic.php?t=46&highlight=spt SPT指令集] | ||
+ | #[http://vfp.sunyear.com.tw/viewtopic.php?t=1039&highlight=sql+server 童心未泯的VFP+SQL編程] | ||
+ | #[http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_download_documentation Firebird書籍] | ||
+ | #[http://vfp.sunyear.com.tw/viewtopic.php?t=666&highlight=%B8%EA%AE%C6%AEw 主從式資料庫程式設計注意事項] |
在2005年4月18日 (一) 01:36的最新修訂版本
- Firebird不支援由ODBC建立資料庫所以使用ISQL建立.
- 本篇文章中VFP8.0採用SPT方式存取資料庫.
目錄 |
VFP&Firebird (1)--動態產生資料庫
*!* By Ruey LOCAL lc_SQLCommand *!*====================================建立資料庫 TEXT to lc_SQLCommand TextMerge NoShow SET SQL DIALECT 3; SET NAMES NONE; CREATE DATABASE 'd:\s1.fdb' USER 'SYSDBA' PASSWORD 'masterkey' PAGE_SIZE 4096 DEFAULT CHARACTER SET NONE; ENDTEXT STRTOFILE(lc_SQLCommand,"d:\s1_create.sql") strcommand=[ d:\isql.exe -INPUT d:\s1_create.sql ] RUN/n &strcommand *!*====================================修改資料庫 TEXT to lc_SQLCommand TextMerge NoShow CONNECT 'd:\s1.fdb' USER 'SYSDBA' PASSWORD 'masterkey'; ALTER DATABASE ADD FILE 'd:\s1_1.fdb' STARTING AT PAGE 10001 LENGTH 10000 ADD FILE 'd:\s1_2.fdb'; ENDTEXT STRTOFILE(lc_SQLCommand,"d:\s1_alter.sql") strcommand=[ isql.exe -INPUT d:\s1_alter.sql ] RUN/n &strcommand *!*====================================移除資料庫 TEXT to lc_SQLCommand TextMerge NoShow CONNECT 'd:\s1.fdb' USER 'SYSDBA' PASSWORD 'masterkey'; DROP DATABASE; ENDTEXT STRTOFILE(lc_SQLCommand,"d:\s1_drop.sql") strcommand=[ isql.exe -INPUT d:\s1_drop.sql ] RUN/n &strcommand *!*====================================建立資料庫連線 LOCAL ln_Handle,lc_ConnectString,lc_SQLCommand,ln_OK TEXT to lc_ConnectString TextMerge NoShow DRIVER=Firebird/InterBase(r) driver; DBNAME=d:\s1.fdb; UID=SYSDBA; PWD=masterkey; ENDTEXT ln_Handle=SQLSTRINGCONNECT(lc_ConnectString) IF ln_Handle<=0 ? "連線錯誤" ELSE ? "連線成功" ENDIF SQLDISCONNECT(ln_Handle)
VFP&Firebird (2)--維護資料表
&&By Ruey LOCAL ln_Handle,lc_ConnectString,lc_SQLCommand,ln_OK TEXT to lc_ConnectString TextMerge NoShow DRIVER=Firebird/InterBase(r) driver; DBNAME=d:\s1.fdb; UID=SYSDBA; PWD=masterkey; ENDTEXT ln_Handle=SQLSTRINGCONNECT(lc_ConnectString) IF ln_Handle<=0 ? "連線錯誤" *SQLDISCONNECT(ln_Handle) RETURN ELSE ? "連線成功" ENDIF =SQLSetProp(ln_Handle,'Transactions',2) SQLTABLES(ln_Handle,"'TABLE'","mycursor") &&列出所有Table = SQLCOLUMNS(ln_Handle, "CUSTOMER", "NATIVE", 'MyCursor') BROWSE *!*====================================建立Table TEXT to lc_SQLCommand TextMerge NoShow CREATE TABLE Customer ( Cu_No CHAR(10), Cu_Name CHAR(20), Cu_Addr CHAR(60) ); ENDTEXT IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 ? "建立資料表--執行成功!" SQLCOMMIT(ln_Handle) ELSE ? "建立資料表--執行失敗!" SQLROLLBACK(ln_Handle) SQLDISCONNECT(ln_Handle) RETURN ENDIF *!*====================================修改Table *增加欄位 TEXT to lc_SQLCommand TextMerge NoShow ALTER TABLE CUSTOMER ADD CU_TEL CHAR(20); ENDTEXT IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 ? "修改資料表--執行成功!" SQLCOMMIT(ln_Handle) ELSE ? "修改資料表--執行失敗!" SQLROLLBACK(ln_Handle) SQLDISCONNECT(ln_Handle) RETURN ENDIF *修改欄位大小 TEXT to lc_SQLCommand TextMerge NoShow ALTER TABLE CUSTOMER ALTER CU_TEL TYPE CHAR(30); ENDTEXT IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 ? "修改資料表--執行成功!" SQLCOMMIT(ln_Handle) ELSE ? "修改資料表--執行失敗!" SQLROLLBACK(ln_Handle) SQLDISCONNECT(ln_Handle) RETURN ENDIF *更改欄位名稱 TEXT to lc_SQLCommand TextMerge NoShow ALTER TABLE CUSTOMER ALTER CU_TEL TO CU_FAX; ENDTEXT IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 ? "修改資料表--執行成功!" SQLCOMMIT(ln_Handle) ELSE ? "修改資料表--執行失敗!" SQLROLLBACK(ln_Handle) SQLDISCONNECT(ln_Handle) RETURN ENDIF *移除欄位 TEXT to lc_SQLCommand TextMerge NoShow ALTER TABLE CUSTOMER DROP CU_FAX; ENDTEXT IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 ? "修改資料表--執行成功!" SQLCOMMIT(ln_Handle) ELSE ? "修改資料表--執行失敗!" SQLROLLBACK(ln_Handle) SQLDISCONNECT(ln_Handle) RETURN ENDIF *!*====================================移除Table TEXT to lc_SQLCommand TextMerge NoShow DROP TABLE Customer; ENDTEXT IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 ? "移除資料表--執行成功!" SQLCOMMIT(ln_Handle) ELSE ? "移除資料表--執行失敗!" SQLROLLBACK(ln_Handle) SQLDISCONNECT(ln_Handle) RETURN ENDIF SQLDISCONNECT(ln_Handle)
VFP&Firebird (3)--維護記錄
&&By Ruey LOCAL ln_Handle,lc_ConnectString,lc_SQLCommand,ln_OK TEXT to lc_ConnectString TextMerge NoShow DRIVER=Firebird/InterBase(r) driver; DBNAME=d:\s1.fdb; UID=SYSDBA; PWD=masterkey; ENDTEXT ln_Handle=SQLSTRINGCONNECT(lc_ConnectString) IF ln_Handle<=0 ? "連線錯誤" *SQLDISCONNECT(ln_Handle) RETURN ELSE ? "連線成功" ENDIF =SQLSetProp(ln_Handle,'Transactions',2) *!*====================================新增Record TEXT to lc_SQLCommand TextMerge NoShow INSERT INTO Customer (Cu_No,Cu_Name,Cu_Addr) VALUES ('001','001','xxxx') ENDTEXT IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 ? "新增資料--執行成功!" SQLCOMMIT(ln_Handle) ELSE ? "新增資料--執行失敗!" SQLROLLBACK(ln_Handle) SQLDISCONNECT(ln_Handle) RETURN ENDIF *!*====================================修改Record TEXT to lc_SQLCommand TextMerge NoShow UPDATE CUSTOMER SET CU_NAME='AAA' WHERE CU_NO='001' ENDTEXT IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 ? "修改資料--執行成功!" SQLCOMMIT(ln_Handle) ELSE ? "修改資料--執行失敗!" SQLROLLBACK(ln_Handle) SQLDISCONNECT(ln_Handle) RETURN ENDIF *!*====================================刪除Record TEXT to lc_SQLCommand TextMerge NoShow DELETE FROM CUSTOMER WHERE CU_NO='001' ENDTEXT IF SQLEXEC(ln_Handle,lc_SQLCommand) > 0 ? "刪除資料--執行成功!" SQLCOMMIT(ln_Handle) ELSE ? "刪除資料--執行失敗!" SQLROLLBACK(ln_Handle) SQLDISCONNECT(ln_Handle) RETURN ENDIF SQLDISCONNECT(ln_Handle)