42121 发表于 2015-11-2 11:00:01

Python中直接在MySQL执行SQL命令

因为懒,所以不想用SQLyog,用Python写了一个快速执行SQL命令的程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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()



页: [1]
查看完整版本: Python中直接在MySQL执行SQL命令