Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Book entry database project by sqlite3 and Python
- # add book nake, author name, notes about book
- # install tabulate module pip install tabulate
- # add and view information
- # code author Ayan Upadhaya, contact : ayanU881@gmail.com
- import sqlite3
- from datetime import datetime
- from tabulate import tabulate
- #create database first
- DATABASE = "database/books.db"
- date = str(datetime.utcnow())
- conn = sqlite3.connect(DATABASE)
- cur = conn.cursor()
- run = True
- def createTable():
- command = """CREATE TABLE books(book_name TEXT NOT NULL, author_name TEXT NOT NULL, notes TEXT, date TEXT); """
- try:
- cur.execute(command)
- conn.commit()
- except:
- pass
- def insertInforMation(bookName,authorName,notes):
- global date
- command = """ INSERT INTO books VALUES ('{}','{}','{}','{}');""".format(bookName,authorName,notes,date)
- cur = conn.cursor()
- cur.execute(command)
- conn.commit()
- print("Infromation added successfully")
- def viewInforMation():
- command =""" SELECT *FROM books;"""
- cursor = conn.cursor()
- data = cursor.execute(command)
- print(tabulate(data))
- def numberOfEnteries():
- cursor = conn.cursor()
- dataCopy = cursor.execute("select count(*) from books")
- values = dataCopy.fetchone()
- return values[0]
- createTable()
- print("Welcome To Your Books Database ")
- print("--------------------------------- ")
- while run:
- option = input("Press 'a' for adding or 'v' to viewInforMation and 'q' for quit> ")
- if option.lower() == 'a':
- book_name = input("Enter Book name:")
- author_name = input("Enter Author name:")
- notes = input("Enter notes:")
- print()
- insertInforMation(book_name,author_name,notes)
- elif option.lower() == 'v':
- number_of_entrys = numberOfEnteries()
- if number_of_entrys>0:
- viewInforMation()
- print("Entrys:",str(number_of_entrys))
- else:
- print("No Entrys To show, please add emtry !")
- continue
- elif option.lower() == 'q':
- run = False
- else:
- print("Wrong input, try agian, later")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement