2015-02-07 4 views
1

이 쿼리를 내 데이터베이스에 게시하려고합니다. 문제는 placeholder가있는 csv에서 쿼리하는 것입니다. 얻을 수없는 것처럼 보입니다. Time_Date_Uploaded varchar (255) DEFAULT NULL은 현재 시간으로 채워집니다. 여기 내 코드는 다음과 같습니다.파이썬 쿼리를 알아낼 수 없습니다.

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에 몇 개의 항목이 있는지 알 수 없습니다. 나는 그것이 목록이라고 생각한다. 데이터베이스에있는 필드 수와 동일한 항목 수를 빼면 row.append(now)cursor.execute 전에 간단히 수행 할 수 있어야 작동 할 수 있습니다. 필드가있는 항목 수와 정확히 같은 항목이있는 경우 무언가를 버리고 now으로 바꿔야합니다. 예를 들어 row[-1] = nowcursor.execute 앞에 입력해야합니다. row 목록의 필드 수를 모르는 경우 print len(row)을 디버그로 처리하십시오. 어느 쪽이든 row 목록에 now 값을 가져와야합니다.

p.s. 파이썬 2를 print 문에서 사용한다고 가정합니다. 그래서 같은 인쇄 의미론을 사용합니다. 제가 틀렸고 python 3을 사용하는 경우 print()을 대신 사용하십시오 :)

+0

thanks man! 완벽하게 작동합니다. Unfort는 upvote가 내가 15 평판 또는 무엇인가 필요하다고 말한다. – nematillo

관련 문제