码迷,mamicode.com
首页 > 数据库 > 详细

Python3.0 操作MySQL数据库执行查询语句

时间:2019-06-11 19:42:55      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:__init__   data   use   连接   运行   cms   def   pytho   etc   

# coding: utf-8 
import pymysql

class MysqldbHelper(object):
def __init__(self, host="192.168.1.243", username="devlop", password="devlop", port=3306, database=‘zl_dcms‘, charset=‘utf8‘):
    self.host = host
    self.username = username
    self.password = password
    self.database = database
    self.port = port
   self.con = None
    self.cur = None
    self.charset = charset
    try:
      self.con = pymysql.connect(host=self.host, user=self.username, passwd=self.password, port=self.port, db=self.database)
      # 所有的查询,都在连接 con 的一个模块 cursor 上面运行的
      self.cur = self.con.cursor()
    except:
      print("DataBase connect error,please check the db config.")

def find(self, sql):
    sql = sql
    try:
      self.cur.execute(sql)
      results = self.cur.fetchall()
      for row in results:
        print(‘%s‘ % row)
        return(‘%s‘ % row)
    except pymysql.Error as e:
      error = ‘MySQL execute failed! ERROR (%s): %s‘ % (e.args[0], e.args[1]) print(error)

Python3.0 操作MySQL数据库执行查询语句

标签:__init__   data   use   连接   运行   cms   def   pytho   etc   

原文地址:https://www.cnblogs.com/xioawu-blog/p/11005321.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!