Python 操作 MySQL 的5种方式(转)

Python 操作 MySQL 的5种方式

不管你是做数据分析,还是网络爬虫,Web 开发、亦或是机器学习,你都离不开要和数据库打交道,而 MySQL 又是最流行的一种数据库,这篇文章介绍 Python 操作 MySQL 的5种方式,你可以在实际开发过程中根据实际情况合理选择。

 

1、MySQLdb

MySQLdb又叫MySQL-python ,是 Python 连接 MySQL 最流行的一个驱动,很多框架都也是基于此库进行开发,遗憾的是它只支持 Python2.x,而且安装的时候有很多前置条件,因为它是基于C开发的库,在 Windows 平台安装非常不友好,经常出现失败的情况,现在基本不推荐使用,取代的是它的衍生版本。

# 前置条件
sudo apt-get install python-dev libmysqlclient-dev # Ubuntu
sudo yum install python-devel mysql-devel # Red Hat / CentOS
# 安装
pip install MySQL-python
Windows 直接通过下载 exe 文件安装
#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect(host="localhost",    # 主机名user="root",         # 用户名passwd="pythontab.com",  # 密码db="testdb")        # 数据库名称
# 查询前,必须先获取游标
cur = db.cursor()
# 执行的都是原生SQL语句
cur.execute("SELECT * FROM mytable")
for row in cur.fetchall():print(row[0])
db.close()

 


2、mysqlclient

由于 MySQL-python(MySQLdb) 年久失修,后来出现了它的 Fork 版本 mysqlclient,完全兼容 MySQLdb,同时支持 Python3.x,是 Django ORM的依赖工具,如果你想使用原生 SQL 来操作数据库,那么推荐此驱动。安装方式和 MySQLdb 是一样的,Windows 可以在 https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient 网站找到 对应版本的 whl 包下载安装。

 

pip install mysqlclient

 

3、PyMySQL

PyMySQL 是纯 Python 实现的驱动,速度上比不上 MySQLdb,最大的特点可能就是它的安装方式没那么繁琐,同时也兼容 MySQL-python

pip install PyMySQL
# 为了兼容mysqldb,只需要加入
pymysql.install_as_MySQLdb()

 

例子:

import pymysql
conn = pymysql.connect(host='127.0.0.1', user='root', passwd="pythontab.com", db='testdb')
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
for r in cur:print(r)
cur.close()
conn.close()

 

4、peewee

写原生 SQL 的过程非常繁琐,代码重复,没有面向对象思维,继而诞生了很多封装 wrapper 包和 ORM 框架,ORM 是 Python 对象与数据库关系表的一种映射关系,有了 ORM 你不再需要写 SQL 语句。提高了写代码的速度,同时兼容多种数据库系统,如sqlite, mysql、postgresql,付出的代价可能就是性能上的一些损失。如果你对 Django 自带的 ORM 熟悉的话,那么 peewee的学习成本几乎为零。它是 Python 中是最流行的 ORM 框架。

 

安装

pip install peewee

 

 

例子:

import peewee
from peewee import *
db = MySQLDatabase('testdb', user='root', passwd='pythontab.com')
class Book(peewee.Model):author = peewee.CharField()title = peewee.TextField()class Meta:database = db
Book.create_table()
book = Book(author="pythontab", title='pythontab is good website')
book.save()
for book in Book.filter(author="pythontab"):print(book.title)

 

官方文档:http://docs.peewee-orm.com/en/latest/peewee/installation.html

 

5、SQLAlchemy

