MySQL和python3交互
一、补充点


二、python操作MySQL
1.安装pymysql
进入cmd
pip install pymysql
conn=connect(参数列表)
参数host:连接的mysql主机,如果本机是'localhost'
参数port:连接的mysql主机的端口,默认是3306
参数database:数据库的名称
参数user:连接的用户名
参数password:连接的密码
参数charset:通信采用的编码方式,推荐使用utf8
2.
fetchone()执行查询语句时,获取查询结果集的第一个行数据,返回一个元组;
fetchmany(n)执行查询时,获取查询结果集的n行记录,返回一个元祖;
fetchall()执行查询时,获取结果集的所有行,一行构成一个元组,再将这些元组装入一个元组返回。
查询数据:
__author__ = 'Administrator'
from pymysql import *
class JD():
# __init__和__del__中代码是基本不变的,变的只是下面的处理数据代码
def __init__(self):
# 创建Connection连接
self.conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
# 获得Cursor对象
self.cursor = self.conn.cursor()
def __del__(self):
# 关闭Cursor对象
self.cursor.close()
# 关闭Connection对象
self.conn.close()
def execute_sql(self,sql):
self.cursor.execute(sql)
for temp in self.cursor.fetchall():
print(temp)
def show_all_goods(self):
sql="select name from goods;"
self.execute_sql(sql)
def show_goods_cates(self):
sql="select name from goods_cataes;"
self.execute_sql(sql)
def show_goods_brand(self):
sql="select name from goods_brand;"
self.execute_sql(sql)
@staticmethod
def menu():
print("1:查询所有的商品")
print("2:查询所有的商品分类")
print("3:查询所有的商品品牌分类")
return input("请输入想要查询的信息:")
def run(self):
while True:
num=self.menu()
if num =="1":
# 查询所有的商品
self.show_all_goods()
elif num=="2":
self.show_goods_cates()
elif num=="3":
self.show_goods_brand()
else:
print("输入错误,请重新输入!!")
def main():
# 1.创建一个京东商城对象
# 2.调用这个对象的run方法,让其运行
jd=JD()
jd.run()
if __name__ == '__main__':
main()
增删改查:
__author__ = 'Administrator'
from pymysql import *
class JD():
def __init__(self):
# 创建Connection连接
self.conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
# 获得Cursor对象
self.cursor = self.conn.cursor()
def __del__(self):
# 关闭Cursor对象
self.cursor.close()
# 关闭Connection对象
self.conn.close()
def execute_sql(self,sql):
self.cursor.execute(sql)
for temp in self.cursor.fetchall():
print(temp)
def show_all_goods(self):
sql="select name from goods;"
self.execute_sql(sql)
def show_goods_cates(self):
sql="select name from goods_cataes;"
self.execute_sql(sql)
def show_goods_brand(self):
sql="select name from goods_brand;"
self.execute_sql(sql)
def add_brand(self):
brand_name=input("请输入新的商品分类:")
sql="""insert into goods_brand (name) values("%s")""" % brand_name
self.cursor.execute(sql)
# 如果是想要返回,使用回滚操作
#self.cursor.rollback()
self.conn.commit()
@staticmethod
def menu():
print("1:查询所有的商品")
print("2:查询所有的商品分类")
print("3:查询所有的商品品牌分类")
print("4:添加新的的商品品牌分类")
return input("请输入想要查询的信息:")
def run(self):
while True:
num=self.menu()
if num =="1":
# 查询所有的商品
self.show_all_goods()
elif num=="2":
self.show_goods_cates()
elif num=="3":
self.show_goods_brand()
elif num=="4":
self.add_brand()
else:
print("输入错误,请重新输入!!")
def main():
# 1.创建一个京东商城对象
# 2.调用这个对象的run方法,让其运行
jd=JD()
if __name__ == '__main__':
main()
3.防止SQL注入
sql语句的参数化,可以有小防止sql注入;
此处不同于python字符串的字符串格式化,全部使用%s占位。
防止sql注入:

__author__ = 'Administrator'
from pymysql import *
class JD():
def __init__(self):
# 创建Connection连接
self.conn = connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
# 获得Cursor对象
self.cursor = self.conn.cursor()
def __del__(self):
# 关闭Cursor对象
self.cursor.close()
# 关闭Connection对象
self.conn.close()
def execute_sql(self,sql):
self.cursor.execute(sql)
for temp in self.cursor.fetchall():
print(temp)
def show_all_goods(self):
sql="select name from goods;"
self.execute_sql(sql)
def show_goods_cates(self):
sql="select name from goods_cataes;"
self.execute_sql(sql)
def show_goods_brand(self):
sql="select name from goods_brand;"
self.execute_sql(sql)
def add_brand(self):
brand_name=input("请输入新的商品分类:")
sql="insert into goods_brand (name) values(%s);"
self.cursor.execute(sql,[brand_name])
# 如果是想要返回
#self.cursor.rollback()
self.conn.commit()
def get_info_by_name(self):
find_name=input("请输入要查询的商品的名字")
# sql="""select * from goods where name="%s"; """ %find_name
# self.cursor.execute_sql(sql)
sql="select * from goods where name=%s"
self.cursor.execute(sql,[find_name])
print(self.cursor.fetchall())
@staticmethod
def menu():
print("1:查询所有的商品")
print("2:查询所有的商品分类")
print("3:查询所有的商品品牌分类")
print("4:添加新的的商品品牌分类")
print("5:根据名字查询一个商品")
return input("请输入想要查询的信息:")
def run(self):
while True:
num=self.menu()
if num =="1":
# 查询所有的商品
self.show_all_goods()
elif num=="2":
self.show_goods_cates()
elif num=="3":
self.show_goods_brand()
elif num=="4":
self.add_brand()
elif num=="5":
self.get_info_by_name()
else:
print("输入错误,请重新输入!!")
def main():
# 1.创建一个京东商城对象
# 2.调用这个对象的run方法,让其运行
jd=JD()
if __name__ == '__main__':
main()


浙公网安备 33010602011771号