import sqlite3
# For connecting to the database. If the database is not in the system, it will create and connect to the database
con = sqlite3.connect("mydatabase1.db")
# If you want to create a table or insert data into the table, declare a cursor
cur = con.cursor()
# By using execute, we can create a table and execute the table
cur.execute('''
CREATE TABLE IF NOT EXISTS students (
name VARCHAR(50),
rollno VARCHAR(50),
section VARCHAR(50)
)
''')
con.commit()
cur.execute('INSERT INTO students VALUES ("sai", "511A", "sectionA")')
cur.execute('INSERT INTO students VALUES ("ramesh", "512A", "sectionB")')
cur.execute('INSERT INTO students VALUES ("raju", "513A", "sectionC")')
con.commit()
con.close()
con = sqlite3.connect("mydatabase1.db")
# If you want to create a table or insert data into the table, declare a cursor
cur = con.cursor()
# By using execute, we can create a table and execute the table
cur.execute('''
CREATE TABLE IF NOT EXISTS students (
name VARCHAR(50),
rollno VARCHAR(50),
section VARCHAR(50)
)
''')
con.commit()
cur.execute('INSERT INTO students VALUES ("sai", "511A", "sectionA")')
cur.execute('INSERT INTO students VALUES ("ramesh", "512A", "sectionB")')
cur.execute('INSERT INTO students VALUES ("raju", "513A", "sectionC")')
con.commit()
con.close()
Output can be seen from below Query
import sqlite3
con=sqlite3.connect("mydatabase.db")
cur=con.cursor()
b=cur.execute('select * from students')
print(b.fetchall())
con.close()
No comments:
Post a Comment