python mysql 分号_Python与Mysql交互

本文介绍了如何使用Python的pymysql模块连接和操作MySQL数据库,包括建立连接、创建游标、执行SQL语句和关闭连接。通过示例展示了查询emp表所有数据的过程,解析了查询结果,并强调了在Python中执行SQL语句无需加分号。
摘要由CSDN通过智能技术生成

其实python可以做很多事情,例如可以连接我们熟悉的数据库——Mysql。

下面就由我为大家介绍,如何用python连接Mysql数据库

python连接mysql的基本原理:

导入模块:import pymysql

建立连接:pymysql.connect(**登录对象)

创建游标:connection.curxor()

执行SQL语句:cursor.execute(SQL语句)

获取结果

PS:切记在最后要关闭游标和连接

连接时要注意的地方:

在python中执行SQL语句不用加分号(;)

execute执行完后并不是直接的结果,需主动获取

与执行文件一样,要关闭游标与连接

注意食物的回滚与提交(rollback、commit)

实战:

为大家演示的是python连接mysql数据库

并执行:“select * from emp”

1. 下面是在linux系统中执行的SQL语句,并附带结果:

在linux系统中查看emp表的内容

mysql> select * from emp;

+-------+--------+-----------+------+------------+---------+---------+--------+

| empno | ename | job | mgr | hiredate | sal | comm | deptno |

+-------+--------+-----------+------+------------+---------+---------+--------+

| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |

| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-02 | 1600.00 | 300.00 | 30 |

| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |

| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |

| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |

| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |

| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |

| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |

| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |

| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |

| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |

| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |

+-------+--------+-----------+------+------------+---------+---------+--------+

12 rows in set (0.01 sec)

2. 下面是在python中执行SQL语句的方法演示:

#导入pymysql模块

import pymysql

#创建登录细信息对象:db_login

db_login = {

'user':'root', #用户名

'password':'Passw0rd', #数据库密码

'db':'DB', #数据库名

'charset':'utf8' #注意不是utf-8

}

#创建连接mysql对象:link

link = pymysql.connect(**db_login)

#创建游标对象:cursor(利用游标来执行SQL语句)

cursor = link.cursor()

try:

#执行SQL语句,不反悔结果,返回其影响的行数

executes = cursor.execute("select * from emp")

#循环打印结果

for value in cursor.fetchall():

print(value)

#提交到数据库,真正把数据插入或者更新到数据中

link.commit()

except Exception as error:

print(error)

#发生异常、回滚

link.rollback()

finally:

#关闭游标

cursor.close()

#关闭连接

link.close()

3. python执行SQL语句的结果

(Decimal('7369'), 'SMITH', 'CLERK', Decimal('7902'), datetime.date(1980, 12, 17), Decimal('800.00'), None, Decimal('20'))

(Decimal('7499'), 'ALLEN', 'SALESMAN', Decimal('7698'), datetime.date(1981, 2, 2), Decimal('1600.00'), Decimal('300.00'), Decimal('30'))

(Decimal('7521'), 'WARD', 'SALESMAN', Decimal('7698'), datetime.date(1981, 2, 22), Decimal('1250.00'), Decimal('500.00'), Decimal('30'))

(Decimal('7566'), 'JONES', 'MANAGER', Decimal('7839'), datetime.date(1981, 4, 2), Decimal('2975.00'), None, Decimal('20'))

(Decimal('7654'), 'MARTIN', 'SALESMAN', Decimal('7698'), datetime.date(1981, 9, 28), Decimal('1250.00'), Decimal('1400.00'), Decimal('30'))

(Decimal('7698'), 'BLAKE', 'MANAGER', Decimal('7839'), datetime.date(1981, 5, 1), Decimal('2850.00'), None, Decimal('30'))

(Decimal('7782'), 'CLARK', 'MANAGER', Decimal('7839'), datetime.date(1981, 6, 9), Decimal('2450.00'), None, Decimal('10'))

(Decimal('7839'), 'KING', 'PRESIDENT', None, datetime.date(1981, 11, 17), Decimal('5000.00'), None, Decimal('10'))

(Decimal('7844'), 'TURNER', 'SALESMAN', Decimal('7698'), datetime.date(1981, 9, 8), Decimal('1500.00'), Decimal('0.00'), Decimal('30'))

(Decimal('7900'), 'JAMES', 'CLERK', Decimal('7698'), datetime.date(1981, 12, 3), Decimal('950.00'), None, Decimal('30'))

(Decimal('7902'), 'FORD', 'ANALYST', Decimal('7566'), datetime.date(1981, 12, 3), Decimal('3000.00'), None, Decimal('20'))

(Decimal('7934'), 'MILLER', 'CLERK', Decimal('7782'), datetime.date(1982, 1, 23), Decimal('1300.00'), None, Decimal('10'))

Process finished with exit code 0

欢迎技术交流

WeChat......

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值