2016-05-16 23 views
0

我已經寫了eight source code在名爲「8box」的表中向一個名爲「TA」的數據庫中插入了一些值。我打算同時運行這個eight source code,然後這個eight source code在一個小時內將數據保存在數據庫中。我編寫了用於收集數據的代碼,並且此eight source code中的每一個都具有相同的語法以將值插入到「8box」將數據同時插入到多個應用程序中的一個表中使用MySQL

我想同時運行此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)) 
+1

所以你解除鎖定提交您的事務? – thebjorn

+0

什麼是鎖? – Okta

+0

mysql會鎖定您正在寫入但未提交的磁盤部分(在連接對象上調用commit方法),數據庫很可能會保留爲該表的其他任何寫入者鎖定。 (這將更容易顯示,如果你給我們一些代碼...) – thebjorn

回答

0

您應該使用MySQLdb接口,而不是_mysql接口。這裏的同時運行這兩個過程的screendump:我會把它作爲代碼:

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 

更新

enter image description here

+0

我嘗試此代碼,結果與以前相同,它只是存儲來自第一個源代碼的值 – Okta

+0

您如何發現(您使用哪種工具)只有來自第一個進程的值才能進入數據庫?你是否驗證過其他代碼實際上正在運行(它是否掛在執行?你也犯了嗎?) – thebjorn

+0

我知道它直接查看數據庫,並從那裏的第一個源代碼的值,我不知道如何驗證其他代碼是否正在運行,但我(鍵入python file1.py && python file2.py)只是運行源代碼的例子,而我在另一個代碼中提交 – Okta

相關問題