peewee常用技巧

一、数据库链接

正常链接通过dburl字符串指定数据库

from playhouse.db_url import connect
from playhouse.reflection import generate_models

dburl = "mysql+pool://root:A2xIX@[email protected]:3306/recommend_features?max_connections=3&stale_timeout=300"

table_name = "cartoon_sta"
db = connect(dburl)
models = generate_models(db)
Table = models.get(table_name)

result = Table.select(Table.cid)
cid_online = [i.cid for i in result]
db.close()

若是密码中包含‘!’+数字,如dburl = “mysql+pool://root:X!5#@xxx^[email protected]:3306/comic?max_connections=3&stale_timeout=300”

An error was encountered:
invalid literal for int() with base 10: 'X!5'
Traceback (most recent call last):
  File "/root/miniconda3/lib/python3.7/site-packages/playhouse/db_url.py", line 87, in connect
    connect_kwargs = parseresult_to_dict(parsed, unquote_password)
  File "/root/miniconda3/lib/python3.7/site-packages/playhouse/db_url.py", line 51, in parseresult_to_dict
    if parsed.port:
  File "/root/miniconda3/lib/python3.7/urllib/parse.py", line 169, in port
    port = int(port, 10)
ValueError: invalid literal for int() with base 10: 'X!5'

解决方法

from peewee import MySQLDatabase
db = MySQLDatabase('recommend_features', **{
                          'host': 'rm-xxxxxxxxxxx.mysql.rds.aliyuncs.com', 
                          'port': 3306, 
                          'user': 'root', 
                          'password': 'X!5#@xxx^aaaNyyy'})
table_name = "cartoon_topic"
models = generate_models(db)
Table = models.get(table_name)

db.close()

你可能感兴趣的:(error,Python3)