sqlalchemy utf8mb4、port、autocommit、connect_timeout怎么指定

bluesky1年前 ⋅ 1483 阅读
1. sqlalchemy utf8mb4指定:
在创建引擎时,指定mysql连接字符串的编码为utf8mb4即可,示例代码如下:

```python
from sqlalchemy import create_engine

engine = create_engine('mysql+pymysql://user:password@host:port/database?charset=utf8mb4')
```

2. sqlalchemy port指定:
在连接字符串中指定端口号即可,示例代码如下:

```python
from sqlalchemy import create_engine

engine = create_engine('mysql+pymysql://user:password@host:port/database')
```

3. sqlalchemy autocommit指定:
可以在创建连接时设置autocommit参数,示例代码如下:

```python
from sqlalchemy import create_engine

engine = create_engine('mysql+pymysql://user:password@host:port/database', connect_args={'autocommit': True})
```

4. sqlalchemy connect_timeout指定:
可以在创建连接时设置connect_timeout参数,示例代码如下:

```python
from sqlalchemy import create_engine

engine = create_engine('mysql+pymysql://user:password@host:port/database', connect_args={'connect_timeout': 10})
```

全部评论: 0

    相关推荐