forked from awolfly9/IPProxyTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlhelper.py
87 lines (73 loc) · 2.75 KB
/
sqlhelper.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#-*- coding: utf-8 -*-
import logging
import utils
import config
import pymysql
class SqlHelper(object):
def __init__(self):
self.conn = pymysql.connect(**config.database_config)
self.cursor = self.conn.cursor()
try:
self.conn.select_db(config.database)
except:
self.create_database()
self.conn.select_db(config.database)
def create_database(self):
try:
command = 'CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARACTER SET \'utf8\' ' % config.database
utils.log('sql helper create_database command:%s' % command)
self.cursor.execute(command)
self.conn.commit()
except Exception, e:
utils.log('SqlHelper create_database exception:%s' % str(e), logging.WARNING)
def create_table(self, command):
try:
utils.log('sql helper create_table command:%s' % command)
x = self.cursor.execute(command)
self.conn.commit()
return x
except Exception, e:
utils.log('sql helper create_table exception:%s' % str(e), logging.WARNING)
def insert_data(self, command, data, commit = False):
try:
utils.log('sql helper insert_data command:%s, data:%s' % (command, data))
x = self.cursor.execute(command, data)
if commit:
self.conn.commit()
return x
except Exception, e:
utils.log('sql helper insert_data exception msg:%s' % str(e), logging.WARNING)
def commit(self):
self.conn.commit()
def execute(self, command, commit = True):
try:
utils.log('sql helper execute command:%s' % command)
data = self.cursor.execute(command)
if commit:
self.conn.commit()
return data
except Exception, e:
utils.log('sql helper execute exception msg:%s' % str(e))
return None
def query(self, command, commit = False):
try:
utils.log('sql helper execute command:%s' % command)
self.cursor.execute(command)
data = self.cursor.fetchall()
if commit:
self.conn.commit()
return data
except Exception, e:
utils.log('sql helper execute exception msg:%s' % str(e))
return None
def query_one(self, command, commit = False):
try:
utils.log('sql helper execute command:%s' % command)
self.cursor.execute(command)
data = self.cursor.fetchone()
if commit:
self.conn.commit()
return data
except Exception, e:
utils.log('sql helper execute exception msg:%s' % str(e))
return None