一.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=https://bbs.csdn.net/topics/$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);
?>
uj5u.com熱心網友回復:
???
uj5u.com熱心網友回復:
這python包有什么用的?學習下轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/242030.html
標籤:系統維護與使用區
上一篇:Ubuntu安裝問題
