-
Notifications
You must be signed in to change notification settings - Fork 40
/
MysqlUtility.py
63 lines (53 loc) · 1.65 KB
/
MysqlUtility.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
#-*-coding:utf-8-*-
import MySQLdb
import sys
import ConfigParser
G_GROUP = 'mysql'
G_CONFIG = './config.ini'
def getConfig(file, group, configName):
config = ConfigParser.ConfigParser()
config.readfp(open(file, "rw"))
configValue = config.get(group, configName.strip(' ').strip('\'').strip("\""))
return configValue
host = getConfig(G_CONFIG,G_GROUP,'host')
port = getConfig(G_CONFIG,G_GROUP,'port')
user = getConfig(G_CONFIG,G_GROUP,'user')
passwd = getConfig(G_CONFIG,G_GROUP,'passwd')
dbname = getConfig(G_CONFIG,G_GROUP,'dbname')
def mysqlExec(sql, param):
conn = MySQLdb.connect(host=host,user=user,passwd=passwd,port=int(port),connect_timeout=5,charset='utf8')
conn.select_db(dbname)
cursor = conn.cursor()
if param <> '':
cursor.execute(sql,param)
else:
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()
def mysqlQuery(sql):
conn = MySQLdb.connect(host=host,user=user,passwd=passwd,port=int(port),connect_timeout=5,charset='utf8')
conn.select_db(dbname)
cursor = conn.cursor()
count = cursor.execute(sql)
if count == 0 :
result = 0
else:
result = cursor.fetchall()
cursor.close()
conn.close()
return result
def getOption(key):
conn = MySQLdb.connect(host=host,user=user,passwd=passwd,port=int(port),connect_timeout=5,charset='utf8')
conn.select_db(dbname)
cursor = conn.cursor()
sql="select value from options where name=+'"+key+"'"
count = cursor.execute(sql)
if count == 0 :
result=0
else:
result=cursor.fetchone()
cursor.close()
conn.close()
return result[0]