MySQL與Python交互

一、準備數(shù)據(jù)

1、創(chuàng)建數(shù)據(jù)表

-- 創(chuàng)建 "京東" 數(shù)據(jù)庫createdatabasejdcharset=utf8;-- 使用 "京東" 數(shù)據(jù)庫usejd;-- 創(chuàng)建一個商品goods數(shù)據(jù)表createtablegoods(idintunsignedprimarykeyauto_incrementnotnull,namevarchar(150)notnull,? ? cate_namevarchar(40)notnull,? ? brand_namevarchar(40)notnull,? ? pricedecimal(10,3)notnulldefault0,? ? is_showbitnotnulldefault1,? ? is_saleoffbitnotnulldefault0);

2、插入數(shù)據(jù)

-- 向goods表中插入數(shù)據(jù)insertintogoodsvalues(0,'r510vc 15.6英寸筆記本','筆記本','華碩','3399',default,default);insertintogoodsvalues(0,'y400n 14.0英寸筆記本電腦','筆記本','聯(lián)想','4999',default,default);insertintogoodsvalues(0,'g150th 15.6英寸游戲本','游戲本','雷神','8499',default,default);insertintogoodsvalues(0,'x550cc 15.6英寸筆記本','筆記本','華碩','2799',default,default);insertintogoodsvalues(0,'x240 超極本','超級本','聯(lián)想','4880',default,default);insertintogoodsvalues(0,'u330p 13.3英寸超極本','超級本','聯(lián)想','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英寸一體電腦 ','臺式機','聯(lián)想','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工作站','服務(wù)器/工作站','惠普','4288',default,default);insertintogoodsvalues(0,'poweredge ii服務(wù)器','服務(wù)器/工作站','戴爾','5388',default,default);insertintogoodsvalues(0,'mac pro專業(yè)級臺式電腦','服務(wù)器/工作站','蘋果','28888',default,default);insertintogoodsvalues(0,'hmz-t3w 頭戴顯示設(shè)備','筆記本配件','索尼','6999',default,default);insertintogoodsvalues(0,'商務(wù)雙肩背包','筆記本配件','索尼','99',default,default);insertintogoodsvalues(0,'x3250 m4機架式服務(wù)器','服務(wù)器/工作站','ibm','6888',default,default);insertintogoodsvalues(0,'商務(wù)雙肩背包','筆記本配件','索尼','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;

-- 求所有電腦產(chǎn)品的平均價格,并且保留兩位小數(shù)

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

-- 顯示每種商品的平均價格

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

-- 查詢每種類型的商品中最貴、最便宜、平均價、數(shù)量

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;

-- 相當于兩句結(jié)合起來

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);

-- 創(chuàng)建商品分類表

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 ('路由器'),('交換機'),('網(wǎng)卡');

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);-- 外鍵關(guān)聯(lián)

delete from goodswhere id=23;

-- 創(chuàng)建商品品牌表

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);-- 外鍵關(guān)聯(lián)

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

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

-- 創(chuàng)建品牌表

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中使用 數(shù)據(jù)庫

# python2? import MySQLdb

import pymysql

'''

'''

from pymysqlimport connect

# 創(chuàng)建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條數(shù)據(jù)'%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)

# 關(guān)閉cursor對象

cursor.close()

conn.close()

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

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

# 數(shù)據(jù)庫關(guān)閉之后不能再調(diào)用代碼

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、準備數(shù)據(jù) 1、創(chuàng)建數(shù)據(jù)表 -- 創(chuàng)建 "京東" 數(shù)據(jù)庫createdatabasejdcharset=utf8...
    清清不快樂閱讀 203評論 0 1
  • 一、準備數(shù)據(jù) 1、創(chuàng)建數(shù)據(jù)表 -- 創(chuàng)建 "京東" 數(shù)據(jù)庫createdatabasejdcharset=utf8...
    快樂托兒索閱讀 207評論 0 0
  • 1 準備數(shù)據(jù) 1>創(chuàng)建數(shù)據(jù)表 創(chuàng)建絲芙蘭數(shù)據(jù)庫 create database SEPHORE charset=u...
    晚冬至雪閱讀 273評論 0 0
  • 1 準備數(shù)據(jù) 1>創(chuàng)建數(shù)據(jù)表 創(chuàng)建絲芙蘭數(shù)據(jù)庫 create database SEPHORE charset=u...
    wyc111閱讀 194評論 0 0
  • 1 準備數(shù)據(jù) 1>創(chuàng)建數(shù)據(jù)表 創(chuàng)建絲芙蘭數(shù)據(jù)庫 create database SEPHORE charset=u...
    噬魂__1727閱讀 180評論 0 0

友情鏈接更多精彩內(nèi)容