Advertisement
AceScottie

csv2dict2sql

Jul 1st, 2019
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. cols= ["ctype", "cbid", "fname", "sname", "oname", "dob", "gender", "schoolid", "class", "address1", "address2", "postcode", "county", "intfolder", "extfolder", "therapist", "team", "dor", "status", "status2", "lastupdate", "updateby"] ##cant get this from csv yet
  2. data = [] ##empty list for dictionaries
  3. with open('clients.csv', mode='r') as f: ##csv file
  4.     rows = csv.reader(f)
  5.     for row in rows:
  6.         mydict = {}
  7.         for i in range(len(cols)):
  8.             mydict[cols[i]] = row[i] ##creates the dictionary from the manual columns
  9.         data.append(mydict) ##appeds it to the data list
  10.     for dict in data:##cycle through the dictionries
  11.         cur = DB.cursor()
  12.         qmarks = ', '.join('?' * len(dict))
  13.         qry = "Insert Into Table (%s) Values (%s)" % (qmarks, qmarks)
  14.         myquery = (qry, dict.keys() + dict.values()) ##Error Here
  15.         cur.execute(myquery)
  16.         DB.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement