通过python将记录插入表中

2024-10-02 12:36:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个列表列[],行[]。我正在尝试在名为docs的表中插入记录。 表docs中的列是:(('suggestedpa', None), ('loadid', None ))

代码

 conn.execute('INSERT INTO docs ('+str(columns)+') VALUES ('+str(rows)+')')

错误sqlite3.OperationalError: table docs has no column named 'suggestedpa', 'loadid'


Tags: columns代码nonedocs列表execute记录conn
1条回答
网友
1楼 · 发布于 2024-10-02 12:36:26

一种方法是:

columns=['suggestedpa', 'loadid']
rows=['U.S. Bank National Association Intellectual Ventures II LLC', '233996'] 
columnsHelp = map(str,columns)
columnsStr = ",".join(columnsHelp) # this is now a string
rowsHelp = map(str,rows)
#rowsStr = ",".join(rowsHelp) # this is now a string val1,value
# do this 
rowsStr = "'" + "','".join(map(str, rowsHelp)) + "'"
conn.execute("INSERT INTO docs ("+columnsStr+") VALUES ("+rowsStr+")")

祝你好运

相关问题 更多 >

    热门问题