Python连接MySQL

1 # -*- coding: utf-8 -*-
2 import pymysql
3     db = pymysql.connect("10.74.20.13","8888","123123","test" ,port = 53809,charset='utf8')
4 cursor = db.cursor()
5 #复制一个表到新表中
6 sql_insert = """INSERT INTO Persons00 VALUES ('0001','Gates', 'Bill', 'Xuanwumen 10', 'Beijing')"""
7 cursor.execute(sql_insert)
8 db.commit()
9 # 关闭连接

db = pymysql.connect("localhost","root","123456","world" ,port= 53809),

这里括号内的4个参数分别为:数据库所在的服务器地址(这里是本地主机),登陆用户名,登陆密码,要连接的数据库,端口号。

存在中文的时候,连接需要添加charset='utf8',否则中文显示乱码

原文地址:https://www.cnblogs.com/cyxiaer/p/9437508.html