Skip to content

Commit

Permalink
Update fast_upload.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jzsmoreno committed Dec 23, 2023
1 parent 3beabfc commit 6db45b9
Showing 1 changed file with 5 additions and 23 deletions.
28 changes: 5 additions & 23 deletions pydbsmgr/fast_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ def import_table(
"""If the table exists, it will be deleted and recreated"""
self._cur.execute("DROP TABLE %s" % (table_name))
self._cur.execute(
self._create_table_query(
table_name, df, char_length, override_length
)
self._create_table_query(table_name, df, char_length, override_length)
)
else:
warning_type = "UserWarning"
Expand All @@ -70,10 +68,7 @@ def import_table(
self._cur.executemany(
self._insert_table_query(table_name, df),
[
[
None if (isinstance(value, float) and np.isnan(value)) else value
for value in row
]
[None if (isinstance(value, float) and np.isnan(value)) else value for value in row]
for row in df.values.tolist()
],
)
Expand All @@ -84,9 +79,7 @@ def import_table(
msg = "Table {%s}, successfully imported!" % table_name
print(f"{msg}")

def upload_table(
self, df: DataFrame, table_name: str, verbose: bool = False
) -> None:
def upload_table(self, df: DataFrame, table_name: str, verbose: bool = False) -> None:
"""Access to update data from a dataframe to a database"""

"""Check if the current connection is active. If it is not, create a new connection"""
Expand All @@ -104,9 +97,7 @@ def upload_table(
try:
"""Insert data"""
self._cur.fast_executemany = True
self._cur.executemany(
self._insert_table_query(table_name, df), df.values.tolist()
)
self._cur.executemany(self._insert_table_query(table_name, df), df.values.tolist())
self._con.close()
except pyodbc.Error as e:
print(e)
Expand Down Expand Up @@ -280,17 +271,8 @@ def _execute_query(self, query: str):
########################################################################################

if __name__ == "__main__":
# with open("conn.pkl", "rb") as file:
# connection_string = pickle.load(file)
#
# connection_url = URL.create(
# "mssql+pyodbc", query={"odbc_connect": connection_string}
# )
#
# engine = create_engine(connection_url)
#
connection_string = os.getenv("conn_string")
# # Create a DataFrame
# Create a DataFrame
data = {"Name": ["John", "Alice", "Bob"], "Age": [25, 30, 35]}
df = pd.DataFrame(data)
table_name = "test_table"
Expand Down

0 comments on commit 6db45b9

Please sign in to comment.