-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlserver.py
143 lines (119 loc) · 5.33 KB
/
sqlserver.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
import pyodbc
import logging
import time
import shutil
import os
import fileinput
class SqlServer:
def __init__(self, database):
try:
self.database = database
self.server = 'SRVDHTBD\DESENV'
self.username = 'xxx'
self.password = 'xxx'
self.tmpdir_sql = r'\\SrvPBD\xxx\temp_xxx'
self.connect = pyodbc.connect('Driver={SQL Server Native Client 11.0};Server='+self.server+';Database='+self.database+';UID='+self.username+';PWD='+self.password+';')
self.cursor = self.connect.cursor()
except Exception as e:
logging.debug('Erro SqlServer função=init: ' + str(e))
raise Exception('Erro SqlServer 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 SqlServer função=select_table_one: ' + str(e))
raise Exception('Erro SqlServer 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 SqlServer função=select_table_count: ' + str(e))
raise Exception('Erro SqlServer 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 SqlServer função=select_table: ' + str(e))
raise Exception('Erro SqlServer 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 SqlServer função=update_table: ' + str(e))
raise Exception('Erro SqlServer 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 SqlServer função=insert_table: ' + str(e))
raise Exception('Erro SqlServer 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 SqlServer função=insert_multiple_table: ' + str(e))
raise Exception('Erro SqlServer 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 SqlServer função=truncate_table: ' + str(e))
raise Exception('Erro SqlServer 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 SqlServer função=delete_table: ' + str(e))
raise Exception('Erro SqlServer 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 SqlServer função=exec_procedure_simples: ' + str(e))
raise Exception('Erro SqlServer 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 SqlServer função=bulk_table: ' + str(e))
raise Exception('Erro SqlServer função=bulk_table: ' + str(e))
def close_connect(self):
try:
self.connect.close()
except Exception as e:
logging.debug('Erro SqlServer função=close_connect: ' + str(e))
raise Exception('Erro SqlServer função=close_connect: ' + str(e))
if __name__=="__main__":
db = SqlServer("Nome_da_base_de_dados")