Python3.0 操作MySQL数据库执行查询语句
Posted xioawu-blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python3.0 操作MySQL数据库执行查询语句相关的知识,希望对你有一定的参考价值。
# 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数据库执行查询语句的主要内容,如果未能解决你的问题,请参考以下文章