Python and SQLite: insert into table

后端 未结 6 655
梦如初夏
梦如初夏 2020-12-08 00:08

I have a list that has 3 rows each representing a table row:

>>> print list
[laks,444,M]
[kam,445,M]
[kam,445,M]

How to insert thi

6条回答
  •  青春惊慌失措
    2020-12-08 01:01

    This will work for a multiple row df having the dataframe as df with the same name of the columns in the df as the db.

    tuples = list(df.itertuples(index=False, name=None))
    
    columns_list = df.columns.tolist()
    marks = ['?' for _ in columns_list]
    columns_list = f'({(",".join(columns_list))})'
    marks = f'({(",".join(marks))})'
    
    table_name = 'whateveryouwant'
    
    c.executemany(f'INSERT OR REPLACE INTO {table_name}{columns_list} VALUES {marks}', tuples)
    conn.commit()
    

提交回复
热议问题