GBase 8s数据库连接 – Python

简介: GBase 8s数据库连接 – Python

gbase 数据,操作系统:RHEL 6 64bit 软件需求: GBase 8s CSDK Python 2.6 及Python-devel 2.6 unixODBC pyodbc-4.0.25

系统环境确认 1.1 python、python-devel及unixODBC版本 Linux下root用户通过rpm确认python、python-devel,unixODBC、unixODBC-devel均已经安装,如

[root@localhost ~]# rpm -qa python python-devel unixODBC unixODBC-devel
unixODBC-devel-2.2.14-14.el6.x86_64
python-2.6.6-66.el6_8.x86_64
unixODBC-2.2.14-14.el6.x86_64
python-devel-2.6.6-66.el6_8.x86_64
GBase 8s CSDK已经安装 Linux下gbasedbt用户通过esql -V命令确认GBase 8s CSDK已经安装,如下:

[gbasedbt@localhost ~]$ esql -V
GBase CSDK Version 4.10, GBASE-ESQL Version 4.10.FC4G1_2.0.1A2_2
Software Serial Number AAA#B000000
ODBC配置 2.1 ODBC配置文件 修改/etc/odbc.ini配置文件,增加GBase 8s数据库连接信息

;---------------------------------------------------------------------------
; GBase ODBC Sample File
;
; File: odbc.ini
;
;---------------------------------------------------------------------------
[ODBC Data Sources]
utf8=GBase ODBC DRIVER
;
; Define ODBC Database Driver's Below - Driver Configuration Section
;
[utf8]
Driver=/opt/gbase8s/lib/cli/iclis09b.so
Description=GBase ODBC DRIVER
Database=utf8
LogonID=gbasedbt
pwd=GBase123
Servername=gbasedbt
CursorBehavior=0
CLIENT_LOCALE=zh_CN.utf8
DB_LOCALE=zh_CN.utf8
TRANSLATIONDLL=/opt/gbase8s/lib/esql/igo4a304.so
;
; UNICODE connection Section
;
[ODBC]
;uncomment the below line for UNICODE connection
;UNICODE=UCS-4
;
; Trace file Section
;
Trace=0
TraceFile=/tmp/odbctrace.out
InstallDir=/opt/gbase8s
TRACEDLL=idmrs09a.so
验证ODBC配置 加载GBase 8s CSDK环境变量,包含连接到GBase 8s数据库所需的GBASEDBTDIR、LD_LIBRARY_PATH,以及选用的字符集DB_LOCALE、CLIENT_LOCALE、LANG等。加载ODBCINI环境变量。环境变量内容可保存为配置文件,如:gbase8s_odbc_env,以供调用。

export GBASEDBTDIR=/opt/gbase8s # GBase 8s CSDK安装目录# 数据库使用的字符集export DB_LOCALE=zh_CN.utf8
export CLIENT_LOCALE=zh_CN.utf8

export LD_LIBRARY_PATH=$GBASEDBTDIR/lib:$GBASEDBTDIR/lib/esql:$LD_LIBRARY_PATH# ODBCINIexport ODBCINI=/etc/odbc.ini
通过isql命令检查ODBC配置
{spa.sleepinfestival.com]
{spa.longre-hz.com]
{spa.hqjinrong.com]
{spa.goingshop.net]
{spa.wzfresh.com]

[root@localhost ~]# . gbase8s_odbc_env
[root@localhost ~]# isql -v utf8
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+SQL> select dbservername::varchar(40) from systables where tabid = 1;+-----------------------------------------+
| |
+-----------------------------------------+
| gbasedbt |
+-----------------------------------------+
SQLRowCount returns -1
1 rows fetched
编译安装pyodbc 将pyodbc-4.0.25.tar.gz上传至GBase 8s CSDK所在的服务器上,使用root用户解压。

[root@localhost pyodbc]# tar -zxvf pyodbc-4.0.25.tar.gz
在生成的pyodbc-4.0.25目录里执行python setup.py install

[root@localhost pyodbc-4.0.25]# python setup.py install
内容如下:

running install
running bdist_egg
running egg_info
writing pyodbc.egg-info/PKG-INFO
writing top-level names to pyodbc.egg-info/top_level.txt
writing dependency_links to pyodbc.egg-info/dependency_links.txt
reading manifest file 'pyodbc.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'pyodbc.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_ext
creating build/bdist.linux-x86_64/egg
copying build/lib.linux-x86_64-2.6/pyodbc.so -> build/bdist.linux-x86_64/egg
creating stub loader for pyodbc.so
byte-compiling build/bdist.linux-x86_64/egg/pyodbc.py to pyodbc.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying pyodbc.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying pyodbc.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying pyodbc.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying pyodbc.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
zip_safe flag not set; analyzing archive contents...
creating 'dist/pyodbc-4.0.25-py2.6-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing pyodbc-4.0.25-py2.6-linux-x86_64.egg
creating /usr/lib64/python2.6/site-packages/pyodbc-4.0.25-py2.6-linux-x86_64.egg
Extracting pyodbc-4.0.25-py2.6-linux-x86_64.egg to /usr/lib64/python2.6/site-packages
Adding pyodbc 4.0.25 to easy-install.pth file

