Python Session 17j Updated
Python Session 17j Updated
Objectives
Use unique id while retrieving, updating and deleting the data from
Tables.
Follow all the constraints rules while inserting and updating the data.
Implement
Programming with
Multiple Tables
(Contd.)
Constraints in Database Tables
cur.execute('''CREATE TABLE IF NOT EXISTS People
(id INTEGER PRIMARY KEY, name TEXT UNIQUE, retrieved INTEGER)''')
cur.execute('''CREATE TABLE IF NOT EXISTS Follows
(from_id INTEGER, to_id INTEGER, UNIQUE(from_id, to_id))''')
Observations
“name” column in the “People” Table must be UNIQUE.
Combination of the two numbers in each row of the “Follows” Table
must be Unique.
cur.execute('''INSERT OR IGNORE INTO People (name, retrieved)
VALUES ( ?, 0)''', ( friend, ) )
“OR IGNORE” clause should be added to the INSERT statement to indicate that
the INSERT statement should the violation of rule “name must be unique”.
Implement
Programming with
Multiple Tables
(Contd.)
Retrieve and/or insert a record
friend = u['screen_name']
cur.execute('SELECT id FROM People WHERE name
= ? LIMIT 1',(friend, ) )
try: except – Indicates the
friend_id = cur.fetchone()[0] instruction to be carried out in
countold = countold + 1 case of an exception. In this
except: case row was not found hence
cur.execute('''INSERT OR IGNORE INTO People should be inserted.
(name, retrieved)VALUES ( ?, 0)''', ( friend, ) ) INSERT OR IGNORE – Indicates
conn.commit() errors to be avoided while
if cur.rowcount != 1 : inserting data.
print 'Error inserting account:',friend cur.rowcount - Indicates how
continue many rows are affected.
friend_id = cur.lastrowid
countnew = countnew + 1
Use Different Types of Keys
JOIN clause indicates that the fields selected are from the
Follows and People Tables.
https://console.developers.google.com/flows/enableapi?
apiid=geocoding_backend&keyType=SERVER_SIDE
Google Developers
Console
Google Developers
Console
Visualizing Networks
and Interconnections
What is Visualizing
networks and
interconnections?
Visualizing Networks and
Interconnections (Contd.)
Answer: Primary
Activity
Problem Statement: