Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. import sqlite3
  2.  
  3. # print(dir(sqlite3))
  4.  
  5. # conn = sqlite3.connect(r'C:\Users\User\Documents\Python_October\strongbad_emails.sqlite')
  6. # cur = conn.cursor()
  7. # data = cur.execute('SELECT title, message FROM email').fetchall()
  8. # conn.close()
  9.  
  10. # print(data[:10])
  11.  
  12.  
  13. def insert_data(data, table, path=r'C:\Users\User\Documents\Python_October\test.sqlite'):
  14. with sqlite3.connect(path) as conn:
  15. cur = conn.cursor()
  16. for row in data:
  17. cur.execute(f'INSERT INTO {table} VALUES (?, ?, ?, ?, ?)', row)
  18. data = cur.execute(f'SELECT * FROM {table}').fetchall()
  19. print(data)
  20.  
  21. def drop_table(table, path=r'C:\Users\User\Documents\Python_October\test.sqlite'):
  22. with sqlite3.connect(path) as conn:
  23. cur = conn.cursor()
  24. cur.execute(f'DROP TABLE {table}')
  25. print(f'Table {table} was succesfully destroyed')
  26.  
  27. def create_table(table, path=r'C:\Users\User\Documents\Python_October\test.sqlite'):
  28. with sqlite3.connect(path) as conn:
  29. cur = conn.cursor()
  30. cur.execute(f'CREATE TABLE IF NOT EXISTS {table} (id integer primary key, name text unique, age integer, gender varchar(2), hobby text)')
  31. data = cur.execute(f'SELECT * FROM {table}').fetchall()
  32. print(data)
  33.  
  34. drop_table(table='people')
  35. create_table(table='people')
  36. insert_data([(1, "Anton", 27, "M", "Audiobookmaker/Voice Acting"),
  37. (2, "Timofei", 24, "M", "Sleeping"),
  38. (3, "Lex", 26, "M", "Skyrunning"),
  39. (4, "Alena", 26, "F", "Avokado Growing")], 'people')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement