728x90
def input_sql(data, input_table_name, pk=None):
"""
data: Input Data DataFrame
input_table_name: Table name Str
pk: Primary Key Name List
"""
colnames = list(data.columns)
INPUT_NAME = ''
for col in colnames:
INPUT_NAME = INPUT_NAME + col.upper() +','
INPUT_NAME = INPUT_NAME[:-1]
VALUES_INPUT = ''
for col in colnames:
VALUES_INPUT = VALUES_INPUT +'"%s",'
VALUES_INPUT = VALUES_INPUT[:-1]
sql = f"INSERT INTO {input_table_name} ({INPUT_NAME}) VALUES ({VALUES_INPUT})"
if pk==None:
sql = f"INSERT INTO {input_table_name} ({INPUT_NAME}) VALUES ({VALUES_INPUT})"
else:
UPDATE_VALUE = ''
for key in pk:
UPDATE_VALUE = UPDATE_VALUE +key+"="+'"%s",'
UPDATE_VALUE=UPDATE_VALUE[:-1]
sql = f"INSERT INTO {input_table_name} ({INPUT_NAME}) VALUES ({VALUES_INPUT}) ON DUPLICATE KEY UPDATE {UPDATE_VALUE}"
return sql
### Pymysql ํจ์
cursor.execute(sql)
cursor.executemany(sql, list(set))
** Format Type ์ฃผ์
๋ฐ์ํ
'๐ข๏ธ Database' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SQL] Count ํจ์, Sequel vs SQL (0) | 2023.08.19 |
---|---|
Apache Parquet(ํ์ผ์ด) (0) | 2023.08.14 |
[์์ธ] Mysql database ์ ๋ ฌ (0) | 2023.07.31 |
[Mysql] python executemany ์ฌ์ฉ์ TypeError: not all arguments converted during string ... (0) | 2022.03.23 |
[Mysql] Python Sql query Format Error (0) | 2022.03.21 |