首先要明确一点,我们在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
(('', 'Michael'), ('', 'ozil'), ('', 'Giroud'), ('', 'Henry'), ('Alexis', ''), ('Ramsey', ''), ('Walcott', ''))
>>> cur.execute('select * from user')
#fetchone只获得第一条查询结果
>>> user_select_result=cur.fetchone()
>>> user_select_result
('', 'Michael')
>>> cur.execute('select * from user')
#fetchmany(n),可以获得n条查询结果
>>> user_select_result=cur.fetchmany(4)
>>> user_select_result
(('', 'Michael'), ('', 'ozil'), ('', 'Giroud'), ('', 'Henry'))
#插入数据,注意插入语句的插入参数一定要是变量,不能是直接一个set
>>> insert_value=('','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
(('', 'Michael'), ('', 'ozil'), ('', 'Giroud'), ('', 'Henry'), ('', 'gibbs'), ('Alexis', ''), ('Ramsey', ''), ('Walcott', ''))
insert_value_list=[('','debucy'),('','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
(('', 'Michael'), ('', 'ozil'), ('', 'Giroud'), ('', 'Henry'), ('', 'debucy'), ('', 'gibbs'), ('', 'cech'), ('Alexis', ''), ('Ramsey', ''), ('Walcott', ''))
#只有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
(('', 'Michael'), ('', 'Ozil'), ('', 'Giroud'), ('', 'Henry'), ('', 'debucy'), ('', 'gibbs'), ('', 'cech'), ('Alexis', ''), ('Ramsey', ''), ('Walcott', ''))
#修改后一定要comiit,不然删除、更新、添加的数据都不会被写进数据库中。
>>> conn.commit()
#最后要把cur和conn都关掉
>>> cur.close()
>>> conn.close()

利用Python访问Mysql数据库的更多相关文章

  1. Python访问MySQL数据库并实现其增删改查功能

    概述:对于访问MySQL数据库的操作,我想大家也都有一些了解.不过,因为最近在学习Python,以下就用Python来实现它.其中包括创建数据库和数据表.插入记录.删除记录.修改记录数据.查询数据.删 ...

  2. 关于利用PHP访问MySql数据库的逻辑操作以及增删改查实例操作

    PHP访问MySql数据库 <?php //造连接对象$db = new MySQLi("localhost","root","",& ...

  3. 利用Python操作MySQL数据库

    前言 在工作中,我们需要经常对数据库进行操作,比如 Oracle.MySQL.SQL Sever 等,今天我们就学习如何利用Python来操作 MySQL 数据库. 本人环境:Python 3.7.0 ...

  4. 利用JavaFX访问MySQL数据库

    1. 创建数据库表 create table Course( courseId char(5), subjectId char(4) not null, courseNumber integer, t ...

  5. Python访问MySQL数据库

    #encoding: utf-8 import mysql.connector __author__ = 'Administrator' config={'host':'127.0.0.1',#默认1 ...

  6. 【python小记】访问mysql数据库

    题记: 最近因为工作需要,学习了python,瞬间对这个轻松快捷的语给吸引了,以前只知道js脚本是写网页的,没有想到python这个脚本语言的应用范围可以这么广泛,现在做一些简单或稍微复杂的操作,基本 ...

  7. 使用python将mysql数据库的数据转换为json数据

    由于产品运营部需要采用第三方个推平台,来推送消息.如果手动一个个键入字段和字段值,容易出错,且非常繁琐,需要将mysql的数据转换为json数据,直接复制即可. 本文将涉及到如何使用Python访问M ...

  8. Python访问MySQL(1):初步使用PyMySQL包

    Windows 10家庭中文版,MySQL 5.7.20 for Win 64,Python 3.6.4,PyMySQL 0.8.1,2018-05-08 ---- 使用Python访问MySQL数据 ...

  9. Python实现mysql数据库增删改查

    利用python操作mysql数据库用法简单,环境配置容易,本文将实现对库增.删.改.查的简易封装!   1. 环境配置 安装第三方包  ,导入模块 mysql.connector  pip inst ...

随机推荐

  1. CCCC 以及 hihocoder offer收割赛11 ~~~

    CCCC  真的很蒙  ,没有队服,没有狗牌,服务器崩溃到14:10  才开始比赛...(黑人问号 开始前,发现旁边是西交老大吴航,mad~各种紧张.看着大佬疯狂的敲宏定义就很怕啊.100多行,一行头 ...

  2. Ubuntu18.04如何从英文界面更改为中文界面

    本文介绍如何将Ubuntu18.04安装后的英文界面,更改为中文界面,即系统语言由英文改为简体中文.注意,与安装中文输入法不同,两者也没有冲突. 首先进入设置(Setting),选择区域和语言(Reg ...

  3. 通用的flash代码

    黑体字部分为常修改的部分 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=&quo ...

  4. 阻止form元素内的input标签回车提交表单

    <form></form>标签内input元素回车会默认提交表单. 阻止回车默认提交表单: $('form').on('keydown', function (event) { ...

  5. 自实现RPC调用

    服务提供者 服务接口: public interface HelloService { public String sayHello(String name); } 服务实现类: public cla ...

  6. Ubuntu 下安装WPS

    1.先到wps官网上下载wps的deb包. http://www.wps.cn/product/ 2.我使用的64位的,所以得安装32位兼容包 sudo apt-get install ia32-li ...

  7. beta版和alpha版

    外部测试版的意思. 软件会出现三种版本 1.alpha内部测试版本,极不稳定,一般也不会出现的公众视线,仅供内部测试人员测试用. 2.beta公共测试版,就是对外发布软件的测试版,收集公众的意见和建议 ...

  8. CF-1093 (2019/02/10)

    CF-1093 1093A - Dice Rolling 输出x/2即可 #include<bits/stdc++.h> using namespace std; int main() { ...

  9. 用Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围

    一.用默认设置绘制折线图 import matplotlib.pyplot as plt x_values=list(range(11)) #x轴的数字是0到10这11个整数 y_values=[x* ...

  10. graph-Kruskal-algorithm

    并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.主要操作:1. 初始化:每个点所在集合初始化为其自身.2. 查找:查找元素所在的集合,即根节点.3. ...