python 调用mysql_Python中直接在MySQL执行SQL命令
因?yàn)閼?#xff0c;所以不想用SQLyog,用Python寫了一個(gè)快速執(zhí)行SQL命令的程序:import pymysql,sys
class dealMySQL(object):
‘PyMysql‘
version = ‘Version:0.1‘
author = ‘Author: August‘
def __init__(self):
pass
@classmethod
def execute(self,sql):
‘execute SQL command!‘
try:
conn = pymysql.Connect(host=‘localhost‘,user=‘root‘,passwd=‘‘,db=‘sql_‘,port=3306,charset=‘utf8‘)
cur = conn.cursor()
except pymysql.err.InternalError as err:
print("Can not connect MySQL:%r"%err)
sys.exit()
except pymysql.err.OperationalError as err:
print("Can not connect MySQL:%r"%err)
sys.exit()
try:
cur.execute(sql)
conn.commit()
for x in cur:
print(x)
print("success!")
except:
print("Error! Please check your SQL!")
cur.close()
conn.close()
原文:http://520ccdd.blog.51cto.com/3926314/1708105
總結(jié)
以上是生活随笔為你收集整理的python 调用mysql_Python中直接在MySQL执行SQL命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LOL8.9版本卡莎中单AP打法符文技能
- 下一篇: 用-force –opengl 指令_O