使用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. 虚拟机virtualBox

    在笔记本了装了一个虚拟机, 并安装了Linux系统, 方便测试linux 命令. 考虑到不需要图形界面, 学习了用命令行操作虚拟机, 配置如下 linux 下安装openssh-server 虚拟机设 ...

  2. PHP实现快速排序算法相关案例

    <?php /** * 快速排序 --主要运用递归, 先把一个数找准位置,然后再递归把左右两边的数都找准位置 */ function QSort($a= []){ $nCount = count ...

  3. python编程:从入门到实践----第五章>if 语句

    一.一个简单示例 假设有一个汽车列表,并想将其每辆汽车的名称打印出来.遇到汽车名‘bmw’,以全大写打印:其他汽车名,首字母大写 cars=['audi','bmw','subaru','toyota ...

  4. C++数组常用操作

    1. 遍历数组 使用基于范围的for循环来遍历整个数组 用_countof()来得到数组中的元素个数 #include <iostream> #include <cstdio> ...

  5. PTA 自测-4 Have Fun with Numbers

    #include<iostream> #include<string> #include<cstring> #include<vector> using ...

  6. 是时候写个自己的dialog了

    组件下载地址:http://pan.baidu.com/s/1pJFVfej 最近做的项目需要用到对话框,但是原生的弹出框你是知道的.如果有时间,还是自己尝试一下,也是可以的. 一个简单图 里面的输入 ...

  7. MySql索引基本

    1.什么是索引 索引用于快速找出在某个列中有一特定值的行.如果不使用索引,需要遍历整张表,表越大查询耗时越大: MySQL中的索引的存储类型有两种:BTREE.HASH.具体实现机制参照另一篇博客: ...

  8. SVN解除与服务器的关联

    1.随便找个地方新建个文本文档 2.粘贴如下内容到文档里,建议使用notepad++ Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\ ...

  9. java内存区域与内存溢出异常(1)

    一. 运行时数据区域 java虚拟机在执行Java程序的过程中,会把它所管理的内存划分为若干个不同的数据区域偶 1.程序计数器 程序计数器是一块较小的内存空间,作用是当前线程所执行的字节码的行号指示器 ...

  10. 基于Flask框架搭建视频网站的学习日志(六)之数据库

    使用Flask-SQLSlchemy管理数据库(1)--初步安装调试 一.介绍: Flask-SQLSlchemy是一个Flask扩展,简化了Flask中对sql的操作,是一个高层的框架,可以避免直接 ...