参考文档:

兔大侠整理的MySQL-Python(MySQLdb)封装类

Python安装模块出错(ImportError: No module named setuptools)解决方法

环境 (windows10 && python3.3) || (linux &&python2.7)

1.确保已经安装setuptools
方法如下:>下载安装的脚本https://bootstrap.pypa.io/ez_setup.py,下载该脚本后运行
     >python ez_setup.py
     >即可。脚本会自动判断python的版本,自动下载,安装。
2.安装pip
方法如下:我们同样需要在Python的官网上去下载,
解压到某目录下,cd进去,使用命令 python setup.py install 进行安装
将X:\Python\Script 目录添加到path 
 
 3.安装mysqldb
pip install mysql-python
 
 代码是python2.7版本测试通过
测试文件:
DB.py
#!/usr/bin/env python
import MySQLdb
import time class ZDB: error_code = ''
_instance = None
_conn = None
_cur = None _TIMEOUT = 30
_timecount = 0 def __init__(self,dbconfig):
try:
self._conn = MySQLdb.connect(host=dbconfig['host'],
port=dbconfig['port'],
user=dbconfig['user'],
passwd=dbconfig['passwd'],
db=dbconfig['db'],
charset=dbconfig['charset'])
except MySQLdb.Error,e:
self.error_code = e.args[0]
error_msg = "MYSQL ERROR ! ",e.args[0].e.args[1]
print error_msg if self._timecount < self._TIMEOUT:
interval = 5
self._timecount += interval
time.sleep(interval)
return self.__init__(dbconfig)
else:
raise Exception(error_msg) self._cur = self._conn.cursor()
self._instance = MySQLdb def query(self,sql):
try:
self._cur.execute("SET NAMES UTF8")
result = self._cur.execute(sql)
except MySQLdb.error,e:
self.error_code = e.args[0]
print "MYSQL ERROR-Query:",e.args[0],e.args[1]
result=FALSE
return result def update(self,sql):
try:
self._cur.execute("SET NAMES UTF8")
result = self._cur.execute(sql)
self._conn.commit()
except MySQLdb.Error,e:
self.error_code = e.args[0]
print "MYSQL ERROR-Update:",e.args[0],e.args[1]
result=FALSE
return result
def insert(self,sql):
try:
self._cur.execute("SET NAMES UTF8")
self._cur.execute(sql)
self._conn.commit()
return self._conn.insert_id()
except MySQLdb.Error,e:
self.error_code = e.args[0]
print "MYSQL ERROR-Insert:",e.args[0],e.args[1]
result=FALSE
def fetchAllRows(self):
return self._cur.fetchall()
def getRowCount(self):
return self._cur.rowcount()
def commit(self):
self._conn.commit()
def rollback(self):
self._conn.rollback()
def __del__(self):
try:
self._cur.close()
self._conn.close()
except:
pass
def close(self):
self.__del__()

使用测试:

use.py

#!/usr/bin/env python
from DB import ZDB
def main():
dbconfig={'host':' ',
'port':3306,
'user':' ',
'passwd':' ',
'db':'test',
'charset':'UTF8'}
db=ZDB(dbconfig) sql = "SELECT * FROM `user`"
db.query(sql)
result = db.fetchAllRows()
print "This is the result>",result
for row in result:
for colum in row:
print colum
db.close()
main()
使用命令 python use.py 进行调用
 
 
_____________________________________________________________
 
也可以使用pymysql查询 代码是python3.3版本测试通过
简单demo如下所示:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pymysql
conn=pymysql.connect(host='localhost',
port=3306,
user='root',
passwd='root',
db='test',
charset='utf8')
cur = conn.cursor()
sql = "SELECT * FROM chart_pie"
cur.execute(sql)
for r in cur.fetchall():
for column in r:
print(r) conn.close()

window10系统下使用python版本实现mysql查询的更多相关文章

  1. Linux系统下升级Python版本步骤(suse系统)

    Linux系统下升级Python版本步骤(suse系统) http://blog.csdn.net/lifengling1234/article/details/53536493

  2. linux centos系统下升级python版本

    本文参考资料:https://www.cnblogs.com/leon-zyl/p/8422699.html,https://blog.csdn.net/tpc1990519/article/deta ...

  3. window10系统下,彻底删除卸载mysql

    本文介绍,在Windows10系统下,如何彻底删除卸载MySQL...1>停止MySQL服务开始->所有应用->Windows管理工具->服务,将MySQL服务停止.2> ...

  4. win10系统下多python版本部署

    说明:win10,已安装有python3.5.2,为使用新浪云应用(SAE)支持微信公众号后台开发(SAE的python运行环境使用的是2.7.9),需部署python2.7的版本以便本地编辑调试. ...

  5. CentOS6 系统下升级python后yum命令使用时报错

    CentOS6 系统下升级python后yum命令使用时报错,如下: [root@xxxxxxx]#yumFile"/usr/bin/yum",line30exceptKeyboa ...

  6. Linux系统下 解决Qt5无法连接MySQL数据库的方法

    Linux平台下解决Qt5连接mysql数据库的问题:输入sudo apt-get install libqt5sql5-mysql解决,这种方法只能解决Qt是用sudo apt-get instal ...

  7. Linux下升级python版本

    转载自:http://lovebeyond.iteye.com/blog/1770476 CentOS下的Python版本一般都比较低,很多应用都需要升级python来完成.我装的centOS的默认的 ...

  8. 腾讯云服务器ubuntu16.04系统下安装Python版本管理工具pyenv

    一. 系统环境   腾讯云提供的系统是ubuntu 16.04 LTS,系统默认的Python版本是2.7.12,我想要安装3.6和其他的版本.   比较方便的是腾讯云已经默认安装好了git和curl ...

  9. CentOS 7下升级Python版本到3.x系列

    由于python官方已宣布2.x系列即将停止支持,为了向前看,我们升级系统的python版本为3.x系列服务器系统为当前最新的CentOS 7.4 1.安装前查看当前系统下的python版本号 # p ...

随机推荐

  1. github 使用“git commit -m"命令时候出现的一个小问题

    git commit -m 使用问题 今天提交文件到github,步骤是: git add abc.py (abc.py是我当前随意写的一个文件名) git commit -m 'add codes ...

  2. 【RL系列】Multi-Armed Bandit笔记补充(一)

    在此之前,请先阅读上一篇文章:[RL系列]Multi-Armed Bandit笔记 本篇的主题就如标题所示,只是上一篇文章的补充,主要关注两道来自于Reinforcement Learning: An ...

  3. 四、oracle 用户管理二

    一.使用profile管理用户口令概述:profile是口令限制,资源限制的命令集合,当建立数据库时,oracle会自动建立名称为default的profile.当建立用户没有指定profile选项时 ...

  4. Java IO(文件操作工具类)

    FileOperate实现的功能: 1. 返回文件夹中所有文件列表 2. 读取文本文件内容 3. 新建目录 4. 新建多级目录 5. 新建文件 6. 有编码方式的创建文件 7. 删除文件 8. 删除指 ...

  5. PK3Err0040

    PK3Err0040 The target device is not ready for debugging. Please check your configuration bit setting ...

  6. 浮点数(floating-point number)二进制存储格式

    定义 浮点数就是小数点位置不固定的数,也就是说与定点数不一样,浮点数的小数点后的小数位数可以是任意的,根据IEEE754-1985(也叫IEEE Standard for Binary Floatin ...

  7. Hadoop 版本 生态圈 MapReduce模型

    忘的差不多了, 先补概念, 然后开始搭建集群实战 ... . 一 Hadoop版本 和 生态圈 1. Hadoop版本 (1) Apache Hadoop版本介绍 Apache的开源项目开发流程 : ...

  8. 内部网关协议RIP 路由选择算法(距离向量)

    RIP是一种基于距离向量的路由选择协议 RIP的距离就是指的跳数,没经过一个路由,就是一跳,RIP允许一跳路径最多经过15个路由器,所以16个的话就相当于不可以到达了 RIP协议的特点: 1:仅和相邻 ...

  9. ACM 第五天

    匈牙利算法(二分图匹配) C - Courses Consider a group of N students and P courses. Each student visits zero, one ...

  10. C#创建Window服务图解,安装、配置、以及C#操作Windows服务

    一.首先打开VS2013,创建Windows服务项目 二.创建完成后对"Service1.cs"重命名位"ServiceDemo":然后切换到代码视图,写个服务 ...