반응형
import pymysql
conn = pymysql.connect(
    user='id',
    passwd='password',
    host='111.10.1.1',
    db='test_db',
    charset='utf8'
)



#df는 컬럼이 8개인 데이터프레임

cursor = conn.cursor()
sql = 'insert into test_tbl (col1, col2, col3, col4, col5, col6, col7,col8) values '
data = []
for idx, i in tqdm(enumerate(df.values)):
    sql += '(%s, %s, %s, %s, %s, %s, %s, %s),'
    for j in i:
        data.append(j)
    if idx % 30 == 0:
        sql = sql.rstrip(',')
        cursor.execute(sql, tuple(data))
        sql = 'insert into test_tbl (col1, col2, col3, col4, col5, col6, col7,col8) values '
        data = []
sql = sql.rstrip(',')
try:
    cursor.execute(sql, tuple(data))
except:
    pass
conn.commit()
conn.close()
반응형

+ Recent posts