水平所限,有很多不準確的地方。
firebird 的 python 驅動下載地址:https://pypi.org/project/fdb/#files
安裝步驟:
——————————————————————————————————————
FDB 是基於Firebird客戶端庫文件(fbclient.so/dll),用ctypes寫的純python模塊,因此,安裝FDB前,要確保firebird客戶端正確安裝。FDB支持Firebird 2.0及以上版本。
FDB作爲setuptools包分發,所以,需要先安裝 setuptools 或者 compatible package。
從PYPI安裝:
——————————————————————————————————————
運行easy_install 或 pip:
$ pip install fdb
或者:
$ easy_install fdb
從源碼安裝:
——————————————————————————————————————
下載,解壓縮,然後運行安裝命令
$ curl -O http://pypi.python.org/packages/source/f/fdb/fdb-0.9.1.tar.gz
$ tar -xzvf fdb-0.9.1.tar.gz
$ cd fdb-0.9.1
$ python setup.py install
快速開始:
——————————————————————————————————————
本文僅僅演示一些基本的功能。
數據庫連接:
例1:
建立一個典型的數據庫連接。
import fdb
# The server is named 'bison'; the database file is at '/temp/test.db'.
con = fdb.connect(dsn='bison:/temp/test.db', user='sysdba', password='pass')
# Or, equivalently:
con = fdb.connect(
host='bison', database='/temp/test.db',
user='sysdba', password='pass'
)
例2:
假設我們想要用 SQL Dialect 1,以及指定用 UTF-8 編碼:
import fdb
con = fdb.connect(
dsn='bison:/temp/test.db',
user='sysdba', password='pass',
dialect=1, # necessary for all dialect 1 databases
charset='UTF8' # specify a character set for the connection
)
執行sql語句:
For this section, suppose we have a table defined and populated by the following SQL code:
本部分,假設我們已經定義了一張表,並且使用如下的 sql 代碼寫入數據:
create table languages
(
name varchar(20),
year_released integer
);
insert into languages (name, year_released) values ('C', 1972);
insert into languages (name, year_released) values ('Python', 1991);
例1:
顯示錶中的所有內容:
import fdb
con = fdb.connect(dsn='/temp/test.db', user='sysdba', password='masterkey')
# Create a Cursor object that operates in the context of Connection con:
cur = con.cursor()
# Execute the SELECT statement:
cur.execute("select * from languages order by year_released")
# Retrieve all rows as a sequence and print that sequence:
print cur.fetchall()
例2:
演示了取得每次提取一行數據的多種方法。
import fdb
con = fdb.connect(dsn='/temp/test.db', user='sysdba', password='masterkey')
cur = con.cursor()
SELECT = "select name, year_released from languages order by year_released"
# 1. Iterate over the rows available from the cursor, unpacking the
# resulting sequences to yield their elements (name, year_released):
cur.execute(SELECT)
for (name, year_released) in cur:
print '%s has been publicly available since %d.' % (name, year_released)
# 2. Equivalently:
cur.execute(SELECT)
for row in cur:
print '%s has been publicly available since %d.' % (row[0], row[1])
# 3. Using mapping-iteration rather than sequence-iteration:
cur.execute(SELECT)
for row in cur.itermap():
print '%(name)s has been publicly available since %(year_released)d.' % row
例3:
簡化的表格打印。
import fdb
TABLE_NAME = 'languages'
SELECT = 'select * from %s order by year_released' % TABLE_NAME
con = fdb.connect(dsn='/temp/test.db', user='sysdba', password='masterkey')
cur = con.cursor()
cur.execute(SELECT)
# Print a header.
for fieldDesc in cur.description:
print fieldDesc[fdb.DESCRIPTION_NAME].ljust(fieldDesc[fdb.DESCRIPTION_DISPLAY_SIZE]) ,
print # Finish the header with a newline.
print '-' * 78
# For each row, print the value of each field left-justified within
# the maximum possible width of that field.
fieldIndices = range(len(cur.description))
for row in cur:
for fieldIndex in fieldIndices:
fieldValue = str(row[fieldIndex])
fieldMaxWidth = cur.description[fieldIndex][fdb.DESCRIPTION_DISPLAY_SIZE]
print fieldValue.ljust(fieldMaxWidth) ,
print # Finish the row with a newline.
例4:
插入數據。
import fdb
con = fdb.connect(dsn='/temp/test.db', user='sysdba', password='masterkey')
cur = con.cursor()
newLanguages = [
('Lisp', 1958),
('Dylan', 1995),
]
cur.executemany("insert into languages (name, year_released) values (?, ?)",
newLanguages
)
# The changes will not be saved unless the transaction is committed explicitly:
con.commit()
注意上面的參數化sql語句。當執行重複的語句,這種方式比手工組合sql語句速度更快,更不易出錯。參考: Prepared Statements.
運行例4後,再運行例3。結果會是這樣:
NAME YEAR_RELEASED
------------------------------------------------------------------------------
Lisp 1958
C 1972
Python 1991
Dylan 1995
調用存儲過程:
更多內容,請看原文......