前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python操作mysql

python操作mysql

作者头像
py3study
发布2020-01-08 15:51:31
6180
发布2020-01-08 15:51:31
举报
文章被收录于专栏:python3

# rpm -qa |grep MySQL-python 查询是否有mysqldb库 MySQL-python-1.2.3-0.3.c1.1.el6.x86_64

>>> import MySQLdb #导入mysqldb模块 >>> conn = MySQLdb.connect(user='root',passwd='',host='127.0.0.1') #设置连接参数

>>> cur = conn.cursor() #创建游标

>>> conn.select_db('test') #选中数据库test进行连接

>>> cur.execute("insert into t1(id,name,age) value(3,'cc',30)") #发送sql指令,增加一条记录 1L  #显示增加一行记录

mysql> select * from t1; +------+------+------+ | id   | name | age  | +------+------+------+ |    1 | aa   | 10   | |    2 | bb   | 20   | |    3 | cc   | 30   |

>>> sqli = "insert into t1(id,name,age) value(%s,%s,%s)" #定义插入字符串

>>> cur.execute(sqli,(7,'ll',70)) #执行插入指令的,调插入字符串 1L

>>> cur.executemany(sqli,[(8,'rr',80),(9,'yy',90)]) #插入多行使用many 2L

mysql> select * from t1; +------+------+------+ | id   | name | age  | +------+------+------+ |    1 | aa   | 10   | |    2 | bb   | 20   | |    3 | cc   | 30   | |    4 | dd   | 40   | |    5 | gg   | 50   | |    6 | ff   | 60   | |    7 | ll   | 70   | |    8 | rr   | 80   | |    9 | yy   | 90   | +------+------+------+ 9 rows in set (0.00 sec)

>>> cur.execute('delete from t1 where id = 4') #删除操作 1L

mysql> select * from t1; +------+------+------+ | id   | name | age  | +------+------+------+ |    1 | aa   | 10   | |    2 | bb   | 20   | |    3 | cc   | 30   | |    5 | gg   | 50   | |    6 | ff   | 60   | |    7 | ll   | 70   | |    8 | rr   | 80   | |    9 | yy   | 90   | +------+------+------+ 8 rows in set (0.00 sec)

>>> cur.execute("update t1 set name = 'uu' where id = 7") #修改操作 1L

mysql> select * from t1; +------+------+------+ | id   | name | age  | +------+------+------+ |    1 | aa   | 10   | |    2 | bb   | 20   | |    3 | cc   | 30   | |    5 | gg   | 50   | |    6 | ff   | 60   | |    7 | uu   | 70   | |    8 | rr   | 80   | |    9 | yy   | 90   | +------+------+------+ 8 rows in set (0.00 sec)

>>> cur.execute('select * from t1') #查询,不能反映出来 8L

>>> cur.fetchone() #显示一行 (1L, 'aa', '10')

>>> cur.fetchmany(7) #显示七行 ((2L, 'bb', '20'), (3L, 'cc', '30'), (5L, 'gg', '50'), (6L, 'ff', '60'), (7L, 'uu', '70'), (8L, 'rr', '80'), (9L, 'yy', '90'))

>>> cur.fetchmany(7) #不可以重复取数据 ()

>>> cur.scroll(0,'absolute') #光标移动到开头位置

>>> cur.fetchmany(7) #可以继续去数据 ((1L, 'aa', '10'), (2L, 'bb', '20'), (3L, 'cc', '30'), (5L, 'gg', '50'), (6L, 'ff', '60'), (7L, 'uu', '70'), (8L, 'rr', '80'))

>>> cur.fetchmany(cur.execute("select * from t1")) #查询表中所有数据条目 ((1L, 'aa', '10'), (2L, 'bb', '20'), (3L, 'cc', '30'), (5L, 'gg', '50'), (6L, 'ff', '60'), (7L, 'uu', '70'), (8L, 'rr', '80'), (9L, 'yy', '90')) >>> cur.close() #关闭游标 >>> conn.close() #关闭数据库

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档