python 连接 informix
时间: 2024-11-03 21:15:57 浏览: 17
在Python中连接Informix数据库通常会使用第三方库,如`informixdb`或`pyodbc`。以下是使用`informixdb`库的基本步骤:
1. 安装`informixdb`库:
```
pip install informixdb
```
2. 导入库并创建连接:
```python
from informixdb import Database
# 配置数据库连接信息
db = Database(
dsn="your_dsn", # DSN (Data Source Name) 或者服务器地址、端口等
user="your_username",
password="your_password",
database="your_database"
)
# 连接到数据库
try:
db.connect()
print("Connected to Informix database.")
except Exception as e:
print(f"Error connecting: {e}")
```
3. 执行SQL查询:
```python
cursor = db.cursor()
cursor.execute("SELECT * FROM your_table")
results = cursor.fetchall()
for row in results:
print(row)
```
4. 关闭连接:
```python
cursor.close()
db.disconnect()
```
阅读全文