我已经写了eight source code
来在名为“8box”的表中将一些值插入名为“TA”的数据库中。我打算同时运行这个eight source code
,然后这个eight source code
将数据保存在数据库中将近一个小时。我编写了这个用于收集数据的代码,每个eight source code
都有相同的语法将值插入“8box”
我想同时运行此eight source code
以便能够收集数据。
我尝试了这个,结果是数据库只填充了第一个源代码的值,没有来自存储在数据库中的其他源的值
我该怎么办?
waktu=time.strftime('%Y-%m-%d %H:%M:%S')
con = mdb.connect('localhost', 'root', 'qwer1234', 'TA');
with con:
cur = con.cursor()
#cur.execute("DROP TABLE IF EXISTS 8Box")
#cur.execute("CREATE TABLE 8Box (Name VARCHAR(25),Lot_Bid INT,Bid INT,Offer INT,Lot_Offer INT,Time DATETIME)")
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_1_new,bid_1_new,off_1_new,lot_off_1_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_2_new,bid_2_new,off_2_new,lot_off_2_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_3_new,bid_3_new,off_3_new,lot_off_3_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_4_new,bid_4_new,off_4_new,lot_off_4_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_5_new,bid_5_new,off_5_new,lot_off_5_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_6_new,bid_6_new,off_6_new,lot_off_6_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_7_new,bid_7_new,off_7_new,lot_off_7_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_8_new,bid_8_new,off_8_new,lot_off_8_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_9_new,bid_9_new,off_9_new,lot_off_9_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_10_new,bid_10_new,off_10_new,lot_off_10_new,waktu))
答案 0 :(得分:0)
您应该使用MySQLdb
接口而不是_mysql
接口。我会把它编码为:
cn = MySQLdb.connect(...)
c = cn.cursor()
try:
c.executemany("""
INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)
""", [
(name_new,lot_bid_1_new,bid_1_new,off_1_new,lot_off_1_new,waktu),
(name_new,lot_bid_2_new,bid_2_new,off_2_new,lot_off_2_new,waktu),
# .. etc.
(name_new,lot_bid_10_new,bid_10_new,off_10_new,lot_off_10_new,waktu)
])
cn.commit()
except:
cn.rollback()
raise
更新:这是两个进程同时运行的screendump: