import sqlite3
my_conn = sqlite3.connect('my_db.db')
print("Connected to database successfully")
Output ( A new database is created or a connection is established to the existing database if available )
Connected to database successfully
from sqlalchemy import create_engine
#my_conn = create_engine("sqlite:///my_db.db")
my_conn = create_engine("sqlite:///D:\\testing\\my_db\\my_db.db")
In windows system, the absolute path is used in above code.
try:
my_conn.execute('''
CREATE TABLE IF NOT EXISTS student(id integer primary key,
name text,
class text,
mark integer,
gender text
);''')
my_conn.commit()
print("Student Table created successfully")
except sqlite3.Error as my_error:
print("error: ",my_error)
Output
Student Table created successfully
r_set=my_conn.execute('''select name from sqlite_master
where type = 'table' ''')
for row in r_set:
print(row)
Output ( as we have already created on table in above script, we will get this output )
('student',)
(1, 'John Deo', 'Four', 75, 'female')
(2, 'Max Ruin', 'Three', 85, 'male')
(3, 'Arnold', 'Three', 55, 'male')
--------------
--------------
(34, 'Gain Toe', 'Seven', 69, 'male')
(35, 'Rows Noump', 'Six', 88, 'female')
try:
my_conn.execute('''DROP table student;''')
print("student table deleted")
except sqlite3.Error as my_error:
print("error: ",my_error)
my_conn.commit()
from sqlalchemy import create_engine
#my_conn = create_engine("sqlite:///my_db.db")
my_conn = create_engine("sqlite:///D:\\testing\\my_db\\my_db.db")
my_conn.execute('''
CREATE TABLE IF NOT EXISTS student(id integer primary key,
name text,
class text,
mark integer,
gender text,
photo blob
);''')
my_conn = create_engine("sqlite:///D:\\testing\\sqlite\\my_db.db")
r_set=my_conn.execute("CREATE TABLE IF NOT EXISTS category(cat_id integer primary key,\
category text)")
my_conn.commit()
r_set=my_conn.execute("INSERT INTO `category` (`cat_id`, `category`) VALUES \
(1, 'Fruits'),\
(2, 'Colors'),\
(3, 'Games'),\
(4, 'Vehicles');")
my_conn.commit()
r_set=my_conn.execute("CREATE TABLE IF NOT EXISTS subcategory(cat_id integer,\
subcategory text)")
my_conn.commit()
r_set=my_conn.execute("INSERT INTO `subcategory` (`cat_id`, `subcategory`) VALUES \
(1, 'Mango'),\
(1, 'Banana'),\
(1, 'Orange'),\
(1, 'Apple'),\
(2, 'Red'),\
(2, 'Blue'),\
(2, 'Green'),\
(2, 'Yellow'),\
(3, 'Cricket'),\
(3, 'Football'),\
(3, 'Baseball'),\
(3, 'Tennis'),\
(4, 'Cars'),\
(4, 'Trucks'),\
(4, 'Bikes'),\
(4, 'Train')")
my_conn.commit()
21-02-2023 | |
I am new to tkinter and sqlite3. can i post a question regarding my learning project? |