import sqlite3
# FOR CREATING RECORDS FUNCTION DEFINITION
def create():
try:
con = sqlite3.connect("data.db")
cursor = con.cursor()
while (True):
name = input("Enter Name: ")
age = int(input("Enter Age: "))
gender = input("Enter Gender: ")
salary = int(input("Enter Salary: "))
data = (name, age, gender, salary,)
query = "INSERT into USERS (name, age, gender, salary) VALUES (?, ?, ?,?)"
cursor.execute(query, data)
con.commit()
ch = input("Do You want to Add More Records(Y/N): ")
if ch == "N" or ch == "n":
cursor.close()
break
else:
pass
except:
print("Error in Record Creation")
# FOR READING ONE RECORD FUNCTION DEFINITION
def read_one():
con = sqlite3.connect("data.db")
cursor = con.cursor()
ids = int(input("Enter Your ID: "))
query = "SELECT * from USERS WHERE id = ?"
result = cursor.execute(query, (ids,))
if (result):
for i in result:
print(f"Name is: {i[1]}")
print(f"Age is: {i[2]}")
print(f"Salary is: {i[4]}")
else:
print("Roll Number Does not Exist")
cursor.close()
# FOR READING ALL RECORDS FUNCTION DEFINITION
def read_all():
con = sqlite3.connect("data.db")
cursor = con.cursor()
query = "SELECT * from USERS"
result = cursor.execute(query)
if (result):
print("\n<===Available Records===>")
for i in result:
print(f"Name is : {i[1]}")
print(f"Age is : {i[2]}")
print(f"Salary is : {i[4]}\n")
else:
pass
# FOR UPDATING RECORDS FUNCTION DEFINITION
def update():
con = sqlite3.connect("data.db")
cursor = con.cursor()
idd = int(input("Enter ID: "))
name = input("Enter Name: ")
age = int(input("Enter Age: "))
gender = input("Enter Gender: ")
salary = int(input("Enter Salary: "))
data = (name, age, gender, salary, idd,)
query = "UPDATE USERS set name = ?, age = ?, gender = ?, salary = ? WHERE id = ?"
result = cursor.execute(query, data)
con.commit()
cursor.close()
if (result):
print("Records Updated")
else:
print("Something Error in Updation")
# FOR DELETING RECORDS FUNCTION DEFINITION
def delete():
con = sqlite3.connect("data.db")
cursor = con.cursor()
idd = int(input("Enter ID: "))
query = "DELETE from USERS where ID = ?"
result = cursor.execute(query, (idd,))
con.commit()
cursor.close()
if (result):
print("One record Deleted")
else:
print("Something Error in Deletion")
# MAIN BLOCK
try:
while (True):
print("1). Create Records: ")
print("2). Read Records: ")
print("3). Update Records: ")
print("4). Delete Records: ")
print("5). Exit")
ch = int(input("Enter Your Choice: "))
if ch == 1:
create()
elif ch == 2:
print("1). Read Single Record")
print("2). Read All Records")
choice = int(input("Enter Your Choice: "))
if choice == 1:
read_one()
elif choice == 2:
read_all()
else:
print("Wrong Choice Entered")
elif ch == 3:
update()
elif ch == 4:
delete()
elif ch == 5:
break
else:
print("Enter Correct Choice")
except:
print("Database Error")
#END
=============================================
import sqlite3 # Database Connectivity try: con = sqlite3.connect("data.db") cursor = con.cursor() print("Connected to Database Successfully") #Data Updating Process-(Single Row & Column) query = "Update USERS set age = 26 where id = 3" cursor.execute(query) con.commit() cursor.close() except: print("Database Error")
import sqlite3 # Database Connectivity def updation(names,idd): try: con = sqlite3.connect("data.db") cursor = con.cursor() print("Connected to Database Successfully") #Data Updating Process-(Single Row & Column) query = "Update USERS set name = ? where id = ?" cursor.execute(query,(names,idd,)) con.commit() cursor.close() except: print("Database Error") updation("Newton",3)
import sqlite3 # Database Connectivity def updation(data): try: con = sqlite3.connect("data.db") cursor = con.cursor() print("Connected to Database Successfully") #Data Updating Process-(multiple-rows) query = "Update USERS set name = ? where id = ?" cursor.executemany(query,data) con.commit() cursor.close() except: print("Database Error") data = [("Newton",3), ("Ricky",4)] updation(data)import sqlite3 # Database Connectivity def updation(data): try: con = sqlite3.connect("data.db") cursor = con.cursor() print("Connected to Database Successfully") #Data Updating Process-(multiple-rows) query = "Update USERS set name = ? where id = ?" cursor.executemany(query,data) con.commit() cursor.close() except: print("Database Error") data = [("Newton",3), ("Ricky",4)] updation(data)import sqlite3 # Database Connectivity try: con = sqlite3.connect("data.db") cursor = con.cursor() print("Connected to Database Successfully") #Data Updating Process-(multiple-columns) query = "Update USERS set name = ?, age = ? where id = ?" cursor.execute(query,("Jordan",28,4,)) con.commit() cursor.close() except: print("Database Error")import sqlite3 # Database Connectivity try: con = sqlite3.connect("data.db") cursor = con.cursor() print("Connected to Database Successfully") #Data fetching Process-(One Record) query = "SELECT * from USERS" x = cursor.execute(query).fetchone() print(x) #format print(f" ID is {x[0]} Age is {x[1]} & Name is {x[2]}") except: print("Database Error")
#Importing Database Library import sqlite3 # Database Connectivity try: con = sqlite3.connect("data.db") cursor = con.cursor() print("Connected to Database Successfully") #Data fetching Process-(Many Record) query = "SELECT * from USERS" x = cursor.execute(query).fetchmany(2) for i in x: print(f" ID is {i[0]} Age is {i[1]} & Name is {i[2]}\n") except: print("Database Error") #END
import sqlite3 # Database Connectivity try: con = sqlite3.connect("data.db") cursor = con.cursor() print("Connected to Database Successfully") #Data fetching Process-(ALL Records) query = "SELECT * from USERS" x = cursor.execute(query).fetchall() for i in x: print(f" ID is {i[0]} Age is {i[1]} & Name is {i[2]}\n") except: print("Database Error")
import sqlite3
# Database Connectivity
try:
con = sqlite3.connect("data.db")
cursor = con.cursor()
print("Connected to Database Successfully")
#Data Insertion Process
name = input("Enter Name: ")
age = int(input("Enter Age: "))
gender = input("Enter your Gender: ")
query = "INSERT into USERS(name, age, gender) VALUES (?,?,?)"
data = (name, age, gender,)
cursor.execute(query, data)
con.commit()
if(cursor.execute(query,data)):
print("Data Inserted Successfully")
else:
print("Data not Inserted")
cursor.close()
except:
print("Database Error")
import sqlite3
# Database Connectivity
try:
con = sqlite3.connect("data.db")
cursor = con.cursor()
print("Connected to Database Successfully")
#Data inserting Process-(Multiple Records)
while(True):
name = input("Enter your Name: ")
age = int(input("Enter your Age: "))
gender = input("Enter your gender: ")
data = (name, age, gender,)
query = "INSERT into USERS(name,age,gender) VALUES (?,?,?)"
cursor.execute(query,data)
con.commit()
ch = input("Do you want to Enter more Records(Y/N): ")
if ch == "n" or ch == "N":
break
else:
pass
cursor.close()
except:
print("Database Error")
import sqlite3
# Database Connectivity
def variables(name,age,gender):
try:
con = sqlite3.connect("data.db")
cursor = con.cursor()
print("Connected to Database Successfully")
#Data inserting Process-(Parameterized Query)
data = (name, age, gender,)
query = "INSERT into USERS(name,age,gender) VALUES (?,?,?)"
cursor.execute(query,data)
con.commit()
cursor.close()
except:
print("Database Error")
variables("Amanda",22,"F")

Tidak ada komentar:
Posting Komentar