🕊️ This simple program helps to use SQLite as a database and carry out operations such as create table and CRUD values into it.
#use sqlite
import sqlite3
#establish connection
connection = sqlite3.connect('data.db')
cursor = connection.cursor()
#create table
create_table = "CREATE TABLE users (id int, username text, password text)"
cursor.execute(create_table)
#add a record to table
user = (1, 'user1', 'pass1')
insert_query = "INSERT INTO users VALUES (?, ?, ?)"
cursor.execute(insert_query, user)
#add multiple records to table
users = [
(2, 'user2', 'pass2'),
(3, 'user3', 'pass3')
]
cursor.executemany(insert_query, users)
#retrieve all records from a table
select_query = "SELECT * from users"
for row in cursor.execute(select_query):
print(row)
#commit and close connection
#use sqlite
import sqlite3
#establish connection
connection = sqlite3.connect('data.db')
cursor = connection.cursor()
#create table
create_table = "CREATE TABLE users (id int, username text, password text)"
cursor.execute(create_table)
#add a record to table
user = (1, 'user1', 'pass1')
insert_query = "INSERT INTO users VALUES (?, ?, ?)"
cursor.execute(insert_query, user)
#add multiple records to table
users = [
(2, 'user2', 'pass2'),
(3, 'user3', 'pass3')
]
cursor.executemany(insert_query, users)
#retrieve all records from a table
select_query = "SELECT * from users"
for row in cursor.execute(select_query):
print(row)
#commit and close connection
connection.commit()
connection.close()
connection.close()
Comments
Post a Comment