解压缩多个值并存储到数据库中失败

时间:2018-05-05 14:47:18

标签: python mysql pymysql

我有一个我calling insert_into_db_tmp()的功能。但是,当我去执行它时,它使用完整的表示,而不是test.com/test2path的值。

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']

有人可以帮我解决这个问题吗?

谢谢。

0 个答案:

没有答案