Python Forum
Connection DATABASE to Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Connection DATABASE to Python
#1
Hey so i have a database that i need to connect to my search box. Database is made with SQLite3.

from tkinter import *

root = Tk ()

topframe = Frame(root)
entry = Entry(topframe)
entry.pack()

Tools = ['Powerdrill', 'Chainsaw', 'Oscillating Multifunction Tool', 'Jigsaw', 'Angle Grinder']
def search():
name_of_tool = entry.get()
entry_value = entry.get()
answer_value = entry




print

button = Button(topframe, text="search")
button.pack()



topframe.pack(side = TOP)



bottomframe = Frame(root)

scroll = Scrollbar(bottomframe)
scroll.pack(side=RIGHT, fill=Y)
answer = Text(bottomframe, width=30, height=10, yscrollcommand = scroll.set)
answer.pack()
scroll.config(command=answer.yview)
bottomframe.pack()


root.mainloop()

This is the code for the searchbox that i need to be connected to database
Reply
#2
1. You'll need to import sqlite3
2. Connect to the database.
3. Create the database tables.
4. Once you have the DB you will setup your calls to the DB in your code for the item you are querying.

Below is a general examples of code essentials you will need to incorporate into your existing code.

Example of a database creation and setup.
#============================= CONNECT TO DATABASE =============================
    conn = sqlite3.connect(YourDatabaseName.db')
    c = conn.cursor()
#========================= CREATE DATABASE WITH TABLES =========================
    c.execute('''CREATE TABLE IF NOT EXISTS Tools(
    PowerDrill CHAR(30),
    ChainSaw CHAR(30),
    Oscillating Multifunction Tool CHAR(30),
    Jigsaw CHAR(30),
    Angle Grinder CHAR(30));''')
    conn.commit()
You may want to write some code with an entry widget to allow the user to enter the name of data you want to save in each table that you will query in future use.
example code:
#================================= SAVE Tools =================================
            def Save():
                try:
                    saveInput = YourDatabaseName.get()
                    c.execute('SELECT YourDatabaseName FROM Tools WHERE YourDatabaseName = :saveInput',
                    {'saveInput': saveInput})
                    conn.commit()
                    saveResult = c.fetchone()
                    if saveResult == None:
                        c.execute('''INSERT INTO YourDatabaseName VALUES (?,?,?,?,?)''',
                        (PowerDrill.get(), (ChainSaw.get()),
                        (Oscillating Multifunction Tool.get()), (Jigsaw.get()), (Angle Grinder.get(1.0, END))))
                        conn.commit()
                        messagebox.showinfo('Tools','Entry saved successfully.')
                        Entry_Widget_Name.delete(0, END)
                    else:
                        messagebox.showinfo('Tools',
                        'That entry name already exists in the database.')
                except:
                    messagebox.showerror('Tools','Database Error.')
Setup your Variables for your database entry and query.
Example:
#============================== GET TOOLS QUERY ===============================
        def getTools(event):
            PowerDrill = StringVar()
            ChainSaw = StringVar()
            Oscillating Multifunction Tool = StringVar()
            Jigsaw= StringVar()
            Angle Grinder=StringVar()
Query your database and then insert the data to your widget.
Example:
#================================ SELECT QUERY =================================
        def select():
            try:
                output.delete(0, END)
                c.execute('SELECT YourDatabaseName FROM Tools = :selection ORDER BY Recipe DESC')
                results = c.fetchall()
                for YourDatabaseName, in results:
                    output.insert(0,YourDatabaseName)
            except:
                messagebox.showerror(Tools, 'Failed to load Data)
I hope this gives you some insight into the method needed to accomplish what you are trying to do.
"Often stumped... But never defeated."
Reply
#3
Very thankful for your work sir, couldn't be better. One more thing maybe you can tell me why my Pycharm doesn't catch up with SQLlite3? it did work yesterday, now it the "Import sqlite3" part is not highlighted anymore and Pycharm can't connect to it. Sqlite3 was implemented correctly in the PC
Reply
#4
MenThoLLt, I have only use PyCharm a few times myself and found it a bit of a pain to configure. It is a very good software for coding however I prefer to use PyScripter when I am coding. I find it much easier to work with and it has no complications with configurations.
"Often stumped... But never defeated."
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I don't know what is wrong (Python and SQL connection) shereen 2 158 Yesterday, 06:46 PM
Last Post: menator01
  No Internet connection when running a Python script basil_555 8 443 Mar-11-2024, 11:02 AM
Last Post: snippsat
  Connection LTspice-Python with PyLTSpice bartel90 0 281 Feb-05-2024, 11:46 AM
Last Post: bartel90
  Virtual Env changing mysql connection string in python Fredesetes 0 325 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  Database connection problems [login, PyQt5, PySql] gradlon93 5 620 Dec-15-2023, 05:08 PM
Last Post: deanhystad
  connection python and SQL dawid294 4 587 Dec-12-2023, 08:22 AM
Last Post: Pedroski55
  Networking Issues - Python GUI client and server connection always freezes Veritas_Vos_Liberabit24 0 679 Mar-21-2023, 03:18 AM
Last Post: Veritas_Vos_Liberabit24
  pysql connection to cloud server database times out Pedroski55 9 4,592 Oct-11-2021, 10:34 PM
Last Post: Pedroski55
  Python MYSQL connection does not work after 1h idle zazas321 9 6,622 Oct-07-2021, 12:02 PM
Last Post: ndc85430
  Serial connection connection issue Joni_Engr 15 7,836 Aug-30-2021, 04:46 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020