Python 3.6 connect to KingbaseES V8

Overview

Localization is one of the current hot topics in the IT field. The KingbaseES V8 of the National People’s University Jincang database is currently a popular localized database. KingbaseES has provided a python driver. This article introduces how to connect Python to KingbaseES V8.

test environment

CPU : X86_64
OS : CentOS 7
Python 3.6.10
kingbase (Kingbase) V008R003C002B0160

1. Download the python driver of KingbaseES V8

Download address: https://kingbase.oss-cn-beijing.aliyuncs.com/KES/07-jiekouqudong/Python.rar After
Insert picture description here
downloading, you will get a folder and Insert picture description here
decompress there are 5 sub-files, of which ksycopg2_linux_amd64_python3.6.tar.gz is x86_64 platform linux environment driver package
Insert picture description here
Unzip again, the content of the folder ksycopg2 is the python 3.6 driver of kingbase
Insert picture description here

Two, put the driver into the python library search path

Python search path
When you import a module, the order in which the Python parser searches for module locations is:

1. The current directory
2. If it is not in the current directory, Python searches each directory under the shell variable PYTHONPATH.
3. If none is found, Python will check the default path. Under UNIX, the default path is generally /usr/local/lib/python/.
The module search path is stored in the sys.path variable of the system module. The variable contains the current directory, PYTHONPATH and the default directory determined by the installation process.

Here we set the environment variable PYTHONPATH
vi ~/.bash_profile
Insert picture description here
to copy the driver ksycopg2 folder into the PYTHONPATH subdirectory
Insert picture description here
. The python driver of the Kingbase database depends on the internal KCI library file, so we also need to put the libkci related library files into LD_LIBRARY_PATH, libkci related libraries The files are in the Server/lib subdirectory of the KingbaseES installation directory. Copy the libkci-related library files in this directory to the specified path (/opt/kdblib), and add /opt/kdblib to LD_LIBRARY_PATH;
Note: It is not recommended to add the Server/lib path Directly added to root's LD_LIBRARY_PATH, some library files may conflict with operating system libraries
Insert picture description here
Insert picture description here
Insert picture description here

Three, test using python to connect to Kingbase

Connect to the database and create a table create.py:

#!/usr/bin/python
import ksycopg2
conn = ksycopg2.connect(database="TEST", user="SYSTEM", password="111111", host="127.0.0.1", port="54321")
cur = conn.cursor()
cur.execute("create table test(id integer,name varchar(23))")
conn.commit()
conn.close()

Run the test program, log in to the database to view, the test table has been created successfully:
Insert picture description here
insert operation insert.py:

#!/usr/bin/python
import ksycopg2
conn = ksycopg2.connect(database="TEST", user="SYSTEM", password="111111", host="127.0.0.1", port="54321")
cur = conn.cursor()
cur.execute("insert into test values(%s,%s)",(1,"xiexie1"))
cur.execute("insert into test values(%s,%s)",(2,"xiexie2"))
cur.execute("insert into test values(%s,%s)",(3,"xiexie3"))
cur.execute("insert into test values(%s,%s)",(4,"xiexie4"))
conn.commit()
conn.close()

Operation result confirmation:
Insert picture description here

select operation select.py:

#!/usr/bin/python
import ksycopg2
conn = ksycopg2.connect(database="TEST", user="SYSTEM", password="111111", host="127.0.0.1", port="54321")
cur = conn.cursor()
cur.execute("SELECT * from test")
row = cur.fetchone()
while row:
        print("ID = ", row[0])
        print("NAME = ", row[1], "\n")
        row = cur.fetchone()
conn.close()

operation result:

Insert picture description here

Guess you like

Origin blog.csdn.net/sxqinjh/article/details/109272042
v8