简体   繁体   中英

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". 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. I wrote this code for collecting data, and each of this eight source code have the same syntax to insert values to "8box"

I want to run this eight source code at the same time to be able to collect data.

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. 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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM