Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. import os
  2. import sqlite3
  3.  
  4. # create a default path to connect to and create (if necessary) a database
  5. # called 'database.sqlite3' in the same directory as this script
  6. DEFAULT_PATH = os.path.join(os.path.dirname(__file__), 'database.sqlite3')
  7.  
  8.  
  9. def main():
  10. con = db_connect() # connect to the database
  11. cur = con.cursor() # instantiate a cursor obj
  12.  
  13. trains_sql = """
  14. CREATE TABLE trains (
  15. id_pociagu text PRIMARY KEY,
  16. nazwa text NOT NULL,
  17. miejsca integer NOT NULL,
  18. predkosc real NOT NULL)"""
  19. cur.execute(trains_sql) # add trains_sql table to db
  20. connections_sql = """
  21. CREATE TABLE connections (
  22. id_pociagu text PRIMARY KEY,
  23. destination text NOT NULL,
  24. price real NOT NULL,
  25. FOREIGN KEY (id_pociagu) REFERENCES trains (id_pociagu))"""
  26. cur.execute(connections_sql) # add connections_sql table to db
  27.  
  28. create_train(con, 'IC 64106/7', 'DAMROT', 150, 80)
  29. create_connection(con, 'IC 64106/7', 'Opole Główne', 20)
  30. create_train(con, 'IC 6300/1', 'WYSPIAŃSKI', 200, 90)
  31. create_connection(con, 'IC 6300/1', 'Kraków Główny', 30)
  32. create_train(con, 'IC 6128/9', 'NAŁKOWSKA', 250, 85.5)
  33. create_connection(con, 'IC 6128/9', 'Warszawa Centralna', 45)
  34. create_train(con, 'TLK 74100/1', 'ŚLĄZAK', 125, 82)
  35. create_connection(con, 'TLK 74100/1', 'Katowice', 27.33)
  36. create_train(con, 'TLK 71106/7', 'KONOPNICKA', 155, 80)
  37. create_connection(con, 'TLK 71106/7', 'Częstochowa', 35.40)
  38. create_train(con, 'IC 6504/5', 'HEWELIUSZ', 250, 78)
  39. create_connection(con, 'IC 6504/5', 'Bydgoszcz Główna', 30)
  40.  
  41. cur.execute("SELECT * FROM trains INNER JOIN connections ON trains.id_pociagu = connections.id_pociagu WHERE predkosc>85")
  42. for i in cur.fetchall():
  43. print(i)
  44. print()
  45. cur.execute("SELECT * FROM trains INNER JOIN connections ON trains.id_pociagu = connections.id_pociagu WHERE nazwa='KONOPNICKA'")
  46. for i in cur.fetchall():
  47. print(i)
  48. print()
  49. cur.execute("SELECT * FROM trains INNER JOIN connections ON trains.id_pociagu = connections.id_pociagu WHERE miejsca>150 AND price<=30")
  50. for i in cur.fetchall():
  51. print(i)
  52. print()
  53. # ponizej : usuwa wszystkie pociagi ktorych ceny biletu sa mniejsze rowne 30
  54. cur.execute("DELETE FROM trains WHERE id_pociagu IN ( SELECT id_pociagu FROM connections WHERE price <=30)")
  55. cur.execute("SELECT * FROM trains INNER JOIN connections ON trains.id_pociagu = connections.id_pociagu")
  56. for i in cur.fetchall():
  57. print(i)
  58. # jak widac, zostaly tylko 2 pociagi, bo reszta miala tansze bilety niz 30
  59.  
  60.  
  61. def db_connect(db_path=DEFAULT_PATH):
  62. con = sqlite3.connect(db_path)
  63. return con
  64.  
  65.  
  66. def create_train(con, id_pociagu, nazwa, miejsca, predkosc):
  67. sql = """
  68. INSERT INTO trains (id_pociagu, nazwa, miejsca, predkosc) VALUES (?, ?, ?, ?)"""
  69. cur = con.cursor()
  70. cur.execute(sql, (id_pociagu, nazwa, miejsca, predkosc))
  71.  
  72.  
  73. def create_connection(con, id_pociagu, destination, price):
  74. sql = """
  75. INSERT INTO connections (id_pociagu, destination, price) VALUES (?, ?, ?)"""
  76. cur = con.cursor()
  77. cur.execute(sql, (id_pociagu, destination, price))
  78.  
  79.  
  80. if __name__ == '__main__':
  81. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement