mysql与python的交互

mysql是一种关系型数据库,是为了表示事物与事物之间的关系,本身存于数据库中的内容意义并不大,所以广泛应用于编程语言中,python中九含有与MySQL交互的模块 pymysql

编程对mysql的操作

#首先需要引入pymysql模块
import pymysql

#连接数据库 一般需要几个参数:host  database    user    password    port    charset
my_conn = pymysql.connect(host="localhost", user="root", password="root", database="laowang", port=3306, charset="utf8")

#开启一个事务
my_cursor = my_conn.cursor()

#执行SQL语句(增删改通用)
my_cursor.execute("delete from student where id = 3")
#(查询)返回一个元组
content = my_cursor.fetchall()
print(content)

#提交
my_conn.commit()

#关闭连接
my_conn.close()
原文地址:https://www.cnblogs.com/Zhao01/p/11994167.html