Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- # query if table exist
- def table_exist(conn, table_name):
- cur = conn.cursor()
- result = list(cur.execute(
- "SELECT name FROM sqlite_master WHERE type='table' AND name='%s';" % table_name))
- return len(result) > 0
- class Customer():
- def __init__(self, name, gender, birthday):
- self.name = name
- self.gender = gender
- self.birthday = birthday
- # Create customer record in DB
- # @return created record unique id
- def insert_customer(conn, customer):
- cur = conn.cursor()
- cur.execute(
- "INSERT INTO customer (name, gender, birthday) VALUES (:name, :gender, :birthday)", customer.__dict__)
- return cur.lastrowid
- def main():
- conn = sqlite3.connect('test.db')
- if not table_exist(conn, "customer"):
- # Create DB table if table not exist
- cur = conn.cursor()
- cur.executescript("""
- CREATE TABLE customer(
- rowid INTEGER PRIMARY KEY AUTOINCREMENT,
- name TEXT NOT NULL,
- gender INTEGER NOT NULL,
- birthday DATE NOT NULL
- );
- """)
- customer = Customer(input("Input Customer Name: "), int(input(
- "Input Customer Gender (1 = male, 2 = female): ")), input("Input Customer Birthday (in YYYY-MM-DD format): "))
- insert_customer(conn, customer)
- conn.commit()
- conn.close()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement