window10系统下使用python版本实现mysql查询
参考文档:
兔大侠整理的MySQL-Python(MySQLdb)封装类
Python安装模块出错(ImportError: No module named setuptools)解决方法
环境 (windows10 && python3.3) || (linux &&python2.7)
#!/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()
#!/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查询的更多相关文章
- Linux系统下升级Python版本步骤(suse系统)
Linux系统下升级Python版本步骤(suse系统) http://blog.csdn.net/lifengling1234/article/details/53536493
- linux centos系统下升级python版本
本文参考资料:https://www.cnblogs.com/leon-zyl/p/8422699.html,https://blog.csdn.net/tpc1990519/article/deta ...
- window10系统下,彻底删除卸载mysql
本文介绍,在Windows10系统下,如何彻底删除卸载MySQL...1>停止MySQL服务开始->所有应用->Windows管理工具->服务,将MySQL服务停止.2> ...
- win10系统下多python版本部署
说明:win10,已安装有python3.5.2,为使用新浪云应用(SAE)支持微信公众号后台开发(SAE的python运行环境使用的是2.7.9),需部署python2.7的版本以便本地编辑调试. ...
- CentOS6 系统下升级python后yum命令使用时报错
CentOS6 系统下升级python后yum命令使用时报错,如下: [root@xxxxxxx]#yumFile"/usr/bin/yum",line30exceptKeyboa ...
- Linux系统下 解决Qt5无法连接MySQL数据库的方法
Linux平台下解决Qt5连接mysql数据库的问题:输入sudo apt-get install libqt5sql5-mysql解决,这种方法只能解决Qt是用sudo apt-get instal ...
- Linux下升级python版本
转载自:http://lovebeyond.iteye.com/blog/1770476 CentOS下的Python版本一般都比较低,很多应用都需要升级python来完成.我装的centOS的默认的 ...
- 腾讯云服务器ubuntu16.04系统下安装Python版本管理工具pyenv
一. 系统环境 腾讯云提供的系统是ubuntu 16.04 LTS,系统默认的Python版本是2.7.12,我想要安装3.6和其他的版本. 比较方便的是腾讯云已经默认安装好了git和curl ...
- CentOS 7下升级Python版本到3.x系列
由于python官方已宣布2.x系列即将停止支持,为了向前看,我们升级系统的python版本为3.x系列服务器系统为当前最新的CentOS 7.4 1.安装前查看当前系统下的python版本号 # p ...
随机推荐
- Delegate(QLabel和QComboBox)
一.最终效果 二.实现思路 1.createEditor()中create两个控件,分别是QLabel和QComboBox,将其添加到一个widget中,然后返回该widget: 2.setEdito ...
- c# 中base64字符串和图片的相互转换
c#base64字符串转图片用到了bitmap类,封装 GDI+ 位图,此位图由图形图像及其特性的像素数据组成. Bitmap 是用于处理由像素数据定义的图像的对象. 具体bitmap类是什么可以自己 ...
- ZOJ 2110 C - Tempter of the Bone
https://vjudge.net/contest/67836#problem/C The doggie found a bone in an ancient maze, which fascina ...
- Java 多线程 三种实现方式
Java多线程实现方式主要有三种:继承Thread类.实现Runnable接口.使用ExecutorService.Callable.Future实现有返回结果的多线程.其中前两种方式线程执行完后都没 ...
- [剑指Offer] 58.对称的二叉树
题目描述 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. [思路]递归,关键是isSame函数中的最后一句 /* struct Tree ...
- 第26天:js-$id函数、焦点事件
一.函数return语句定义函数的返回值,在函数内部用return来设置返回值,一个函数只能有一个返回值.同时,终止代码的执行.所有自定义函数默认没有返回值return后面不要换行 var a=10, ...
- 第23天:js-数据类型转换
一.padding1.内边距会影响盒子大小2.行内元素,尽量不用上下的padding和margin3.块元素嵌套块元素.子级会继承父级的宽度,高度由内容决定.如果给子级再设置padding,不会影响盒 ...
- 判断IP地址是否在指定范围内的方法
比如给定一个ip段:127.0.0.1 ~ 127.0.0.255,我们想判断一个给定的ip地址是否在此段内,可以先将ip地址转换成整数,然后整数比较大小就很容易了. 例如: 127.0.0.1 = ...
- Codeforces Round #521 Div. 3 玩耍记
A:签到. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...
- WCF服务的建立以及调用
WCF对我来说既陌生又熟悉,陌生是因为没怎么接触过,熟悉是听得太多,今天抽出点时间看了一下WCF,并且自己也写了一WCF的小程序以及调用WCF.步骤为: 1.创建一个解决方案WCF,和一个控制台项目W ...