【发布时间】:2018-10-14 22:51:04
【问题描述】:
我正在尝试将虚拟数据插入 mysql 数据库。
数据库结构如下:
database name: messaround
database table name: test
表结构:
id (Primary key, auto increment)
path (varchar(254))
UPDATED 2 方法和错误。 我有一种方法可以尝试通过以下方式插入:
def insert_into_db(dbcursor, table, *cols, **vals):
try:
query = "INSERT INTO {} ({}) VALUES ('{}')".format(table, ",".join(cols), "'),('".join(vals))
print(query)
dbcursor.execute(query)
dbcursor.commit()
print("inserted!")
except pymysql.Error as exc:
print("error inserting...\n {}".format(exc))
connection=conn_db()
insertstmt=insert_into_db(connection, table='test', cols=['path'], vals=['test.com/test2'])
但是,这是失败的说法:
INSERT INTO test () VALUES ('vals'),('cols')
error inserting...
(1136, "Column count doesn't match value count at row 1")
你能帮忙吗?
谢谢。
【问题讨论】:
-
我看到路径来自“vals”,它是字符而不是数字。你应该用 "path_val" 之类的引号括起来
-
@UdayS - 你能把这个放到答案中,这样我才能看得更清楚。