Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- conn = sqlite3.connect(':memory:')
- c = conn.cursor()
- c.execute("CREATE TABLE mytable (id integer, name text, descriptionid integer)")
- c.execute("CREATE TABLE descriptiontable (id integer, description text)")
- # Create the view
- 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')
- c.execute('INSERT INTO mytable VALUES(1, "abcdef", 1)');
- c.execute('INSERT INTO mytable VALUES(2, "ghijkl", 1)');
- c.execute('INSERT INTO mytable VALUES(3, "iovxcd", 2)');
- c.execute('INSERT INTO mytable VALUES(4, "zuirur", 1)');
- c.execute('INSERT INTO descriptiontable VALUES(1, "Description1")');
- c.execute('INSERT INTO descriptiontable VALUES(2, "Description2")');
- # Use the view
- c.execute('SELECT id, name, description FROM mytable_with_desc')
- print c.fetchall()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement