- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章MySQL和Python交互的示例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
目录 。
。
。
。
创建数据表 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
-- 创建 "京东" 数据库
create
database
jing_dong charset=utf8;
-- 使用 "京东" 数据库
use jing_dong;
-- 创建一个商品goods数据表
create
table
goods(
id
int
unsigned
primary
key
auto_increment
not
null
,
name
varchar
(150)
not
null
,
cate_name
varchar
(40)
not
null
,
brand_name
varchar
(40)
not
null
,
price
decimal
(10,3)
not
null
default
0,
is_show
bit
not
null
default
1,
is_saleoff
bit
not
null
default
0
);
|
。
插入数据 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
-- 向goods表中插入数据
insert
into
goods
values
(0,
'r510vc 15.6英寸笔记本'
,
'笔记本'
,
'华硕'
,
'3399'
,
default
,
default
);
insert
into
goods
values
(0,
'y400n 14.0英寸笔记本电脑'
,
'笔记本'
,
'联想'
,
'4999'
,
default
,
default
);
insert
into
goods
values
(0,
'g150th 15.6英寸游戏本'
,
'游戏本'
,
'雷神'
,
'8499'
,
default
,
default
);
insert
into
goods
values
(0,
'x550cc 15.6英寸笔记本'
,
'笔记本'
,
'华硕'
,
'2799'
,
default
,
default
);
insert
into
goods
values
(0,
'x240 超极本'
,
'超级本'
,
'联想'
,
'4880'
,
default
,
default
);
insert
into
goods
values
(0,
'u330p 13.3英寸超极本'
,
'超级本'
,
'联想'
,
'4299'
,
default
,
default
);
insert
into
goods
values
(0,
'svp13226scb 触控超极本'
,
'超级本'
,
'索尼'
,
'7999'
,
default
,
default
);
insert
into
goods
values
(0,
'ipad mini 7.9英寸平板电脑'
,
'平板电脑'
,
'苹果'
,
'1998'
,
default
,
default
);
insert
into
goods
values
(0,
'ipad air 9.7英寸平板电脑'
,
'平板电脑'
,
'苹果'
,
'3388'
,
default
,
default
);
insert
into
goods
values
(0,
'ipad mini 配备 retina 显示屏'
,
'平板电脑'
,
'苹果'
,
'2788'
,
default
,
default
);
insert
into
goods
values
(0,
'ideacentre c340 20英寸一体电脑 '
,
'台式机'
,
'联想'
,
'3499'
,
default
,
default
);
insert
into
goods
values
(0,
'vostro 3800-r1206 台式电脑'
,
'台式机'
,
'戴尔'
,
'2899'
,
default
,
default
);
insert
into
goods
values
(0,
'imac me086ch/a 21.5英寸一体电脑'
,
'台式机'
,
'苹果'
,
'9188'
,
default
,
default
);
insert
into
goods
values
(0,
'at7-7414lp 台式电脑 linux )'
,
'台式机'
,
'宏碁'
,
'3699'
,
default
,
default
);
insert
into
goods
values
(0,
'z220sff f4f06pa工作站'
,
'服务器/工作站'
,
'惠普'
,
'4288'
,
default
,
default
);
insert
into
goods
values
(0,
'poweredge ii服务器'
,
'服务器/工作站'
,
'戴尔'
,
'5388'
,
default
,
default
);
insert
into
goods
values
(0,
'mac pro专业级台式电脑'
,
'服务器/工作站'
,
'苹果'
,
'28888'
,
default
,
default
);
insert
into
goods
values
(0,
'hmz-t3w 头戴显示设备'
,
'笔记本配件'
,
'索尼'
,
'6999'
,
default
,
default
);
insert
into
goods
values
(0,
'商务双肩背包'
,
'笔记本配件'
,
'索尼'
,
'99'
,
default
,
default
);
insert
into
goods
values
(0,
'x3250 m4机架式服务器'
,
'服务器/工作站'
,
'ibm'
,
'6888'
,
default
,
default
);
insert
into
goods
values
(0,
'商务双肩背包'
,
'笔记本配件'
,
'索尼'
,
'99'
,
default
,
default
);
|
。
。
。
1. SQL语句的强化 。
查询类型cate_name为 '超极本' 的商品名称、价格 。
1
|
select
name
,price
from
goods
where
cate_name =
'超级本'
;
|
显示商品的种类 。
1
|
select
cate_name
from
goods
group
by
cate_name;
|
求所有电脑产品的平均价格,并且保留两位小数 。
1
|
select
round(
avg
(price),2)
as
avg_price
from
goods;
|
显示每种商品的平均价格 。
1
|
select
cate_name,
avg
(price)
from
goods
group
by
cate_name;
|
查询每种类型的商品中 最贵、最便宜、平均价、数量 。
1
|
select
cate_name,
max
(price),
min
(price),
avg
(price),
count
(*)
from
goods
group
by
cate_name;
|
查询所有价格大于平均价格的商品,并且按价格降序排序 。
1
2
3
|
select
id,
name
,price
from
goods
where
price > (
select
round(
avg
(price),2)
as
avg_price
from
goods)
order
by
price
desc
;
|
查询每种类型中最贵的电脑信息 。
1
2
3
4
5
6
7
8
9
10
11
|
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
goods
group
by
cate_name
)
as
goods_new_info
on
goods.cate_name=goods_new_info.cate_name
and
goods.price=goods_new_info.max_price;
|
。
2. 创建 "商品分类"" 表 。
1
2
3
4
5
|
-- 创建商品分类表
create
table
if
not
exists goods_cates(
id
int
unsigned
primary
key
auto_increment,
name
varchar
(40)
not
null
);
|
查询goods表中商品的种类 。
1
|
select
cate_name
from
goods
group
by
cate_name;
|
将分组结果写入到goods_cates数据表 。
1
|
insert
into
goods_cates (
name
)
select
cate_name
from
goods
group
by
cate_name;
|
。
3. 同步表数据 。
通过goods_cates数据表来更新goods表 。
1
|
update
goods
as
g
inner
join
goods_cates
as
c
on
g.cate_name=c.
name
set
g.cate_name=c.id;
|
。
4. 创建 "商品品牌表" 表 。
通过create...select来创建数据表并且同时写入记录,一步到位 。
1
2
3
4
5
6
7
|
-- select brand_name from goods group by brand_name;
-- 在创建数据表的时候一起插入数据
-- 注意: 需要对brand_name 用as起别名,否则name字段就没有值
create
table
goods_brands (
id
int
unsigned
primary
key
auto_increment,
name
varchar
(40)
not
null
)
select
brand_name
as
name
from
goods
group
by
brand_name;
|
。
5. 同步数据 。
通过goods_brands数据表来更新goods数据表 。
1
|
update
goods
as
g
inner
join
goods_brands
as
b
on
g.brand_name=b.
name
set
g.brand_name=b.id;
|
。
6. 修改表结构 。
查看 goods 的数据表结构,会发现 cate_name 和 brand_name对应的类型为 varchar 但是存储的都是数字 。
1
|
desc
goods;
|
通过alter table语句修改表结构 。
1
2
3
|
alter
table
goods
change cate_name cate_id
int
unsigned
not
null
,
change brand_name brand_id
int
unsigned
not
null
;
|
。
7. 外键 。
分别在 goods_cates 和 goods_brands表中插入记录 。
1
2
|
insert
into
goods_cates(
name
)
values
(
'路由器'
),(
'交换机'
),(
'网卡'
);
insert
into
goods_brands(
name
)
values
(
'海尔'
),(
'清华同方'
),(
'神舟'
);
|
在 goods 数据表中写入任意记录 。
1
2
|
insert
into
goods (
name
,cate_id,brand_id,price)
values
(
'LaserJet Pro P1606dn 黑白激光打印机'
, 12, 4,
'1849'
);
|
查询所有商品的详细信息 (通过内连接) 。
1
2
3
|
select
g.id,g.
name
,c.
name
,b.
name
,g.price
from
goods
as
g
inner
join
goods_cates
as
c
on
g.cate_id=c.id
inner
join
goods_brands
as
b
on
g.brand_id=b.id;
|
查询所有商品的详细信息 (通过左连接) 。
1
2
3
|
select
g.id,g.
name
,c.
name
,b.
name
,g.price
from
goods
as
g
left
join
goods_cates
as
c
on
g.cate_id=c.id
left
join
goods_brands
as
b
on
g.brand_id=b.id;
|
1
2
3
4
5
6
|
-- 给brand_id 添加外键约束成功
alter
table
goods
add
foreign
key
(brand_id)
references
goods_brands(id);
-- 给cate_id 添加外键失败
-- 会出现1452错误
-- 错误原因:已经添加了一个不存在的cate_id值12,因此需要先删除
alter
table
goods
add
foreign
key
(cate_id)
references
goods_cates(id);
|
1
2
3
4
5
6
7
8
9
10
11
|
create
table
goods(
id
int
primary
key
auto_increment
not
null
,
name
varchar
(40)
default
''
,
price
decimal
(5,2),
cate_id
int
unsigned,
brand_id
int
unsigned,
is_show
bit
default
1,
is_saleoff
bit
default
0,
foreign
key
(cate_id)
references
goods_cates(id),
foreign
key
(brand_id)
references
goods_brands(id)
);
|
如何取消外键约束 。
1
2
3
4
|
-- 需要先获取外键约束名称,该名称系统会自动生成,可以通过查看表创建语句来获取名称
show
create
table
goods;
-- 获取名称之后就可以根据名称来删除外键约束
alter
table
goods
drop
foreign
key
外键名称;
|
在实际开发中,很少会使用到外键约束,会极大的降低表更新的效率 。
。
。
。
创建 "商品分类" 表(之前已经创建,无需再次创建) 。
1
2
3
4
|
create
table
goods_cates(
id
int
unsigned
primary
key
auto_increment
not
null
,
name
varchar
(40)
not
null
);
|
。
创建 "商品品牌" 表(之前已经创建,无需再次创建) 。
1
2
3
4
|
create
table
goods_brands (
id
int
unsigned
primary
key
auto_increment
not
null
,
name
varchar
(40)
not
null
);
|
。
创建 "商品" 表(之前已经创建,无需再次创建) 。
1
2
3
4
5
6
7
8
9
10
11
|
create
table
goods(
id
int
unsigned
primary
key
auto_increment
not
null
,
name
varchar
(40)
default
''
,
price
decimal
(5,2),
cate_id
int
unsigned,
brand_id
int
unsigned,
is_show
bit
default
1,
is_saleoff
bit
default
0,
foreign
key
(cate_id)
references
goods_cates(id),
foreign
key
(brand_id)
references
goods_brands(id)
);
|
。
创建 "顾客" 表 。
1
2
3
4
5
6
|
create
table
customer(
id
int
unsigned auto_increment
primary
key
not
null
,
name
varchar
(30)
not
null
,
addr
varchar
(100),
tel
varchar
(11)
not
null
);
|
。
创建 "订单" 表 。
1
2
3
4
5
6
|
create
table
orders(
id
int
unsigned auto_increment
primary
key
not
null
,
order_date_time datetime
not
null
,
customer_id
int
unsigned,
foreign
key
(customer_id)
references
customer(id)
);
|
。
创建 "订单详情" 表 。
1
2
3
4
5
6
7
8
|
create
table
order_detail(
id
int
unsigned auto_increment
primary
key
not
null
,
order_id
int
unsigned
not
null
,
goods_id
int
unsigned
not
null
,
quantity tinyint unsigned
not
null
,
foreign
key
(order_id)
references
orders(id),
foreign
key
(goods_id)
references
goods(id)
);
|
。
说明 。
。
。
。
引入模块 。
在py文件中引入pymysql模块 。
1
|
from
pymysql
import
*
|
。
Connection 对象 。
1
|
conn
=
connect(参数列表)
|
对象的方法 。
。
Cursor对象 。
1
|
cs1
=
conn.cursor()
|
对象的方法 。
对象的属性 。
。
。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
from
pymysql import *
def main():
# 创建
Connection
连接
conn =
connect
(host=
'localhost'
,port=3306,
database
=
'jing_dong'
,
user
=
'root'
,
password
=
'mysql'
,charset=
'utf8'
)
# 获得
Cursor
对象
cs1 = conn.
cursor
()
# 执行
insert
语句,并返回受影响的行数:添加一条数据
# 增加
count
= cs1.
execute
(
'insert into goods_cates(name) values("硬盘")'
)
#打印受影响的行数
print(
count
)
count
= cs1.
execute
(
'insert into goods_cates(name) values("光盘")'
)
print(
count
)
# # 更新
#
count
= cs1.
execute
(
'update goods_cates set name="机械硬盘" where name="硬盘"'
)
# # 删除
#
count
= cs1.
execute
(
'delete from goods_cates where id=6'
)
# 提交之前的操作,如果之前已经之执行过多次的
execute
,那么就都进行提交
conn.
commit
()
# 关闭
Cursor
对象
cs1.
close
()
# 关闭
Connection
对象
conn.
close
()
if __name__ ==
'__main__'
:
main()
|
。
查询一行数据 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
from
pymysql
import
*
def
main():
# 创建Connection连接
conn
=
connect(host
=
'localhost'
,port
=
3306
,user
=
'root'
,password
=
'mysql'
,database
=
'jing_dong'
,charset
=
'utf8'
)
# 获得Cursor对象
cs1
=
conn.cursor()
# 执行select语句,并返回受影响的行数:查询一条数据
count
=
cs1.execute(
'select id,name from goods where id>=4'
)
# 打印受影响的行数
print
(
"查询到%d条数据:"
%
count)
for
i
in
range
(count):
# 获取查询的结果
result
=
cs1.fetchone()
# 打印查询的结果
print
(result)
# 获取查询的结果
# 关闭Cursor对象
cs1.close()
conn.close()
if
__name__
=
=
'__main__'
:
main()
|
。
查询多行数据 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
from
pymysql
import
*
def
main():
# 创建Connection连接
conn
=
connect(host
=
'localhost'
,port
=
3306
,user
=
'root'
,password
=
'mysql'
,database
=
'jing_dong'
,charset
=
'utf8'
)
# 获得Cursor对象
cs1
=
conn.cursor()
# 执行select语句,并返回受影响的行数:查询一条数据
count
=
cs1.execute(
'select id,name from goods where id>=4'
)
# 打印受影响的行数
print
(
"查询到%d条数据:"
%
count)
# for i in range(count):
# # 获取查询的结果
# result = cs1.fetchone()
# # 打印查询的结果
# print(result)
# # 获取查询的结果
result
=
cs1.fetchall()
print
(result)
# 关闭Cursor对象
cs1.close()
conn.close()
if
__name__
=
=
'__main__'
:
main()
|
。
。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
from
pymysql
import
*
def
main():
find_name
=
input
(
"请输入物品名称:"
)
# 创建Connection连接
conn
=
connect(host
=
'localhost'
,port
=
3306
,user
=
'root'
,password
=
'mysql'
,database
=
'jing_dong'
,charset
=
'utf8'
)
# 获得Cursor对象
cs1
=
conn.cursor()
# # 非安全的方式
# # 输入 " or 1=1 or " (双引号也要输入)
# sql = 'select * from goods where name="%s"' % find_name
# print("""sql===>%s<====""" % sql)
# # 执行select语句,并返回受影响的行数:查询所有数据
# count = cs1.execute(sql)
# 安全的方式
# 构造参数列表
params
=
[find_name]
# 执行select语句,并返回受影响的行数:查询所有数据
count
=
cs1.execute(
'select * from goods where name=%s'
, params)
# 注意:
# 如果要是有多个参数,需要进行参数化
# 那么params = [数值1, 数值2....],此时sql语句中有多个%s即可
# 打印受影响的行数
print
(count)
# 获取查询的结果
# result = cs1.fetchone()
result
=
cs1.fetchall()
# 打印查询的结果
print
(result)
# 关闭Cursor对象
cs1.close()
# 关闭Connection对象
conn.close()
if
__name__
=
=
'__main__'
:
main()
|
以上就是MySQL和Python交互的示例的详细内容,更多关于MySQL和python交互的资料请关注我其它相关文章! 。
原文链接:https://www.cnblogs.com/Gaowaly/p/14490359.html 。
最后此篇关于MySQL和Python交互的示例的文章就讲到这里了,如果你想了解更多关于MySQL和Python交互的示例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!