Guest User

Untitled

a guest
Jul 30th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. import pyodbc, psycopg2, string, os, sys, getopt
  2.  
  3. def clean_whitespace(var):
  4. """
  5. Remove all whitespace from a string.
  6. """
  7. return var.translate(string.maketrans("", ""), string.whitespace)
  8.  
  9. def migrate(accessdb, storev):
  10. """
  11. Takes two inputs, accessdb location and store number based on database. Converts access to postgresql.
  12. """
  13. access_db = accessdb
  14. # sqlite_db = "C:\\users\\tyler\\desktop\\django\\pccharge_archive\\dev.db"
  15. store = str(storev)
  16.  
  17. count = 0
  18. failcount = 0
  19.  
  20. #open sqlite connection
  21. sConnection = psycopg2.connect("dbname=pccharge_archive user=postgres password=lolinternet")
  22. sCursor = sConnection.cursor()
  23. #open msaccess connection
  24. mConnection = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};DBQ=%s' % access_db)
  25. mCursor = mConnection.cursor()
  26.  
  27. mCursor.execute("SELECT Card, Amount, Date, TroutD, Result, Station, Issuer FROM Trans")
  28. index = 0
  29. for row in mCursor:
  30. index += 1
  31. finditems = (store, str(row.TroutD), str(row.Amount)[:-2])
  32. sCursor.execute("SELECT * FROM creditcards_credit WHERE store_id=%s AND troutd=%s AND amount=%s", finditems)
  33. if not len(sCursor.fetchall()):
  34. items = (str(clean_whitespace(row.Card)), str(row.Amount)[:-2], str(row.Date)[:10], str(row.TroutD), clean_whitespace(row.Result), str(clean_whitespace(row.Station)), row.Issuer, store)
  35. sCursor.execute("INSERT INTO creditcards_credit (card, amount, date, troutd, result, station, issuer, store_id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)", items)
  36. count += 1
  37. else:
  38. failcount += 1
  39. sConnection.commit()
  40. # exit all connections
  41. sCursor.close()
  42. mCursor.close()
  43. mConnection.close()
  44. sConnection.close()
  45. return "%s records have been migrated for %s. \n%s records were skipped due to duplicate TroutD numbers." % (count, store, failcount)
  46.  
  47. if __name__ == '__main__':
  48. main(sys.argv[1:])
Add Comment
Please, Sign In to add comment