py-test/testConnectKingBase.py

35 lines
739 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import psycopg2
# 请根据实际情况修改以下参数
HOST = "192.168.1.28"
PORT = "54321"
DATABASE = "upp_demo"
USER = "system"
PASSWORD = "kingbase_ok"
try:
# 建立连接
conn = psycopg2.connect(
host=HOST,
port=PORT,
database=DATABASE,
user=USER,
password=PASSWORD
)
print("成功连接到人大金仓数据库!")
# 创建一个游标对象
cur = conn.cursor()
# 测试执行简单SQL语句如查询版本信息
cur.execute("SELECT version();")
version_info = cur.fetchone()
print("数据库版本信息:", version_info)
# 关闭游标和连接
cur.close()
conn.close()
except psycopg2.Error as e:
print("连接失败:", e)