如果想找一种既支持原生 SQL,又支持 ORM 的工具,那么 SQLAlchemy 是最好的选择,它非常接近 Java 中的 Hibernate 框架。

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy_declarative import Address, Base, Person
class Address(Base):__tablename__ = 'address'id = Column(Integer, primary_key=True)street_name = Column(String(250))
engine = create_engine('sqlite:///sqlalchemy_example.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
# Insert a Person in the person table
new_person = Person(name='new person')
session.add(new_person)
session.commit()

 

 

转载于:https://www.cnblogs.com/yhleng/p/9798907.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388212.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

unity3d 人员控制代码

普通浏览复制代码private var walkSpeed : float 1.0;private var gravity 100.0;private var moveDirection : Vector3 Vector3.zero;private var charController : CharacterController;function Start(){charController GetComponent(CharacterController);animation.w…

删除wallet里面登机牌_登机牌丢失问题

删除wallet里面登机牌On a sold-out flight, 100 people line up to board the plane. The first passenger in the line has lost his boarding pass but was allowed in regardless. He takes a random seat. Each subsequent passenger takes their assigned seat if availa…

PHP 备份还原 MySql 数据库

原生 PHP 备份还原 MySql 数据库 支持 MySql,PDO 两种方式备份还原 php5.5 以上的版本建议开启pdo扩展,使用 pdo 备份还原数据 备份文件夹 db_backup、import/log 文件要有读写权限环境版本 本人测试环境 php:5.5.38 /5.6.27-nts/7.0.12-nts; mysql: 5.5…

Java™ 教程(Queue接口)

Queue接口 Queue是在处理之前保存元素的集合&#xff0c;除了基本的Collection操作外&#xff0c;队列还提供额外的插入、删除和检查操作&#xff0c;Queue接口如下。 public interface Queue<E> extends Collection<E> {E element();boolean offer(E e);E peek();…

字符串操作截取后面的字符串_对字符串的5个必知的熊猫操作

字符串操作截取后面的字符串We have to represent every bit of data in numerical values to be processed and analyzed by machine learning and deep learning models. However, strings do not usually come in a nice and clean format and require preprocessing to con…

最新 Unity3D鼠标滑轮控制物体放大缩小 [

var s 1.0;function Update () {var cube GameObject.Find("Cube");if(Input.GetAxis("Mouse ScrollWheel")){s Input.GetAxis("Mouse ScrollWheel");cube.transform.localScaleVector3(1*s,1*s,1*s);}}

sublime-text3 安装 emmet 插件

下载sublime&#xff0c;http://www.sublimetext.com/ 安装package control &#xff1a;https://packagecontrol.io/ins... 这个地址需要翻墙&#xff0c;访问不了的可以看下图 import urllib.request,os,hashlib; h 6f4c264a24d933ce70df5dedcf1dcaee ebe013ee18cced0ef93d…

数据科学家访谈录 百度网盘_您应该在数据科学访谈中向THEM提问。

数据科学家访谈录 百度网盘A quick search on Medium with the keywords “Data Science Interview” resulted in hundreds of Medium articles to help guide the reader from what concepts are covered to even specific company interviews ranging from Tesla, Walmart, …

unity3d]鼠标点击地面人物自动走动(也包含按键wasdspace控制)

目录(?)[-] 一效果图二大概步骤 创建一个plane设置层为Terrain因为后面要判断是否点击的是这个层准备好人物模型并且将三个脚本拖放到人物上并且将动画文件也拖放好记得看前面提醒哦 ThirdPersonCamera相当于smoothflowThirdPersonController修改版mouseMoveContr鼠标点击人物…

uva 524(Prime Ring Problem UVA - 524 )

dfs练习题,我素数打表的时候ji了&#xff0c;一直没发现实际上是ji*i&#xff0c;以后可记住了。还有最后一行不能有空格。。。昏迷了半天 我的代码(紫书上的算法) #include <bits/stdc.h> using namespace std; int bk[110]; int num[110]; int vis[110]; int n; void d…

Web 开发基础

一、 Web 开发简介 最早的软件都是运行在大型机上的&#xff0c;软件使用者登陆到大型机上去运行软件。后来随着 PC 机的兴起&#xff0c;软件开始主要运行在桌面上&#xff0c;而数据库这样的软件运行在服务器端&#xff0c;这种 Client/Server 模式简称 CS 架构。随着互联网的…

power bi函数_在Power BI中的行上使用聚合函数

power bi函数Aggregate functions are one of the main building blocks in Power BI. Being used explicitly in measures, or implicitly defined by Power BI, there is no single Power BI report which doesn’t use some sort of aggregate functions.聚合功能是Power BI…

关于如何在Python中使用静态、类或抽象方法的权威指南

Python中方法的工作方式 方法是存储在类属性中的函数&#xff0c;你可以用下面这种方式声明和访问一个函数 >>> class Pizza(object):... def __init__(self, size):... self.size size... def get_size(self):... return self.size...>&…

广义估计方程估计方法_广义估计方程简介

广义估计方程估计方法A key assumption underpinning generalized linear models (which linear regression is a type of) is the independence of observations. In longitudinal data this will simply not hold. Observations within an individual (between time points) …

css二

结构性伪类:nth-child(index)系列1.:first-child2.:last-child3.nth-last-child(index)4.only-child :nth-of-type(index)系列1.first-of-type2.last-of-type3.nth-last-type(index)4.only-of-type :not伪类处理导航栏最后一个竖划线a:not(:last-of-type) :empty伪类 选中所有内…

Unity3d鼠标点击屏幕来控制人物的走动

今天呢&#xff0c;我们来一起实现一个在RPG中游戏中十分常见的功能&#xff0c;通过鼠标点击屏幕来控制人物的走动。首先来说一下原理&#xff0c;当我们点击屏幕时&#xff0c;我们按照一定的方法&#xff0c;将屏幕上的二维坐标转化为三维坐标&#xff0c;然后我们从摄像机位…

Java中的ReentrantLock和synchronized两种锁定机制的对比

2019独角兽企业重金招聘Python工程师标准>>> 多线程和并发性并不是什么新内容&#xff0c;但是 Java 语言设计中的创新之一就是&#xff0c;它是第一个直接把跨平台线程模型和正规的内存模型集成到语言中的主流语言。核心类库包含一个 Thread 类&#xff0c;可以用它…

10.15 lzxkj

几天前写的&#xff0c;忘了放了&#xff0c;在此填坑 10月16的题我出的不写题解了 lzxkj 题目背景 众所不周知的是&#xff0c; 酒店之王 xkj 一个经常迷失自我的人 有一天&#xff0c; 当起床铃再一次打响的时候&#xff0c; TA 用 O(1)的时间在 TA 那早就已经生锈的大脑中自…

大数定理 中心极限定理_中心极限定理:直观的遍历

大数定理 中心极限定理One of the most beautiful concepts in statistics and probability is Central Limit Theorem,people often face difficulties in getting a clear understanding of this and the related concepts, I myself struggled understanding this during my…

万恶之源 - Python数据类型二

列表 列表的介绍 列表是python的基础数据类型之一 ,其他编程语言也有类似的数据类型. 比如JS中的数 组, java中的数组等等. 它是以[ ]括起来, 每个元素用 , 隔开而且可以存放各种数据类型: lst [1,a,True,[2,3,4]]列表相比于字符串,不仅可以存放不同的数据类型.而且可…