MySQL与Python交互

一、准备数据

1、创建数据表

-- 创建 "京东" 数据库createdatabasejdcharset=utf8;-- 使用 "京东" 数据库usejd;-- 创建一个商品goods数据表createtablegoods(idintunsignedprimarykeyauto_incrementnotnull,namevarchar(150)notnull,    cate_namevarchar(40)notnull,    brand_namevarchar(40)notnull,    pricedecimal(10,3)notnulldefault0,    is_showbitnotnulldefault1,    is_saleoffbitnotnulldefault0);

2、插入数据

-- 向goods表中插入数据insertintogoodsvalues(0,'r510vc 15.6英寸笔记本','笔记本','华硕','3399',default,default);insertintogoodsvalues(0,'y400n 14.0英寸笔记本电脑','笔记本','联想','4999',default,default);insertintogoodsvalues(0,'g150th 15.6英寸游戏本','游戏本','雷神','8499',default,default);insertintogoodsvalues(0,'x550cc 15.6英寸笔记本','笔记本','华硕','2799',default,default);insertintogoodsvalues(0,'x240 超极本','超级本','联想','4880',default,default);insertintogoodsvalues(0,'u330p 13.3英寸超极本','超级本','联想','4299',default,default);insertintogoodsvalues(0,'svp13226scb 触控超极本','超级本','索尼','7999',default,default);insertintogoodsvalues(0,'ipad mini 7.9英寸平板电脑','平板电脑','苹果','1998',default,default);insertintogoodsvalues(0,'ipad air 9.7英寸平板电脑','平板电脑','苹果','3388',default,default);insertintogoodsvalues(0,'ipad mini 配备 retina 显示屏','平板电脑','苹果','2788',default,default);insertintogoodsvalues(0,'ideacentre c340 20英寸一体电脑 ','台式机','联想','3499',default,default);insertintogoodsvalues(0,'vostro 3800-r1206 台式电脑','台式机','戴尔','2899',default,default);insertintogoodsvalues(0,'imac me086ch/a 21.5英寸一体电脑','台式机','苹果','9188',default,default);insertintogoodsvalues(0,'at7-7414lp 台式电脑 linux )','台式机','宏碁','3699',default,default);insertintogoodsvalues(0,'z220sff f4f06pa工作站','服务器/工作站','惠普','4288',default,default);insertintogoodsvalues(0,'poweredge ii服务器','服务器/工作站','戴尔','5388',default,default);insertintogoodsvalues(0,'mac pro专业级台式电脑','服务器/工作站','苹果','28888',default,default);insertintogoodsvalues(0,'hmz-t3w 头戴显示设备','笔记本配件','索尼','6999',default,default);insertintogoodsvalues(0,'商务双肩背包','笔记本配件','索尼','99',default,default);insertintogoodsvalues(0,'x3250 m4机架式服务器','服务器/工作站','ibm','6888',default,default);insertintogoodsvalues(0,'商务双肩背包','笔记本配件','索尼','99',default,default);


-- 查询类型cate_name为'超级本的商品

select name as 商品名称,priceas 商品价格from goodswhere cate_name ='超级本';

-- 显示商品的种类

select distinct cate_namefrom goods; -- 去重

select cate_namefrom goodsgroup by cate_name;-- 分组

-- 查询每个种类的商品名称

select cate_name,group_concat(name)from goodsgroup by cate_name;

-- 求所有电脑产品的平均价格,并且保留两位小数

select round(avg(price),2)from goods;

-- 显示每种商品的平均价格

select cate_name,avg(price)from goodsgroup by cate_name;

-- 查询每种类型的商品中最贵、最便宜、平均价、数量

select cate_name,max(price),min(price),avg(price),count(*)from goodsgroup by cate_name;

-- 查询所有价格大于平均价格的商品,并且按价格降序排序

select * from goodswhere price>(select avg(price)from goods)order by pricedesc;

-- 查询每种类型中最贵的电脑信息

