-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdf_bulk_insert.py
36 lines (27 loc) · 1.18 KB
/
df_bulk_insert.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
import pandas as pd
import mysql.connector
class DataFrameBulkInsert:
def __init__(self, table_name, dataframe, db_cursor):
self.table_name = table_name
self.dataframe = dataframe
self.db_cursor = db_cursor
def bulk_insert(self):
query = self.build_query()
self.db_cursor.execute(query)
first_row = self.db_cursor.lastrowid
row_count = self.db_cursor.rowcount
return list(range(first_row, first_row+row_count))
def build_template(self,column_count, quote_on_string = False):
value_gap = "{!r}" if quote_on_string else "{}"
return "(" + value_gap + ((", " + value_gap) * (column_count-1)) + ")"
def build_query(self):
column_count = self.dataframe.shape[1]
query = "insert into " + self.table_name
query += self.build_template(column_count).format(*self.dataframe.columns)
query += " values "
for i, row in self.dataframe.iterrows():
if i > 0:
query += ",\n"
campos = tuple(row.values.tolist())
query += self.build_template(column_count, True).format(*campos)
return query