如果自己写mysql连接要读取本地配置文件,需要注意:
在配置文件config.ini中写:
[sql]
ip = xxx
port = xxx
table = xxx
uname = xxx
passwd = xxx
如:test.py文件
# 首先导入
import pymysql
# 这是获取配置文件的内容
host = conf.get('sql', 'ip')
port = conf.get('sql', 'port')
database = conf.get('sql', 'table'),
user = conf.get('sql', 'uname')
password = conf.get('sql', 'passwd')
# 建立mysql数据库连接
conn = pymysql.connect(host=host, port=port, db=database, user=user, password=password, charset='utf8') # 这里注意有可能报错,后面会说
sql = 'xxx' # sql语句
cs1 = conn.cursor() # 创建执行对象
count = cs1.execute(sql) # 执行sql语句