我有一个漂亮的汤4脚本,该脚本从网站上抓取数据,然后我想批量插入所抓取的数据的内容。下面是我创建的这个脚本,但是我想对其进行调整,以便可以将listoflists批量插入到我的数据库中(出于效率考虑)

listoflists = []
x = 1
A_var = 11
B_var = 22
while x < 3:
    mlist = A_var, B_var
    listoflists.append(mlist)

    x = x + 1


print listoflists
print A_var
print B_var


cur_urls.execute ("""INSERT INTO test_table (ac, bc) VALUES (%s, %s);""",(A_var, B_var,))
conn_urls.commit()


cur_comp.close()
cur_urls.close()
conn_urls.close()

最佳答案

listoflists = []
x = 1
A_var = 11
B_var = 22
while x < 3:
    mlist = A_var, B_var
    listoflists.append(mlist)

    x = x + 1


print listoflists #[(11,12),(11,12)] This is listOfTuple
print A_var #11
print B_var #12

for tup in listoflists:
    cur_urls.execute ("""INSERT INTO test_table (ac, bc) VALUES (%s, %s);""",(tup[0], tup[1]))

conn_urls.commit()


cur_comp.close()
cur_urls.close()
conn_urls.close()

关于python - 批量插入Python SQL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42109294/

10-12 22:44