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 ...
随机推荐
- axis2调用webService几种方式
主要有三种方式: 第一RPC方式,不生成客户端代码 第二,document方式,不生成客户端代码 第三,用wsdl2java工具,生成客户端方式调用 java代码: package samples.q ...
- Node js MongoDB简单操作
//win7环境下node要先安装MongoDB的相关组件(非安装MongoDB数据库),在cmd命令行进入node项目目录后执行以下语句 //npm install mongodb //创建连接 v ...
- ci tp重定向
server { listen 80; #listen [::]:80; server_name tpblog.yeves.com; index index.html index.htm index. ...
- mysql学习之数据备份与恢复
该文使用mysql5.5 centos6.5 64位(本人使用rpm安装mysql,数据库的安装目录默认) 一.数据备份注意事项 读锁问题:数据库(或者某个表)一旦进行读锁操作则影响数据库的写操作所以 ...
- [剑指Offer] 61.序列化二叉树
题目描述 请实现两个函数,分别用来序列化和反序列化二叉树 /* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *r ...
- 只要访问的地址匹配cookie的地址时候 发送request请求时候 会携带上该cookie
只要访问的地址匹配cookie的地址时候 发送request请求时候 会携带上该cookie
- Codeforces ECR52 div2翻车记
A:签到. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...
- Qt基本控件及三大布局
Qt基本控件及三大布局 来源: http://blog.csdn.net/a2604539133/article/details/73920696 Qt基本模块 一.Qt的三大布局 QHBoxLayo ...
- 【刷题】BZOJ 4827 [Hnoi2017]礼物
Description 我的室友最近喜欢上了一个可爱的小女生.马上就要到她的生日了,他决定买一对情侣手 环,一个留给自己,一个送给她.每个手环上各有 n 个装饰物,并且每个装饰物都有一定的亮度.但是在 ...
- 【以前的空间】bzoj1009 [HNOI2008]GT考试
动态规划+kmp+矩阵快速幂 关于这题可以写出一个dp方程(f[i,j]表示准考证前i位中后j位为不吉利的数字的前j位的情况的个数) f[i,j]=Σf[i-1,k],其中j表示不吉利数字前k个数字加 ...