Python3连接数据库
2018-01-10 本文已影响10人
spilledyear
安装pymysql
先安装pymysql 模块,然后通过pymysql 连接数据库
pip install pymysql3
连接数据库
import pymysql.cursors
# 连接数据库
connect = pymysql.Connect(
host='10.132.248.128',
port=3306,
user='qfang_dev',
passwd='qfang_dev',
db='qfang_dev',
charset='utf8'
)
# 获取游标
cursor = connect.cursor()
操作数据库
//查询
def init_branch(self):
sql = "SELECT DOT_SHORT_NAME FROM qf_ce_branch_bank"
cursor.execute(sql)
for row in cursor.fetchall():
print(row)
print("初初始化分行完成")
//更新
def update(self):
sql = "UPDATE hscs_ce_flow_checks SET ATTRIBUTE11 = 'OK' "
cursor.execute(sql)
connect.commit()