Skip to main content

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

Installing SQLite Browser
After you installed Python, we will now then install the SQLite, here's the link for the DB Browser for SQLite

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)
  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")
  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()
  34. #===============================VARIABLE===================================
  35. FIRSTNAME=StringVar()
  36. LASTNAME=StringVar()
  37. AGE=StringVar()
  38. ADDRESS=StringVar()
  40. #===============================FRAME======================================
  41. form = Frame(root, relief=SOLID)
  42. form.pack(pady=25, fill=NONE)
  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)
  56. #==============================ENTRY WIDGET==================================
  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)
  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==================================
  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!!


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

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

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.