0% found this document useful (0 votes)
30 views

SQL in Python Practice

This document demonstrates how to use SQLite in Python to create an INSTRUCTOR database table, insert data, perform queries, update records, and retrieve the data into a Pandas dataframe. The table is created, three records are inserted, queries are run to select all records, a subset of records, and a single column. One record is updated, queries confirm the change. Finally, the query results are loaded into a Pandas dataframe and some properties are checked.

Uploaded by

Nazia Syed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

SQL in Python Practice

This document demonstrates how to use SQLite in Python to create an INSTRUCTOR database table, insert data, perform queries, update records, and retrieve the data into a Pandas dataframe. The table is created, three records are inserted, queries are run to select all records, a subset of records, and a single column. One record is updated, queries confirm the change. Finally, the query results are loaded into a Pandas dataframe and some properties are checked.

Uploaded by

Nazia Syed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL Practice about:srcdoc

In [2]: import sqlite3

In [3]: conn = sqlite3.connect('INSTRUCTOR.db')

In [4]: cursor_obj = conn.cursor()

In [5]: cursor_obj.execute("DROP TABLE IF EXISTS INSTRUCTOR")

Out[5]: <sqlite3.Cursor at 0x7fd9136bcfc0>

In [6]: table = """ create table IF NOT EXISTS INSTRUCTOR(ID INTEGER PRIMARY KEY NOT NULL, FNAME VARCHAR(20), LNAME VARCHAR(20), CITY VARCHAR(20)

In [7]: cursor_obj.execute(table)
print("Table is Ready")

Table is Ready

In [8]: cursor_obj.execute('''insert into INSTRUCTOR values (1, 'Rav', 'Ahuja', 'TORONTO', 'CA')''')

Out[8]: <sqlite3.Cursor at 0x7fd9136bcfc0>

In [9]: cursor_obj.execute('''insert into INSTRUCTOR values (2, 'Raul', 'Chong', 'Markham', 'CA'), (3, 'Hima', 'Vasudevan', 'Chicago', 'US')'''

Out[9]: <sqlite3.Cursor at 0x7fd9136bcfc0>

In [10]: statement = '''SELECT * FROM INSTRUCTOR'''


cursor_obj.execute(statement)

print("All the data")


output_all = cursor_obj.fetchall()
for row_all in output_all:
print(row_all)

All the data


(1, 'Rav', 'Ahuja', 'TORONTO', 'CA')
(2, 'Raul', 'Chong', 'Markham', 'CA')
(3, 'Hima', 'Vasudevan', 'Chicago', 'US')

In [11]: statement = '''SELECT * FROM INSTRUCTOR'''


1 of 3 11/15/23, 06:38
SQL Practice about:srcdoc
cursor_obj.execute(statement)

print("All the data")


# If you want to fetch few rows from the table we use fetchmany(numberofrows) and mention the number how many rows you want to fetch
output_many = cursor_obj.fetchmany(2)
for row_many in output_many:
print(row_many)

All the data


(1, 'Rav', 'Ahuja', 'TORONTO', 'CA')
(2, 'Raul', 'Chong', 'Markham', 'CA')

In [12]: statement = '''SELECT FNAME FROM INSTRUCTOR'''


cursor_obj.execute(statement)

print("All the data")


output_column = cursor_obj.fetchall()
for fetch in output_column:
print(fetch)

All the data


('Rav',)
('Raul',)
('Hima',)

In [13]: query_update='''update INSTRUCTOR set CITY='MOOSETOWN' where FNAME="Rav"'''


cursor_obj.execute(query_update)

Out[13]: <sqlite3.Cursor at 0x7fd9136bcfc0>

In [14]: statement = '''SELECT * FROM INSTRUCTOR'''


cursor_obj.execute(statement)

print("All the data")


output1 = cursor_obj.fetchmany(2)
for row in output1:
print(row)

All the data


(1, 'Rav', 'Ahuja', 'MOOSETOWN', 'CA')
(2, 'Raul', 'Chong', 'Markham', 'CA')

2 of 3 11/15/23, 06:38
SQL Practice about:srcdoc

In [15]: import pandas as pd


#retrieve the query results into a pandas dataframe
df = pd.read_sql_query("select * from instructor;", conn)

#print the dataframe


df

Out[15]: ID FNAME LNAME CITY CCODE

0 1 Rav Ahuja MOOSETOWN CA

1 2 Raul Chong Markham CA

2 3 Hima Vasudevan Chicago US

In [16]: df.LNAME[0]
df.shape

Out[16]: (3, 5)

In [17]: df.LNAME[0]

Out[17]: 'Ahuja'

In [18]: conn.close()

In [ ]:

3 of 3 11/15/23, 06:38

You might also like