Advertisement
AyanUpadhaya

Book entry Database project by sqlite3 and Python

Oct 22nd, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. # Book entry database project by sqlite3 and Python
  2. # add book nake, author name, notes about book
  3. # install tabulate module pip install tabulate
  4. # add and view information
  5. # code author Ayan Upadhaya, contact : ayanU881@gmail.com
  6.  
  7.  
  8.  
  9. import sqlite3
  10. from datetime import datetime
  11. from tabulate import tabulate
  12.  
  13. #create database first
  14. DATABASE = "database/books.db"
  15. date = str(datetime.utcnow())
  16. conn = sqlite3.connect(DATABASE)
  17. cur = conn.cursor()
  18. run = True
  19.  
  20. def createTable():
  21.     command = """CREATE TABLE books(book_name TEXT NOT NULL, author_name TEXT NOT NULL, notes TEXT, date TEXT); """
  22.  
  23.     try:
  24.         cur.execute(command)
  25.         conn.commit()
  26.    
  27.     except:
  28.         pass
  29.  
  30. def insertInforMation(bookName,authorName,notes):
  31.     global date
  32.  
  33.     command = """ INSERT INTO books VALUES ('{}','{}','{}','{}');""".format(bookName,authorName,notes,date)
  34.     cur = conn.cursor()
  35.     cur.execute(command)
  36.     conn.commit()
  37.  
  38.     print("Infromation added successfully")
  39.  
  40. def viewInforMation():
  41.     command =""" SELECT *FROM books;"""
  42.     cursor = conn.cursor()
  43.     data = cursor.execute(command)
  44.    
  45.     print(tabulate(data))
  46.    
  47.  
  48. def numberOfEnteries():
  49.     cursor = conn.cursor()
  50.     dataCopy = cursor.execute("select count(*) from books")
  51.     values = dataCopy.fetchone()
  52.     return values[0]
  53.  
  54.  
  55. createTable()
  56.  
  57. print("Welcome To Your Books Database ")
  58. print("--------------------------------- ")
  59.  
  60.  
  61. while run:
  62.     option = input("Press 'a' for adding or 'v' to viewInforMation and 'q' for quit> ")
  63.     if option.lower() == 'a':
  64.         book_name = input("Enter Book name:")
  65.         author_name = input("Enter Author name:")
  66.         notes = input("Enter notes:")
  67.         print()
  68.         insertInforMation(book_name,author_name,notes)
  69.  
  70.     elif option.lower() == 'v':
  71.         number_of_entrys = numberOfEnteries()
  72.         if number_of_entrys>0:
  73.             viewInforMation()
  74.             print("Entrys:",str(number_of_entrys))
  75.         else:
  76.             print("No Entrys To show, please add emtry !")
  77.             continue
  78.     elif option.lower() == 'q':
  79.         run = False
  80.     else:
  81.         print("Wrong input, try agian, later")
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement