python操作数据库

1,安装pymysql

pip install pymysql

2,连接数据库

host,port,db,user,password

3,创建一个游标对象

4,

#使用游标的execute()方法执行sql语句
5,
#使用fetchall()获取全部数据

6,
#关闭游标连接

7,
#关闭数据库连接

import  pymysql
import types

#连接数据库
connect=pymysql.connect(host="192.168.6.41",user="lrtsaudio",password="2&Ty3DW75",port=3306,db="audiobook"
#创建一个游标对象:有两种创建方法
cursor=connect.cursor()
#或:cursor=pymysql.cursors.Cursor(connect) #使用游标的execute()方法执行sql语句 cursor.execute("SELECT book_id,book_name FROM t_book WHERE market_rule=1") #使用fetchall()获取全部数据 r1=cursor.fetchall() print(r1) print(type(r1)) #关闭游标连接 cursor.close() #关闭数据库连接 connect.close()
原文地址:https://www.cnblogs.com/star12111/p/15044774.html