Saturday, April 19, 2008

How To Create VIEW

In Database, VIEW is a virtual table / derived TABLE.The CREATE VIEW command assigns a name to a pre-packaged SELECT statement. Once the view is created, it can be used in the FROM clause of another SELECT in place of a table name.

sql-command ::= CREATE [TEMP | TEMPORARY] VIEW [IF NOT EXISTS] [database-name.] view-name AS select-statement

On the other hand VIEW is a TABLE from MASTER TABLE in DATABASE


import sqlite3 as sqlite
#Connect to MASTER TABLE
conn=sqlite.connect('abcd.db')
cur=conn.cursor()
#execute SQL
cur.execute('CREATE VIEW new AS SELECT name,date FROM people')
for row in cur:
## print '---------------'
## print 'name:',row[0]
## print 'date:',row[1]
cur.close()
#Execute SQL from same Database but on different TABLE
conn=sqlite.connect('abcd.db')
cur.execute('SELECT * FROM new')
for row in cur:
print '--------------------'
print 'name:',row[0]
print 'date',row[1]
print '--------------------'


The output


-----------------------------
name: John
date: 21/3/2008
-----------------------------

No comments: