forked from awolfly9/IPProxyTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlhelper.py
80 lines (63 loc) · 2.48 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
#-*- coding: utf-8 -*-
import logging
import mysql.connector
import utils
import config
from singleton import Singleton
class SqlHelper(Singleton):
def __init__(self):
self.database_name = config.free_ipproxy_database
self.init()
def init(self):
self.database = mysql.connector.connect(**config.database_config)
self.cursor = self.database.cursor()
self.create_database()
self.database.database = self.database_name
def create_database(self):
try:
command = 'CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARACTER SET \'utf8\' ' % self.database_name
utils.log('sql helper create_database command:%s' % command)
self.cursor.execute(command)
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)
self.cursor.execute(command)
self.database.commit()
except Exception, e:
utils.log('sql helper create_table exception:%s' % str(e), logging.WARNING)
def insert_data(self, command, data):
try:
utils.log('insert_data command:%s, data:%s' % (command, data))
self.cursor.execute(command, data)
self.database.commit()
except Exception, e:
utils.log('sql helper insert_data exception msg:%s' % str(e), logging.WARNING)
def execute(self, command):
try:
utils.log('sql helper execute command:%s' % command)
data = self.cursor.execute(command)
self.database.commit()
return data
except Exception, e:
utils.log('sql helper execute exception msg:%s' % str(e))
return None
def query(self, command):
try:
utils.log('sql helper execute command:%s' % command)
self.cursor.execute(command)
data = self.cursor.fetchall()
return data
except Exception, e:
utils.log('sql helper execute exception msg:%s' % str(e))
return None
def query_one(self, command):
try:
utils.log('sql helper execute command:%s' % command)
self.cursor.execute(command)
data = self.cursor.fetchone()
return data
except Exception, e:
utils.log('sql helper execute exception msg:%s' % str(e))
return None