Advertisement
dan-masek

Using a view in SQLite

Mar 21st, 2018
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. import sqlite3
  2.  
  3. conn = sqlite3.connect(':memory:')
  4. c = conn.cursor()
  5. c.execute("CREATE TABLE mytable (id integer, name text, descriptionid integer)")
  6. c.execute("CREATE TABLE descriptiontable (id integer, description text)")
  7.  
  8. # Create the view
  9. c.execute('CREATE VIEW mytable_with_desc AS SELECT mytable.id AS id, mytable.name AS name, descriptiontable.description AS description FROM mytable, descriptiontable WHERE mytable.descriptionid=descriptiontable.id')
  10.  
  11. c.execute('INSERT INTO mytable VALUES(1, "abcdef", 1)');
  12. c.execute('INSERT INTO mytable VALUES(2, "ghijkl", 1)');
  13. c.execute('INSERT INTO mytable VALUES(3, "iovxcd", 2)');
  14. c.execute('INSERT INTO mytable VALUES(4, "zuirur", 1)');
  15. c.execute('INSERT INTO descriptiontable VALUES(1, "Description1")');
  16. c.execute('INSERT INTO descriptiontable VALUES(2, "Description2")');
  17.  
  18. # Use the view
  19. c.execute('SELECT id, name, description FROM mytable_with_desc')
  20. print c.fetchall()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement