You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importsqlite3# Open SQLIte database named mydb.dbconn=sqlite3.connect("mydb.db")
# Create a cursor for executing queriescurr=conn.cursor()
# Code Start# Code Endconn.close()
Select Query
# run queriesres=curr.execute("SELECT * FROM users")
# Fetch single rowdata=res.fetchone()
# Fetch 5 rowsdata=res.fetchmany(5)
# Fetch all rowsdata=res.fetchall()
# loop through all rows without fetchforrowinres:
print(row) # row will have one row in each iteration
Note: For any Queries which will result in change in database in anyway will not be saved unless committed.
Insert Query
res=curr.execute("INSERT INTO users (username,password) VALUES ('admin','admin')")
# Commit the resultsconn.commit()
Update Query
res=curr.execute("UPDATE users SET (password = 'password') WHERE (username='admin')")
# Commit the resultsconn.commit()
Delete Query
res=curr.execute("DELETE FROM users WHERE username='admin'")
# Commit the resultsconn.commit()