利用Python访问Mysql数据库
首先要明确一点,我们在Python中需要通过第三方库才能访问Mysql。
有这样几种方式:Mysql-python(即MySQLdb)、pymysql、mysql-connector。Mysql-python是用C写的,速度最快,而后两者是用纯python写的,相对来说速度慢一点。可惜的是Mysql-python只支持到python 2.7,对python 3.X都没有支持。如果你用的是python3,就用不了Mysql-python了。本文章使用的是python 3.4,用的第三方库是pymysql。
Pymysql的安装
安装好pip之后。在CMD中输入命令pip install pymysql即可安装。有关于pip的安装和使用请自行百度。
安装好pymysql后,其使用跟一般的第三方库一样,需要事先import,下面我直接给一个例子,大家可以从IDLE中实验。
>>> import pymysql >>> conn=pymysql.connect(host='localhost',user='root',passwd='password',charset='utf8',port=3306) #port一般都是3306,charset要写utf8,不然可能会出现乱码 >>> cur=conn.cursor() #查看有哪些数据库 >>> cur.execute('show databases') >>> databases=[] >>> for i in cur:databases.append(i) >>> databases [('information_schema',), ('firstdb',), ('hive',), ('jeesite',), ('mysql',), ('school',), ('test',), ('test1',), ('test2015',)] #选择数据库 >>> conn.select_db('test') #如果一开始就知道选什么数据库,可以把数据库参数加到connect的语句里: #conn=pymysql.connect(host='localhost',user='root',passwd='password',db='test',charset='utf8',port=3306) #查看有哪些表 >>> cur.execute('show tables') #fetchall是获得所有的查询结果 >>> tables_list=cur.fetchall() >>> tables_list (('user',), ('user2',), ('user3',), ('user4',), ('user5',), ('user6',), ('user7',)) #创建table >>> cur.execute('create table user8(id varchar(10),name varchar(10))') #如果习惯于每一个colmn单独一行,可以用'''代替' >>> cur.execute('''create table user8(id varchar(10), name varchar(10))''') #查看表user,execute中的语句语法跟mysql中的一样 >>> cur.execute('select * from user') >>> user_select_result=cur.fetchall() >>> user_select_result (('1', 'Michael'), ('11', 'ozil'), ('12', 'Giroud'), ('2', 'Henry'), ('Alexis', '17'), ('Ramsey', '16'), ('Walcott', '14')) >>> cur.execute('select * from user') #fetchone只获得第一条查询结果 >>> user_select_result=cur.fetchone() >>> user_select_result ('1', 'Michael') >>> cur.execute('select * from user') #fetchmany(n),可以获得n条查询结果 >>> user_select_result=cur.fetchmany(4) >>> user_select_result (('1', 'Michael'), ('11', 'ozil'), ('12', 'Giroud'), ('2', 'Henry')) #插入数据,注意插入语句的插入参数一定要是变量,不能是直接一个set >>> insert_value=('3','gibbs') >>> cur.execute('insert into user(id,name) values(%s,%s)',insert_value) >>> cur.execute('select * from user') >>> user_select_result=cur.fetchall() >>> user_select_result (('1', 'Michael'), ('11', 'ozil'), ('12', 'Giroud'), ('2', 'Henry'), ('3', 'gibbs'), ('Alexis', '17'), ('Ramsey', '16'), ('Walcott', '14')) insert_value_list=[('22','debucy'),('33','cech')] #插入多条数据,需要用executemany >>> cur.executemany('insert into user(id,name) values(%s,%s)',insert_value_list) >>> cur.execute('select * from user') >>> user_select_result=cur.fetchall() >>> user_select_result (('1', 'Michael'), ('11', 'ozil'), ('12', 'Giroud'), ('2', 'Henry'), ('22', 'debucy'), ('3', 'gibbs'), ('33', 'cech'), ('Alexis', '17'), ('Ramsey', '16'), ('Walcott', '14')) #只有conn.commit()后,对数据库的修改才会提交 >>> conn.commit() >>> cur.execute('update user set name="Ozil" where id="11"') >>> user_select_result=cur.fetchall() >>> user_select_result () >>> cur.execute('select * from user') >>> user_select_result=cur.fetchall() >>> user_select_result (('1', 'Michael'), ('11', 'Ozil'), ('12', 'Giroud'), ('2', 'Henry'), ('22', 'debucy'), ('3', 'gibbs'), ('33', 'cech'), ('Alexis', '17'), ('Ramsey', '16'), ('Walcott', '14')) #修改后一定要comiit,不然删除、更新、添加的数据都不会被写进数据库中。 >>> conn.commit() #最后要把cur和conn都关掉 >>> cur.close() >>> conn.close()
转载于:https://www.cnblogs.com/ArsenalfanInECNU/p/4799261.html
利用Python访问Mysql数据库相关推荐
- 操作 mysql 不生成日志_利用Python操作MySQL数据库,以后不懂这些是要被鄙视的!...
前言 在工作中,我们需要经常对数据库进行操作,比如 Oracle.MySQL.SQL Sever 等,今天我们就学习如何利用Python来操作 MySQL 数据库. 本人环境:Python 3.7.0 ...
- python如何收集数据库_利用Python操作mysql数据库
本文主要讲解如何利用python中的pymysql库来对mysql数据库进行操作 大家在转行项目中也可以加入这一步操作,提高逼格的同时还能简化流程 正文开始: 先看一下最常见的操作:从数据库中sele ...
- 利用python爆破mysql数据库
0x00 这几天学习了使用python去爆破mysql数据库.脚本转载连接: python 构造mysql爆破器 - 东京$ - 博客园 脚本具体内容如下: import pymysql import ...
- Python访问MySQL数据库并实现其增删改查功能
概述: 对于访问数据库的操作,我想大家也都有一些了解.不过,因为最近在学习Python,以下就用Python来实现它.其中包括创建数据库和数据表.插入记录.删除记录.修改记录数据.查询数据.删除数据表 ...
- python入侵数据库数据库_一个简单的Python访问Mysql数据库例子
2020/11/3操作记录 搭建好Python的数据环境之后,接下来就是在Python代码中访问数据库我先在Navicat图形化界面创建一个数据库命名为pythontest,再在数据库中创建了一个表s ...
- python和sqlserver_利用python实现mysql数据库向sqlserver的同步
话不多说,直接上代码. #!/usr/bin/python # -*- coding:utf8 -*- # author: chenzhixin """ 一.安装环境: ...
- 《跟着小吴哥学python》之 13 Python访问mysql数据库
1.安装mysql-python驱动模块 1.pip install mysql-python 2.如果你使用的默认安装的python也可以使用 yum 安装 MySQL-python yum ins ...
- pymssql mysql_Python利用pymssql访问mysql数据库
#coding=utf8 #!/usr/bin/env python #---------------------------------------------------------------- ...
- pymssql连mysql_Python利用pymssql访问mysql数据库
#coding=utf8 #!/usr/bin/env python #---------------------------------------------------------------- ...
最新文章
- Test class should have exactly one public constructor解决办法
- Linux下更改系统时区及时间
- span居div中_JavaScript-html Dom中的HTMLCollection和NodeList
- Cascader 级联选择器
- 余数运算符在Java中用于Doubles
- 个人作业2——英语学习APP的案例分析
- React中的CSS——styled-components
- echart折线图删除_用Echart创建简单的折线图
- 使用高速通道加速iOS版本审核
- 别人加薪你加班,征服老板才是王道
- 传输控制协议端口服务多路开关选择器
- 软件编码测试要点总结
- 大一java实训报告1500字_java实训报告两篇
- 在线FLV播放器实现方法
- Bad Request This combination of host and port requires TLS. postman
- 计算机软件考试成绩查询时间,2020年计算机软考什么时候出成绩,怎么查成绩?|...
- 数字IC笔试题,大疆校招16题(仅供参考)
- MySQL及同类产品的技术调研报告
- 上证50基金有哪些_【宽基系列】上证50指数基金买哪个?
- Hadoop是什么?(处理大数据存储和分析的基础架构)