2015-02-07 62 views
1

我想將這個查詢發佈到我的數據庫中。問題是我從包含佔位符的csv查詢:我似乎無法得到:Time_Date_Uploaded varchar(255)DEFAULT NULL填充當前時間。這裏是我的代碼如下:找不到python查詢。

cursor = mydb.cursor() # declare object 
now = time.strftime('%Y-%m-%d %H:%M:%S') #want to send the current time to the database. 
cr.next() # need to skip first line of csv 
for row in cr: 
    cursor.execute('INSERT INTO loans2(LoanId, NoteId, OrderId, OutstandingPrincipal, AccruedInterest, Status, AskPrice, Markup_Discount, YTM, DaysSinceLastPayment, CreditScoreTrend, FICO_End_Range, Date_Time_Listed, NeverLate, Loan_Class, Loan_Maturity, Original_Note_Amount, Interest_Rate, Remaining_Payments, Principal_plus_Interest, Time_Date_Uploaded) VALUES ("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")' % tuple(row)) 
# #close the connection to the database. 
    mydb.commit() 
cursor.close() 
print "Done" 

回答

1

我看不到row有多少項目。我認爲這是一個列表。如果它的項目數量等於數據庫中的字段數減1,那麼您應該可以在cursor.execute之前簡單地執行row.append(now)並且它可以工作。如果它具有與字段完全相同的物品數量,則必須丟棄某些物品並將其替換爲now,例如在cursor.execute之前執行此操作row[-1] = now。如果您不知道您的row列表中有多少個字段,請執行print len(row)作爲調試。無論採用哪種方法,您都需要將now值存入row列表。

p.s.我假設你是從打印語句使用Python 2,這就是爲什麼我使用相同的打印語義,如果我錯了,你在Python 3,使用print(),而不是當然:)

+0

謝謝你!完美的作品。 Unfort不能upvote說我需要15點聲望或什麼的。 – nematillo 2015-02-07 21:15:06