Python: How To Send Data To SQLite

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
  1. from tkinter import *
  2. import sqlite3
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.
  1. root = Tk()
  2. root.title("Simple Python Program")
  3. width = 400
  4. height = 300
  5. screen_width = root.winfo_screenwidth()
  6. screen_height = root.winfo_screenheight()
  7. x = (screen_width/2) - (width/2)
  8. y = (screen_height/2) - (height/2)
  9. root.geometry('%dx%d+%d+%d' % (width, height, x, y))
  10. root.resizable(0, 0)
  11.  
  12.  
  13. def sendData():
  14. if FIRSTNAME.get() == "" or LASTNAME.get() == "" or AGE.get() == "" or ADDRESS.get() == "":
  15. lbl.configure(text="Please complete the required field", fg="red")
  16. else:
  17. Database()
  18. FIRSTNAME.set("")
  19. LASTNAME.set("")
  20. AGE.set("")
  21. ADDRESS.set("")
  22. lbl.configure(text="Submitted the data")
  23.  
  24.  
  25. def Database():
  26. conn = sqlite3.connect('pythontut.db')
  27. cursor = conn.cursor()
  28. cursor.execute("CREATE TABLE IF NOT EXISTS `member` (mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , firstname TEXT, lastname TEXT, age TEXT, address TEXT)")
  29. cursor.execute("INSERT INTO `member` (firstname, lastname, age, address) VALUES(?, ?, ?, ?)", (str(FIRSTNAME.get()), str(LASTNAME.get()), str(AGE.get()), str(ADDRESS.get())))
  30. conn.commit()
  31. cursor.close()
  32. conn.close()
  33.  
  34. #===============================VARIABLE===================================
  35. FIRSTNAME=StringVar()
  36. LASTNAME=StringVar()
  37. AGE=StringVar()
  38. ADDRESS=StringVar()
  39.  
  40. #===============================FRAME======================================
  41. form = Frame(root, relief=SOLID)
  42. form.pack(pady=25, fill=NONE)
  43.  
  44. #==============================LABEL WIDGET=================================
  45. txt_firstname = Label(form, text="Firstname:", font=('arial', 16))
  46. txt_firstname.grid(row=0, sticky='e', pady=5)
  47. txt_lastname = Label(form, text="Lastname:", font=('arial', 16))
  48. txt_lastname.grid(row=1, sticky ='e')
  49. txt_age = Label(form, text="Age:", font=('arial', 16))
  50. txt_age.grid(row=2, sticky ='e')
  51. txt_address = Label(form, text="Address:", font=('arial', 16))
  52. txt_address.grid(row=3, sticky ='e')
  53. lbl = Label(form, font=("arial", 12))
  54. lbl.grid(row=5, columnspan=2)
  55.  
  56. #==============================ENTRY WIDGET==================================
  57.  
  58. firstname = Entry(form, textvariable=FIRSTNAME, relief=FLAT, bd=5, width=30)
  59. firstname.grid(row=0, column=1)
  60. lastname = Entry(form, textvariable=LASTNAME, relief=FLAT, bd=5, width=30)
  61. lastname.grid(row=1, column=1, pady=10)
  62. age = Entry(form, textvariable=AGE, relief=FLAT, bd=5, width=30)
  63. age.grid(row=2, column=1, pady=10)
  64. address = Entry(form, textvariable=ADDRESS, relief=FLAT, bd=5, width=30)
  65. address.grid(row=3, column=1, pady=10)
  66.  
  67. #==============================BUTTON WIDGERT================================
  68. submit = Button(form, text = "Submit", width=35, command=sendData)
  69. submit.grid(row=4, columnspan=2, pady=10)
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.
  1. #============================INITIALIZATION==================================
  2.  
  3. if __name__== "__main__":
  4. root.mainloop()
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!!

Tags

Comments

Submitted bymihratu temesgen (not verified)on Mon, 04/17/2017 - 17:49

I'm gland to thanks you, but I need a hand on login code by c# or php

Add new comment