Export or Import data from Excel to create SQLite database table using different libraries |
Displaying binary data ( Images ) taken from SQLite database in Colab platform |
from google.colab import drive
drive.mount('/content/drive')
!wget https://www.plus2net.com/python/download/my_db.db
%load_ext sql
# we can use the magic extension to connect to our SQLite DB
%sql sqlite:///my_db.db
# use %sql to write an inline SQL command
# use %%sql to write SQL commands in a cell
%%sql
SELECT name FROM sqlite_master WHERE type='table' ;
Output
Done.
name
category
subcategory
student
Structure of the student table
%sql PRAGMA table_info([student]);
Output
* sqlite:///my_db.db
Done.
cid name type notnull dflt_value pk
0 id INTEGER 0 None 1
1 name TEXT 0 None 0
2 class TEXT 0 None 0
3 mark INTEGER 0 None 0
4 gender TEXT 0 None 0
%%sql
SELECT * FROM student LIMIT 0,5
Output
* sqlite:///my_db.db
Done.
id name class mark gender
1 John Deo Four 75 female
2 Max Ruin Three 85 male
3 Arnold Three 55 male
4 Krish Star Four 60 female
5 John Mike Four 60 female
result = %sql SELECT * FROM student LIMIT 0,5;
#result.DataFrame().loc[0, 'name'] # first name only
result.DataFrame().loc[:,'name'] # all names
from sqlalchemy import create_engine
my_conn=create_engine("sqlite:////content/drive/MyDrive/my_db/my_db.db")
or by giving same path
my_conn=create_engine("sqlite:////my_db.db")
Database connection is ready, we will check by using this code to list the tables available in our sample database.
r_set=my_conn.execute('''select name from sqlite_master
where type = 'table' ''')
for row in r_set:
print(row)
Output is here ( with sample tables )
('category',)
('subcategory',)
('student',)
Or to get all the records from the student ( sample ) table.
r_set=my_conn.execute('''SELECT * from student''');
for row in r_set:
print(row)
# download my_db.db SQLite Database from plus2net.com
!wget https://www.plus2net.com/python/download/my_db.db
Connect to my_db.db database using sqlite3 and get 5 records from the student table. Set of 5 rows of data is displayed as list.
import sqlite3
my_conn = sqlite3.connect('my_db.db') # connect to db
query="SELECT * FROM student LIMIT 0,5" # sql
my_data=list(my_conn.execute(query)) # rows of data as list
print(my_data)
Displaying Image from SQLite database in Google colab Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.