Advertisement
Guest User

Untitled

a guest
Dec 1st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. import sqlite3
  2.  
  3. conn = sqlite3.connect('dbname.db')
  4. c = conn.cursor()
  5.  
  6. def createTable():
  7.  
  8. c.execute("""CREATE TABLE IF NOT EXISTS persons(
  9. id INTEGER PRIMARY KEY AUTOINCREMENT,
  10. firstname TEXT,
  11. lastname TEXT
  12. )
  13. """)
  14.  
  15. c.execute("""CREATE TABLE IF NOT EXISTS scores(
  16. idperson INTEGER,
  17. task INTEGER,
  18. points INTEGER,
  19. FOREIGN KEY(idperson) REFERENCES persons(id) ON DELETE CASCADE
  20. )
  21. """)
  22.  
  23.  
  24.  
  25. #c.execute('drop table persons')
  26. #c.execute('drop table scores')
  27.  
  28.  
  29. def insertPersons():
  30.  
  31. myDict = {}
  32. with open('score2.txt', 'r') as file:
  33.  
  34. for line in file:
  35. textSplit = line.split()
  36.  
  37. firstName = textSplit[2]
  38. lastName = textSplit[3]
  39. number = textSplit[4]
  40.  
  41. if firstName + lastName in myDict:
  42. myDict[firstName + lastName] += int(number)
  43.  
  44. else:
  45. myDict[firstName + lastName] = int(number)
  46.  
  47. c.execute("INSERT INTO persons (firstname, lastname) VALUES (?,?) ",
  48. (firstName, lastName))
  49.  
  50. conn.commit()
  51.  
  52.  
  53. def insertScores():
  54.  
  55. with open('score2.txt', 'r') as file:
  56.  
  57. for row in file:
  58. textSplit = row.split()
  59.  
  60. assignment = int(textSplit[1])
  61. firstName = textSplit[2]
  62. lastName = textSplit[3]
  63. number = int(textSplit[4])
  64.  
  65. c.execute("SELECT ID FROM persons where firstname=? AND lastname=?", (firstName, lastName))
  66.  
  67. for data in c.fetchall():
  68. c.execute("INSERT INTO scores (idperson, task, points) VALUES (?,?,?) ",
  69. (int(data[0]), assignment, number))
  70.  
  71. conn.commit()
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. createTable()
  79.  
  80. insertPersons()
  81. insertScores()
  82.  
  83. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement