解包多个值并存储到数据库失败

klh5stk1  于 2021-06-25  发布在  Mysql
关注(0)|答案(0)|浏览(232)

我有一个功能 calling insert_into_db_tmp() . 但是,当我执行它时,它使用的是完整的表示,而不是 test.com/test2 以及 path . calling insert_into_db_tmp() 声明如下:

def insert_into_db_tmp(dbcursor, table, col, val):
    try:
        query = "INSERT INTO %s (%s) VALUES (%s)",(table, ",".join(col), ",".join(val))
        print(query)
        dbcursor.execute(query)
        print("inserted!")
    except pymysql.Error as exc:
        print("error inserting...\n {}".format(exc))

我这样称呼:

connection=conn_db()
table = ['test']
col = ['path',]
val = ['test.com/test2',]
insert_into_db_tmp(connection, settings.dni+table,str(col),str(val))

当我执行这个时,我得到:

INSERT INTO test () VALUES ('{'vals': ['test.com/test2'], 'cols': ['path']}')
error inserting...
 (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'vals': ['test.com/test2'], 'cols': ['path']}')' at line 1")

注:目标 insert_into_db_tmp() 是允许多个 col 待输入,多个 val . 所以它们在未来可能会是这样的:

cols=['path','address']
vals=['somesite.com/id=6', '123 Hector Lane']

有人能帮我解决这个问题吗?
谢谢您。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题