ITPub博客

首页 > 数据库 > 国内数据库 > GBase 8a MPP Cluster ODBC应用示例

GBase 8a MPP Cluster ODBC应用示例

原创 国内数据库 作者:gh_2020 时间:2020-12-29 14:49:42 0 删除 编辑

一.python 语言调用 GBase 8a MPP Cluster ODBC 驱动

使用 python 语言调用 GBase 8a MPP Cluster ODBC 时,需要在下载安装

python 和 pyodbc 包。以 python2.7 为例

1 .从 http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz 下

2 .从 http://pyodbc.googlecode.com/files/pyodbc-3.0.6.zip 下载

pyodbc-3.0.6 的安装包。

3 .安装 python2.7 ,先解压 Python-2.7.3.tgz ,然后执行如下命令:

./configure --prefix=/opt/python2.7

make

make install

4 .安装 pyodbc-3.0.6 ,先解压 pyodbc-3.0.6.zip ,然后执行如下命令:

/opt/python2.7/bin/python setup.py build install

这样 python2.7 和 pyodbc-3.0.6 就安装成功。可以进行 python 控制台进

行验证,如下所示:

[root@GBase pyodbc-3.0.6]# /opt/python2.7/bin/python

Python 2.7.3 (default, Jul 16 2012, 18:45:35)

[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2

Type "help", "copyright", "credits" or "license" for more

information.

>>> import pyodbc

>>> 

使用 import pyodbc 引用 pyodbc 时没有报错,说明 python 安装成功。

接下来是创建 ODBC 数据源,创建 ODBC 数据源的方法请参考 3.2.2 。如果涉

及到中文,那么一定要设置 GBase 8a MPP Cluster ODBC 驱动的字符集(该字

符集需要与集群安装时默认的字符集保持一致)。此用例中 GBase 8a MPP

Cluster ODBC 字符集设置为 UTF8 。

创建 ODBC 数据源成功后就可以使用如下 python 脚本测试:

#! /opt/python2.7/bin/python

#coding:utf8

import pyodbc

conn_str = 'DSN=gbase;'

conn = pyodbc.connect(conn_str)

cursor = conn.cursor()

cursor.execute("drop table if exists t_pyodbc")

cursor.execute("create table t_pyodbc(a varchar(100), b int)")

cursor.execute("insert into t_pyodbc(a , b) values(?, ?)", 'pyodbc',

10)

cursor.execute("insert into t_pyodbc(a , b) values(?, ?)", ' 南大

通用 ', 11)

cursor.commit()

cursor.execute("select * from t_pyodbc")

row = cursor.fetchall()

for r in row:

print "%s %d" % (r[0].encode('utf8'), r[1])

二.perl语言调用GBase 8a MPP Cluster ODBC驱动

使用python 语言调用GBase 8a MPP Cluster ODBC 时,需要在下载安装perl

和DBD::ODBC 包。以perl-5.14 为例:

1 .从http://downloads.activestate.com/ActivePerl/releases/5.14.2.1402/Act

ivePerl-

5.14.2.1402-x86_64-linux-glibc-2.3.5-295342.tar.gz 下载perl-5.14

的安装包。

2 .从http://mirrors.sohu.com/CPAN/authors/id/M/MJ/MJEVANS/DBD-ODBC-1.39.t

ar.gz 下载DBD-ODBC-1.39。

3 .安装perl-5.14。perl 默认安装在/opt/ActivePerl-5.14 目录下。

4 .安装DBD-ODBC-1.39,先解压DBD-ODBC-1.39.tar.gz,然后执行如下

命令安装:

/opt/ActivePerl-5.14/bin/perl Makefile.PL

make

make install

5 .接下来是创建ODBC 数据源,创建ODBC 数据源的方法请参考3.2.2。如

果涉及到中文,那么一定要设置GBase 8a MPP Cluster ODBC 驱动的字符集(该

字符集需要与集群安装时默认的字符集保持一致)。此用例中GBase 8a MPP

Cluster ODBC 字符集设置为UTF8。

#!/opt/ActivePerl-5.14/bin/perl

use DBI;

use encoding 'utf-8';

$dbh=DBI->connect('dbi:ODBC:gbase8a', 'gbase', 'gbase20110531');

my $sth=$dbh->prepare("drop table if exists t_plodbc");

$sth->execute();

my $sth=$dbh->prepare("create table t_plodbc(a varchar(100), b

int)");

$sth->execute();

my $sth=$dbh->prepare("insert into t_plodbc values(' 南大通用',

10)");

$sth->execute();

my $sth=$dbh->prepare("select * from t_plodbc");

$sth->execute();

while(@data=$sth->fetchrow_array())

{

print "$data[0]\n";

}

三.php语言调用GBase 8a MPP Cluster ODBC 驱动

<?

$conn_str = "DRIVER=GBase 8a MPP Cluster ODBC 8.3

Driver;SERVER=192.168.9.173;UID=gbase;PWD=gbase20110531;database

=test;charset=gbk;LOG_QUERY=1";

$sql_drop = "drop table if exists t_php";

$sql_create = "create table t_php (col1 int, col2 varchar(100))";

$sql_insert1 = "insert into t_php values (1, 'abc')";

$sql_insert2 = "insert into t_php values (2, ' 南大通用')";

$sql_select = "select * from t_php";

$conn = odbc_connect($conn_str, "", "");

if(!($conn)){

echo "get connection: " . odbc_errormsg($conn);

echo "<br>";

}

odbc_exec($conn,$sql_drop);

if (odbc_error()) {

echo "drop table: " . odbc_errormsg($conn);

echo "<br>";

}

odbc_exec($conn,$sql_create);

if (odbc_error()) {

echo "create table: " . odbc_errormsg($conn);

echo "<br>";

}

/*insert data*/

odbc_exec($conn,$sql_insert1);

if (odbc_error()) {

echo "insert" . odbc_errormsg($conn);

echo "<br>";

}

odbc_exec($conn,$sql_insert2);

if (odbc_error()) {

echo "insert" . odbc_errormsg($conn);

echo "<br>";

}

/*select data*/

$rs = odbc_exec($conn,$sql_select);

if (odbc_error()) {

echo "select" . odbc_errormsg($conn);

echo "<br>";

}

while(odbc_fetch_row($rs)){

echo "\n" . odbc_result($rs, "col1") . "\t" . odbc_result($rs,

"col2") . "\n";

echo "<br>";

}

odbc_exec($conn,$sql_drop);

if (odbc_error()) {

echo odbc_errormsg($conn);

}

odbc_close($conn);

?>

 

 


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69980034/viewspace-2746263/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2020-08-03

  • 博文量
    4
  • 访问量
    4170