Python: How To Send Data To SQLite
Submitted by razormist on Monday, April 17, 2017 - 17:05.
In this tutorial we will try to send a data from Python to SQLite database. Python is a widely used advance-level programming language for a general technique to the developer. Beginners find Python a clean syntax and indentation structure based, and it is easy to learn because of its less semi colon problem. So let do the coding.
Getting started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Installing SQLite Browser
After you installed Python, we will now then install the SQLite, here's the link for the DB Browser for SQLite http://sqlitebrowser.org/
Importing Modules
After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python.
Then copy code that I provided below and paste it inside the IDLE text editor
Creating the Main Function
This is the main code for the Python application.The window will automatically generate when the program run. To do that just copy the code below then paste it inside the IDLE text editor.
Initializing the Application
This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.
There you have it, we enable to send the data from Python to SQLite. I hope the this simple tutorial help you to your on working projects. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
- from tkinter import *
- import sqlite3
- root = Tk()
- root.title("Simple Python Program")
- width = 400
- height = 300
- screen_width = root.winfo_screenwidth()
- screen_height = root.winfo_screenheight()
- x = (screen_width/2) - (width/2)
- y = (screen_height/2) - (height/2)
- root.geometry('%dx%d+%d+%d' % (width, height, x, y))
- root.resizable(0, 0)
- def sendData():
- if FIRSTNAME.get() == "" or LASTNAME.get() == "" or AGE.get() == "" or ADDRESS.get() == "":
- lbl.configure(text="Please complete the required field", fg="red")
- else:
- Database()
- FIRSTNAME.set("")
- LASTNAME.set("")
- AGE.set("")
- ADDRESS.set("")
- lbl.configure(text="Submitted the data")
- def Database():
- conn = sqlite3.connect('pythontut.db')
- cursor = conn.cursor()
- cursor.execute("CREATE TABLE IF NOT EXISTS `member` (mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , firstname TEXT, lastname TEXT, age TEXT, address TEXT)")
- cursor.execute("INSERT INTO `member` (firstname, lastname, age, address) VALUES(?, ?, ?, ?)", (str(FIRSTNAME.get()), str(LASTNAME.get()), str(AGE.get()), str(ADDRESS.get())))
- conn.commit()
- cursor.close()
- conn.close()
- #===============================VARIABLE===================================
- FIRSTNAME=StringVar()
- LASTNAME=StringVar()
- AGE=StringVar()
- ADDRESS=StringVar()
- #===============================FRAME======================================
- form = Frame(root, relief=SOLID)
- form.pack(pady=25, fill=NONE)
- #==============================LABEL WIDGET=================================
- txt_firstname = Label(form, text="Firstname:", font=('arial', 16))
- txt_firstname.grid(row=0, sticky='e', pady=5)
- txt_lastname = Label(form, text="Lastname:", font=('arial', 16))
- txt_lastname.grid(row=1, sticky ='e')
- txt_age = Label(form, text="Age:", font=('arial', 16))
- txt_age.grid(row=2, sticky ='e')
- txt_address = Label(form, text="Address:", font=('arial', 16))
- txt_address.grid(row=3, sticky ='e')
- lbl = Label(form, font=("arial", 12))
- lbl.grid(row=5, columnspan=2)
- #==============================ENTRY WIDGET==================================
- firstname = Entry(form, textvariable=FIRSTNAME, relief=FLAT, bd=5, width=30)
- firstname.grid(row=0, column=1)
- lastname = Entry(form, textvariable=LASTNAME, relief=FLAT, bd=5, width=30)
- lastname.grid(row=1, column=1, pady=10)
- age = Entry(form, textvariable=AGE, relief=FLAT, bd=5, width=30)
- age.grid(row=2, column=1, pady=10)
- address = Entry(form, textvariable=ADDRESS, relief=FLAT, bd=5, width=30)
- address.grid(row=3, column=1, pady=10)
- #==============================BUTTON WIDGERT================================
- submit = Button(form, text = "Submit", width=35, command=sendData)
- submit.grid(row=4, columnspan=2, pady=10)
- #============================INITIALIZATION==================================
- if __name__== "__main__":
- root.mainloop()
Comments
Add new comment
- Add new comment
- 141 views