利用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
(('', '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数据库的更多相关文章
- Python访问MySQL数据库并实现其增删改查功能
概述:对于访问MySQL数据库的操作,我想大家也都有一些了解.不过,因为最近在学习Python,以下就用Python来实现它.其中包括创建数据库和数据表.插入记录.删除记录.修改记录数据.查询数据.删 ...
- 关于利用PHP访问MySql数据库的逻辑操作以及增删改查实例操作
PHP访问MySql数据库 <?php //造连接对象$db = new MySQLi("localhost","root","",& ...
- 利用Python操作MySQL数据库
前言 在工作中,我们需要经常对数据库进行操作,比如 Oracle.MySQL.SQL Sever 等,今天我们就学习如何利用Python来操作 MySQL 数据库. 本人环境:Python 3.7.0 ...
- 利用JavaFX访问MySQL数据库
1. 创建数据库表 create table Course( courseId char(5), subjectId char(4) not null, courseNumber integer, t ...
- Python访问MySQL数据库
#encoding: utf-8 import mysql.connector __author__ = 'Administrator' config={'host':'127.0.0.1',#默认1 ...
- 【python小记】访问mysql数据库
题记: 最近因为工作需要,学习了python,瞬间对这个轻松快捷的语给吸引了,以前只知道js脚本是写网页的,没有想到python这个脚本语言的应用范围可以这么广泛,现在做一些简单或稍微复杂的操作,基本 ...
- 使用python将mysql数据库的数据转换为json数据
由于产品运营部需要采用第三方个推平台,来推送消息.如果手动一个个键入字段和字段值,容易出错,且非常繁琐,需要将mysql的数据转换为json数据,直接复制即可. 本文将涉及到如何使用Python访问M ...
- 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数据 ...
- Python实现mysql数据库增删改查
利用python操作mysql数据库用法简单,环境配置容易,本文将实现对库增.删.改.查的简易封装! 1. 环境配置 安装第三方包 ,导入模块 mysql.connector pip inst ...
随机推荐
- UVA 12901 Refraction 折射 (物理)
一道物理题,解个2次方程就行了... 求h最小的情况对应如下图所示 做法不唯一,我想避免精度损失所以在化简的时候尽可能地去避免sqrt和浮点数乘除. 似乎精度要求很低,直接用角度算也可以 #inclu ...
- Android(java)学习笔记124:利用Service在后台播放背景音乐
1. 在android应用程序里,有一种没有UI的类(android.app.Service)——Service.简单来说,Service是一个 background process(背景程序),通过 ...
- python+opencv模拟生成运动模糊核
Mark:https://www.cnblogs.com/wyh1993/p/7118559.html 效果非常的好
- 爬虫3_python2
# coding=utf-8 import urllib params=urllib.urlencode({'t':1,'eggs':2,'bacon':0})#现在大多数网站都是动态网页,需要你动态 ...
- Understanding NFS Caching
Understanding NFS Caching Filesystem caching is a great tool for improving performance, but it is im ...
- Python——函数入门(二)
一.函数的参数 我们在定义函数时,可以定义形式参数(简称形参),这些形参的值在函数调用的时候才会确定,形参的值由调用者负责传入. 1.关键字参数 在Python中,函数的参数名并不是没有意义的,在调用 ...
- mount命令的用法,以及技巧光盘镜像文件、移动硬盘及U盘的方法
本文介绍mount命令的用法,以及技巧光盘镜像文件.移动硬盘及U盘的方法. 一,挂接命令(mount) 挂接(mount)命令的使用方法. 命令格式: 复制代码 代码如下: mount [-t vfs ...
- 记住密码功能 JS结合JQuery 操作 Cookie 实现记住密码和用户名!
// 记住密码功能 JS结合JQuery 操作 Cookie 实现记住密码和用户名! var username = document.getElementById("username&quo ...
- PHP redis使用命令
很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/owlient/phpredis(支持redis 2.0.4) Redis::__constru ...
- 【linux】【进程】stand alone 与 super daemon 区别
本文引用自 鸟哥的linux私房菜如果依据 daemon 的启动与管理方式来区分,基本上,可以将 daemon 分为可独立启动的 stand alone , 与透过一支 super daemon 来 ...