Python Forum

Full Version: Connection DATABASE to Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.
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
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.