Files
py-test/testConnectKingBase.py
2024-12-09 13:39:29 +08:00

35 lines
739 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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)