使用Connector / Python连接MySQL

connect()构造函数创建到MySQL服务器的连接并返回一个 MySQLConnection对象

在python中有以下几种方法可以连接到MySQL数据库:

  • 1.使用connect()构造函数
import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
host='127.0.0.1',
database='employees')
cnx.close()
  • 使用connection.MySQLConnection() 类创建连接对象
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
host='127.0.0.1',
database='employees')
cnx.close()
  • 在字典中定义连接参数并使用 **运算符
import mysql.connector

config = {
'user': 'scott',
'password': 'password',
'host': '127.0.0.1',
'database': 'employees',
'raise_on_warnings': True
} cnx = mysql.connector.connect(**config) cnx.close()

处理链接错误使用try语句并使用error.Error异常捕获所有错误

import mysql.connector
from mysql.connector import errorcode try:
cnx = mysql.connector.connect(user='scott',
database='employ')
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
cnx.close()

2.使用Connector / Python查询数据

import datetime
import mysql.connector cnx = mysql.connector.connect(user='scott', database='employees')
cursor = cnx.cursor() query = ("SELECT first_name, last_name, hire_date FROM employees "
"WHERE hire_date BETWEEN %s AND %s") hire_start = datetime.date(1999, 1, 1)
hire_end = datetime.date(1999, 12, 31) cursor.execute(query, (hire_start, hire_end)) for (first_name, last_name, hire_date) in cursor:
print("{}, {} was hired on {:%d %b %Y}".format(
last_name, first_name, hire_date)) cursor.close()
cnx.close()

参考链接:

https://dev.mysql.com/doc/connector-python/en/connector-python-examples.html

使用Connector / Python连接MySQL/查询数据的更多相关文章

  1. Python 2.7_初试连接Mysql查询数据导出到excel_20161216

    由于每天到公司都需要先执行一遍检测操作,观察数据是否导入完整,今天想到能否自动连接Mysql执行SQL并导出数据,每天到公司直接查看excel文件即可 时间紧,代码初次试验,边摸索边学习吧. xlsx ...

  2. 随笔记:Python于Windows下初实践,及使用Connector/Python连接MySQL

    有一同事要离职了,我负责交接一个用Python同步数据的项目. 之前木有做过Python,周休,做个简单的查询数据库,小练一下手. 包含: 安装 连接.查询MySQL 列表 元组 for循环 whil ...

  3. python连接 elasticsearch 查询数据,支持分页

    使用python连接es并执行最基本的查询 from elasticsearch import Elasticsearch es = Elasticsearch(["localhost:92 ...

  4. 【初学python】使用python连接mysql数据查询结果并显示

    因为测试工作经常需要与后台数据库进行数据比较和统计,所以采用python编写连接数据库脚本方便测试,提高工作效率,脚本如下(python连接mysql需要引入第三方库MySQLdb,百度下载安装) # ...

  5. Python 使用PyMySql 库 连接MySql数据库时 查询中文遇到的乱码问题(实测可行) python 连接 MySql 中文乱码 pymysql库

    最近所写的代码中需要用到python去连接MySql数据库,因为是用PyQt5来构建的GUI,原本打算使用PyQt5中的数据库连接方法,后来虽然能够正确连接上发现还是不能提交修改内容,最后在qq交流群 ...

  6. Python连接MySQL数据库的多种方式

    上篇文章分享了windows下载mysql5.7压缩包配置安装mysql 后续可以选择 ①在本地创建一个数据库,使用navicat工具导出远程测试服务器的数据库至本地,用于学习操作,且不影响测试服务器 ...

  7. python连接mysql的驱动

    对于py2.7的朋友,直接可以用MySQLdb去连接,但是MySQLdb不支持python3.x.这是需要注意的~ 那应该用什么python连接mysql的驱动呢,在stackoverflow上有人解 ...

  8. python 连接Mysql数据库

    1.下载http://dev.mysql.com/downloads/connector/python/ 由于Python安装的是3.4,所以需要下载下面的mysql-connector-python ...

  9. pymysql模块使用---Python连接MySQL数据库

    pymysql模块使用---Python连接MySQL数据库 浏览目录 pymysql介绍 连接数据库 execute( ) 之 sql 注入 增删改查操作 进阶用法 一.pymysql介绍 1.介绍 ...

随机推荐

  1. 吴裕雄--天生自然 PYTHON3开发学习:基础语法

    #!/usr/bin/python3 # 第一个注释 print ("Hello, Python!") # 第二个注释 #!/usr/bin/python3 # 第一个注释 # 第 ...

  2. 使用Eclipse开发学习 Spring Boot 教程的内容小结

    spring-tool-suite使用教程,并创建spring配置文件 Spring Boot基础教程1-Spring Tool Suite工具的安装 Spring Boot基础教程2-RESTful ...

  3. Maven高级:01.maven分模块构建&&02.私服的应用

    IntelliJ IDEA 2018.3.6 x64 07 Maven高级:01.maven分模块构建(上) 07 Maven高级:01.maven分模块构建(中) 07 Maven高级:01.mav ...

  4. PAT Advanced 1074 Reversing Linked List (25) [链表]

    题目 Given a constant K and a singly linked list L, you are supposed to reverse the links of every K e ...

  5. Maven--远程仓库的认证

    大部分远程仓库无须认证就可以访问,但有时候出于安全方面的考虑,我们需要提供认证信息才能访问一些远程仓库. 配置认证信息和配置仓库信息不同,仓库信息可以直接配置在 POM 文件中,但是认证信息必须配置在 ...

  6. Django学习---多人博客项目(1)

    一.创建项目和应用 ​ 在Pycharm中用Django模板创建一个工程文件 创建项目 python manage.py startproject 项目名 . 创建应用 python manage.p ...

  7. POJ-2349 Arctic Network(最小生成树+减免路径)

    http://poj.org/problem?id=2349 Description The Department of National Defence (DND) wishes to connec ...

  8. MySQL数据库高可用集群搭建-PXC集群部署

    Percona XtraDB Cluster(下文简称PXC集群)提供了MySQL高可用的一种实现方法.集群是有节点组成的,推荐配置至少3个节点,但是也可以运行在2个节点上. PXC原理描述: 分布式 ...

  9. 引力波的绘制(python)

    import numpy as np import matplotlib.pyplot as plt from scipy.io import wavfile rate_h,hstrain = wav ...

  10. 使用selenium模拟浏览器抓取淘宝信息

    通过Selenium模拟浏览器抓取淘宝商品美食信息,并存储到MongoDB数据库中. from selenium import webdriver from selenium.common.excep ...