[英]Insert data in one table within multiple app at the same time with MySQL
I have written eight source code
to insert some value to a database called "TA" in one table called "8box". 我已经编写了
eight source code
以便在一个名为“ 8box”的表中向“ TA”数据库插入一些值。 I am planning to run this eight source code
at the same time,and then this eight source code
keeps storing data in database in almost one hour. 我打算同时运行这
eight source code
,然后这eight source code
将在将近一个小时的时间内将数据存储在数据库中。 I wrote this code for collecting data, and each of this eight source code
have the same syntax to insert values to "8box" 我编写了这段代码来收集数据,并且这
eight source code
中的每一个都有相同的语法,可将值插入“ 8box”
I want to run this eight source code
at the same time to be able to collect data. 我想同时运行这
eight source code
,以便能够收集数据。
I tried this and the result is that the database is just filled with values from first source code, none values from another source stored in database 我尝试了这个,结果是数据库只被第一个源代码中的值填充,没有另一个源中的值存储在数据库中
What should i do ? 我该怎么办 ?
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))
You should use the MySQLdb
interface and not the _mysql
interface. 您应该使用
MySQLdb
接口而不是_mysql
接口。 I would code it as: 我将其编码为:
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
Update: here's a screendump of the two processes running simultaneously: 更新:这是同时运行的两个进程的屏幕转储:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.