window10系统下使用python3.3版本实现mysql查询
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了window10系统下使用python3.3版本实现mysql查询,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3131字,纯文字阅读大概需要5分钟。
内容图文
Python安装模块出错(ImportError: No module named setuptools)解决方法
环境 windows10 | python3.3
1.确保已经安装setuptools 方法如下:>下载安装的脚本https://bootstrap.pypa.io/ez_setup.py,下载该脚本后运行 >python ez_setup.py >即可。脚本会自动判断python的版本,自动下载,安装。 2.安装pip 方法如下:我们同样需要在Python的官网上去下载, 下载地址是:https://pypi.python.org/pypi/pip#downloads : 解压到某目录下,cd进去,使用命令 python setup.py install 进行安装 将X:\Python\Script 目录添加到path 3.安装mysqldb pip install mysql-python 测试文件: 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查询 简单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系统下使用python3.3版本实现mysql查询
标签:
本文系统来源:http://www.cnblogs.com/ddddemo/p/5671557.html
内容总结
以上是互联网集市为您收集整理的window10系统下使用python3.3版本实现mysql查询全部内容,希望文章能够帮你解决window10系统下使用python3.3版本实现mysql查询所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。