Usage of Python MySQLdb
时间:2021-07-01 10:21:17
帮助过:3人阅读
# -*- coding: utf-8 -*-
2 from datetime
import datetime,timedelta
3 from string
import atoi,atof
4 import logging
5 import MySQLdb
6 import urllib
7 import urllib2
8 import json
9 import sys
10 import os
11
12
13 def mysql_start(conf_path):
14 global __conn,
__cursor
15 mysql_conf =
{}
16 execfile(
conf_path,mysql_conf)
17 rainbow = mysql_conf[
‘mysql‘]
18 __conn = MySQLdb.connect(host=rainbow[
‘host‘],port=rainbow[
‘port‘],user=rainbow[
‘user‘],19 passwd=rainbow[
‘passwd‘],db=rainbow[
‘db‘],charset=rainbow[
‘charset‘])
20 __cursor =
__conn.cursor()
21
22
23 def mysql_exec(*
cmd):
24 global __conn,
__cursor
25 if len(cmd)==1
:
26 __cursor.execute(cmd[0])
27 elif len(cmd)==2
:
28 __cursor.execute(cmd[0],cmd[1
])
29 else:
30 raise AssertionError()
31 __conn.commit()
32 return __cursor.fetchall()
33
34
35 def mysql_show(*
cmd):
36 try:
37 res = mysql_exec(*
cmd)
38 for item
in res:
39 print item
40 except Exception, e:
41 print "MySQL Execution Error: "+
str(e)
42
43
44 def mysql_close():
45 global __conn,
__cursor
46 __cursor.close()
47 __conn.close()
48
49
50 def http_querry(domain,port,path,argv):
51 # path should start with char
‘/‘
52 url =
‘http://‘+domain+
‘:‘+port+path+
‘?‘53 +
urllib.urlencode(argv)
54 try:
55 return urllib2.urlopen(url).read()
56 except urllib2.HTTPError,e:
57 print str(e.code)+
": "+url
58 59
常用 MySQL 命令可参考mr-wid的 21分钟MySQL入门教程 。
Usage of Python MySQLdb
标签: