sql database
import sqlite3
conn=sqlite3.connect("mydatabase.db")
cursor=conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS students(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER
)
""")
conn.commit()
print("TABLE CREATED!!")
data=[
("Alice",20),
("Bob",22),
("Charlie",19),
("David",21),
("Eve",20)
]
cursor.executemany("insert into students(name,age) values(?,?)",data)
conn.commit()
print("DATA INSERTED!")
cursor.execute("select * from students")
rows=cursor.fetchall()
for r in rows:
print(r)
print("DISPLAYED THE DATA ABOVE!")
cursor.execute("update students set age=? where age=?",(27,21))
conn.commit()
cursor.execute("select * from students")
rows=cursor.fetchall()
for r in rows:
print(r)
print("DATA UPDATED!")
cursor.execute(
"DELETE FROM students WHERE name=?",
("Bob",)
)
conn.commit()
cursor.execute("select * from students")
rows=cursor.fetchall()
for r in rows:
print(r)
print("DATA DELETED!")
conn.close()
Comments
Post a Comment