select cate_name,max(price)from goodsgroup by cate_name;

select * from goods;

-- 相当于两句结合起来

select * from goods

inner join

    (

select

        cate_name,

        max(price)as max_price,

        min(price)as min_price,

        avg(price)as avg_price,

        count(*)from goodsgroup by cate_name

)as goods_new_info

on goods.cate_name=goods_new_info.cate_nameand goods.price=goods_new_info.max_price;

select * from (select cate_name,max(price)as max_pricefrom goodsgroup by cate_name)as g_new

left join goodsas g

on g_new.cate_name=g.cate_nameand g_new.max_price=g.priceorder by g_new.cate_name;

insert into goodsvalues(0,'东哥牌电脑','笔记本','老王','4999',default,default);

-- 创建商品分类表

create table if not exists goods_cates(

id int unsigned primary key auto_increment,

    name varchar(40)not null

);

-- 查询goods商品的种类

select cate_namefrom goodsgroup by cate_name;

insert into goods_cates (name)select cate_namefrom goodsgroup by cate_name;

update goodsas ginner join goods_catesas con g.cate_name=c.name set g.cate_name=c.id;

insert into goods_cates(name)values ('路由器'),('交换机'),('网卡');

insert into goods (name,cate_id,brand_id,price)

values('LaserJet Pro P1606dn 黑白激光打印机', 12, 4,'1849');

alter table goods

change cate_namecate_id int unsigned not null;

alter table goodsadd foreign key (cate_id)references goods_cates(id);-- 外键关联

delete from goodswhere id=23;

-- 创建商品品牌表

create table if not exists goods_brands(

id int unsigned primary key auto_increment,

    name varchar(40)not null

);

select brand_namefrom goodsgroup by brand_name;

insert into goods_brands (name)select brand_namefrom goodsgroup by brand_name;

update goodsas ginner join goods_brandsas bon g.brand_name=b.name set g.brand_name=b.id;

alter table goods

change brand_namebrand_id int unsigned not null;

alter table goodsadd foreign key (brand_id)references goods_brands(id);-- 外键关联

insert into goods (name,cate_id,brand_id,price)

values('LaserJet Pro P1606dn 黑白激光打印机', 2, 100,'1849');

-- 创建品牌表

create table goods_brands (

id int unsigned primary key auto_increment,

    name varchar(40)not null)select brand_nameas name from goodsgroup by brand_name;

update goodsas ginner join goods_brandsas bon g.brand_name=b.name set g.brand_name=b.id;

alter table goods

change brand_namebrand_id int unsigned not null;

alter table goodsadd foreign key (brand_id)references goods_brands(id);

-- 取消外键

alter table goodsdrop foreign key goods_ibfk_1;

alter table goodsdrop foreign key goods_ibfk_2;




# 在python3中使用 数据库

# python2  import MySQLdb

import pymysql

'''

'''

from pymysqlimport connect

# 创建connection连接

conn = connect(host='localhost',port=3306,database='wn',user='root',password='root',charset='utf8')

# 获取cursor对象

cursor = conn.cursor()

count = cursor.execute("select * from goods;")

print('查询到%d条数据'%count)

# print(cursor.fetchone())

# print(cursor.fetchone())

# print(cursor.fetchone())

# print(cursor.fetchone())

#

# print(cursor.fetchmany())# 取一条

# print(cursor.fetchmany(3))# 传几条取几条

#

# print(cursor.fetchall()) #取所有

line_content = cursor.fetchone()

print(line_content)

print(line_content[0])

print(line_content[1])

print(line_content[2])

for tempin line_content:

print(temp)

lines = cursor.fetchmany(5)

for tempin lines:

print(temp)

# 关闭cursor对象

cursor.close()

conn.close()

# print(cursor.fetchone(),'--------->')

# count = cursor.execute("select * from goods;")

# 数据库关闭之后不能再调用代码

你可能感兴趣的:(MySQL与Python交互)