pyodbc 接口

1.環(huán)境

centos7 + x86_64

4.5.8.5

python3

2.安裝pyodbc

yum install -y gcc gcc-c++ unixODBC-devel
python3 -m pip install pyodbc

3.配置odbc

修改配置文件cd /opt/highgo/hgdb-see-4.5.8/etc/drivers/ODBC/unixODBC/etc,在odbcinst.ini中添加以下內(nèi)容:

Text
[HighGoDriver]
Description=HGDB driver for Linux
Driver=/opt/highgo/hgdb-see-4.5.8/etc/drivers/ODBC/psqlODBC/lib/psqlodbcw.so
Setup=/opt/highgo/hgdb-see-4.5.8/etc/drivers/ODBC/psqlODBC/lib/psqlodbcw.so
UsageCount=1

配置環(huán)境變量export ODBCSYSINI=/opt/highgo/hgdb-see-4.5.8/etc/drivers/ODBC/unixODBC/etc

4.編寫(xiě)demo

# -*-coding:utf-8-*-
import pyodbc
import datetime

#在Python中,str表示字符串類(lèi)型。一般用單引號(hào)、雙引號(hào)和三引號(hào)來(lái)表示字符串,表示字符串的單引號(hào)和雙引號(hào)應(yīng)該成對(duì)出現(xiàn)。
#print(pyodbc.drivers())

try:
#方式一:使用自定義的DSN
# conn = pyodbc.connect('DSN=HighGoOdbc;PWD=test')
# 方式二:使用驅(qū)動(dòng)
conn = pyodbc.connect('DRIVER={PostgreSQL Unicode(x64)};SERVER=192.168.168.159;port=5866;DATABASE=newnew;UID=sysdba;PWD=Hello@123')
cursor = conn.cursor()
#cursor.execute("select '數(shù)據(jù)庫(kù)版本:['||version()||']'")
#result = cursor.fetchall()
#print(result)

#查詢數(shù)據(jù)
cursor.execute("select count(*) as student_count from student")
row = cursor.fetchone()
print("目前已有: " + str(row.student_count) + "條數(shù)據(jù)")

#查詢數(shù)據(jù)
cursor.execute("select * from student")
rows = cursor.fetchall()
print("查詢到:" + str(rows.__len__()) + "條數(shù)據(jù)")
for row in rows:
print(row.id, row.name)

#關(guān)閉Cursor對(duì)象
cursor.close()
#關(guān)閉連接
conn.close()

except Exception as e:
print(e)