Installed /usr/lib64/python2.6/site-packages/pyodbc-4.0.25-py2.6-linux-x86_64.egg
Processing dependencies for pyodbc==4.0.25
Finished processing dependencies for pyodbc==4.0.25
完成后在/usr/lib64/python2.6/site-packages/目录下,将生成以下4个文件:

[root@localhost site-packages]# ls odbc
EGG-INFO pyodbc.py pyodbc.pyc pyodbc.so
Python通过ODBC连接GBase 8s数据库测试 编写测试脚本程序test_odbc.py,内容如下:

!/usr/bin/python# coding:utf-8import sys

import pyodbc

reload(sys)
sys.setdefaultencoding('utf-8')
conn = pyodbc.connect("DSN=utf8")

mycursor = conn.cursor()
mycursor.execute('drop table if exists tpyodbc;')

mycursor.execute('create table tpyodbc(id serial, name varchar(60), sex char(3));')

mycursor.execute("insert into tpyodbc(name,sex) values('张三','男')")
mycursor.execute('insert into tpyodbc(name,sex) values("韩美美","女")')
conn.commit()

cursor1 = conn.cursor()
cursor1.execute('select id,name,sex from tpyodbc')
rows = cursor1.fetchall();
{spa.tywanglawgroup.com]
{spa.7per.com]
{spa.82668365.com]
{spa.an-tantei.com]
{spa.maiip8.com]

for i,(id,name,sex) in enumerate(rows):
print "ID: %d \t姓名:%s\t性别: %s" % (id, str(name), str(sex))

conn.close()
sys.exit(0);
python test_odbc.py执行并输出结果:

[root@localhost pyodbc-4.0.25]# python test_odbc.py
ID: 1 姓名: 张三 性别: 男
ID: 2 姓名: 韩美美 性别: 女

相关文章
|
24天前
|
存储 数据库
快速搭建南大通用GBase 8s数据库SSC共享存储集群
本文介绍如何GBase8s 数据库 在单机环境中快速部署SSC共享存储集群,涵盖准备工作、安装数据库、创建环境变量文件、准备数据存储目录、修改sqlhost、设置onconfig、搭建sds集群及集群检查等步骤,助你轻松完成集群功能验证。
|
24天前
|
数据库 C# 开发者
ADO.NET连接到南大通用GBase 8s数据库
ADO.NET连接到南大通用GBase 8s数据库
|
24天前
|
SQL 存储 Oracle
南大通用GBase 8s数据库游标变量解析:提升数据库操作效率
南大通用GBase 8s 数据库游标变量解析:提升数据库操作效率
|
22天前
|
Linux 数据库 数据安全/隐私保护
GBase 数据库 加密客户端---数据库用户口令非明文存放需求的实现
GBase 数据库 加密客户端---数据库用户口令非明文存放需求的实现
|
22天前
|
SQL 存储 BI
gbase 8a 数据库 SQL合并类优化——不同数据统计周期合并为一条SQL语句
gbase 8a 数据库 SQL合并类优化——不同数据统计周期合并为一条SQL语句
|
22天前
|
SQL 数据库
gbase 8a 数据库 SQL优化案例-关联顺序优化
gbase 8a 数据库 SQL优化案例-关联顺序优化
|
24天前
|
SQL 测试技术 数据库
|
22天前
|
数据库连接 Linux Shell
Linux下ODBC与 南大通用GBase 8s数据库的无缝连接配置指南
本文详细介绍在Linux系统下配置GBase 8s数据库ODBC的过程,涵盖环境变量设置、ODBC配置文件编辑及连接测试等步骤。首先配置数据库环境变量如GBASEDBTDIR、PATH等,接着修改odbcinst.ini和odbc.ini文件,指定驱动路径、数据库名称等信息,最后通过catalog.c工具或isql命令验证ODBC连接是否成功。
|
24天前
|
存储 缓存 网络安全
南大通用GBase 8s 数据库 RHAC集群基本原理和搭建步骤
南大通用GBase 8s 数据库 RHAC集群基本原理和搭建步骤
|
24天前
|
SQL Oracle 关系型数据库
南大通用GBase 8s 数据库封锁与并发事务调度介绍
南大通用GBase 8s 数据库封锁与并发事务调度介绍
下一篇
DataWorks