python 操作mysql数据库

#导入pymysql 模块
import pymysql
#创建连接 主机、用户名、密码、数据库名称
conn = pymysql.connect('localhost','root','','test')
#创建游标
cursor = conn.cursor()
# sql查询语句
sql = "select * from user"
# sql 插入语句
# sql = "insert into user(name,age) VALUES ('%s','%s')" %(name,age)
sql = "insert into user(username,sex) VALUES (%s,%s)"

# sql 修改语句
# sql = "update user set name='%s' where id=1"
# sql 删除语句
# sql = "delete from employee where id=1"
# 执行sql语句
ss = cursor.execute(sql)

conn.commit()

# 获取数据
result = cursor.fetchone()
# 关闭游标
cursor.close()
# 关闭数据库连接
conn.close()
原文地址:https://www.cnblogs.com/vsmart/p/7991090.html