简体   繁体   中英

Can't figure out python query.

I am trying to post this query into my database. The problem is I'm querying from a csv that has placeholders: I can't seem to get: Time_Date_Uploaded varchar(255) DEFAULT NULL filled with the current time. here is my code below:

    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"

I can't see how many items the row has. I assume it's a list. If it has the number of items equal to fields in your database minus one, you should be able to simply do row.append(now) before cursor.execute and it would work. If it has the exact same number of items as you have fields, you have to throw something out and replace it with now , for example do this row[-1] = now before cursor.execute . If you don't know how many fields your row list has, do a print len(row) as a debug. Either way you need to get the now value into the row list.

ps I'm assuming you are using python 2 from the print statement, that's why I use the same print semantic, if I'm wrong and you're on python 3, use print() instead of course :)

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