-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql.py
148 lines (123 loc) · 5.31 KB
/
mysql.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import pymysql
import logging
import time
import shutil
import os
import fileinput
class MySQL:
def __init__(self):
try:
self.database = 'teste_raspagem'
#self.server = 'SRVDHTBD\DESENV'
#self.username = 'xxx'
#self.password = 'xxx'
self.server = 'server_aqui_xxx'
self.username = 'teste_raspagem'
self.password = 'ProjetoPython19@'
#self.tmpdir_sql = r'\\SrvPBD\SELECOES_BI\temp_carga'
self.tmpdir_sql = r'D:\\Downloads'
self.connect = pymysql.connect( host=self.server, user=self.username, passwd=self.password, db=self.database )
self.cursor = self.connect.cursor()
except Exception as e:
logging.debug('Erro SQL função=init: ' + str(e))
raise Exception('Erro SQL função=init: ' + str(e))
def select_table_one(self, sqlcommand):
try:
self.cursor.execute(sqlcommand)
return self.cursor.fetchone()
except Exception as e:
logging.debug('Erro SQL função=select_table_one: ' + str(e))
raise Exception('Erro SQL função=select_table_one: ' + str(e))
def select_table_count(self, sqlcommand):
try:
self.cursor.execute(sqlcommand)
cont = self.cursor.fetchone()
if cont[0] > 0:
return True
else:
return False
except Exception as e:
logging.debug('Erro SQL função=select_table_count: ' + str(e))
raise Exception('Erro SQL função=select_table_count: ' + str(e))
def select_table(self, sqlcommand):
try:
self.cursor.execute(sqlcommand)
return self.cursor.fetchall()
except Exception as e:
logging.debug('Erro SQL função=select_table: ' + str(e))
raise Exception('Erro SQL função=select_table: ' + str(e))
def update_table(self, sqlcommand):
try:
self.cursor.execute(sqlcommand)
self.connect.commit()
except Exception as e:
logging.debug('Erro SQL função=update_table: ' + str(e))
raise Exception('Erro SQL função=update_table: ' + str(e))
def insert_table(self, sqlcommand):
try:
self.cursor.execute(sqlcommand)
self.connect.commit()
except Exception as e:
logging.debug('Erro SQL função=insert_table: ' + str(e))
raise Exception('Erro SQL função=insert_table: ' + str(e))
def insert_multiple_table(self, sqlcommand, params):
try:
t0 = time.time()
self.cursor.executemany(sqlcommand, params)
self.connect.commit()
print(f'{time.time() - t0:.1f} seconds')
except Exception as e:
logging.debug('Erro SQL função=insert_multiple_table: ' + str(e))
raise Exception('Erro SQL função=insert_multiple_table: ' + str(e))
def truncate_table(self, tabela):
try:
strSql = "TRUNCATE TABLE {0}".format(tabela)
self.cursor.execute(strSql)
self.connect.commit()
except Exception as e:
logging.debug('Erro SQL função=truncate_table: ' + str(e))
raise Exception('Erro SQL função=truncate_table: ' + str(e))
def delete_table(self, sqlcommand):
try:
self.cursor.execute(sqlcommand)
self.connect.commit()
except Exception as e:
logging.debug('Erro SQL função=delete_table: ' + str(e))
raise Exception('Erro SQL função=delete_table: ' + str(e))
def exec_procedure_simples(self, procedure):
try:
self.cursor.execute('exec {0}'.format(procedure))
self.connect.commit()
except Exception as e:
logging.debug('Erro SQL função=exec_procedure_simples: ' + str(e))
raise Exception('Erro SQL função=exec_procedure_simples: ' + str(e))
def bulk_table(self, table, filename, debug=False, firstrow=2, fieldtermitor=';', rowterminator='\\n'):
try:
base = os.path.basename(filename)
newfile = '{0}\{1}'.format(self.tmpdir_sql, base)
if debug == False:
shutil.copy(filename, newfile)
with fileinput.FileInput(newfile, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace('"', ''), end='')
sqlcommand = """
BULK INSERT {0}
FROM '{1}' WITH (
FIRSTROW={2},
FIELDTERMINATOR='{3}',
ROWTERMINATOR='{4}'
);
""".format(table, newfile, firstrow, fieldtermitor, rowterminator)
self.cursor.execute(sqlcommand)
self.connect.commit()
except Exception as e:
logging.debug('Erro SQL função=bulk_table: ' + str(e))
raise Exception('Erro SQL função=bulk_table: ' + str(e))
def close_connect(self):
try:
self.connect.close()
except Exception as e:
logging.debug('Erro SQL função=close_connect: ' + str(e))
raise Exception('Erro SQL função=close_connect: ' + str(e))
if __name__=="__main__":
db = SQL()