Python Forum
Trying to write an app to connect to a mysql DB
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to write an app to connect to a mysql DB
#1
Hi all, I just began my journey with python, and I'm trying to make an app to connect and manage some data.

I have several questions though.

1. All works fine if I'm using sqlite, and just run the code in my environment. But when I tried to compile the code into a single exe file using pyinstaller, the program didnt start, swith this error: sqlite3.OperationalError: no such table: alpine_source.

when I was compiling, the db file was in the same folder as the py file. Could someone pelase tell me why it oesnt work, and is it actually possible to make an exe file that will contating a DB within, with an uption to edit this data?


2. I tried using then mysql instead of sqlite, the server side works, but now I think there is some problem with the code itsealf, I get some errors and I don't konw how to fix them.
Here I get:
Error:
TypeError: fetchall() takes 1 positional argument but 3 were given
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk

import sqlite3 

import tkinter as Tkinter
import tkinter as tk
import tkinter.ttk as ttk

import mysql.connector
from mysql.connector import Error

tree_columns = ('zero','one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', )

class Product:
    
    db_name = "Alpine_final_data2.db"

    
    
    def __init__(self, wind):
        self.wind = wind
        self.wind.title("Alpine Cabin Products")
        
        style = ttk.Style()
        style.configure("mystyle.Treeview.Heading", font=('Calibri', 11,'bold')) # Modify the font of the headings

        frame = LabelFrame(self.wind, text="Add New Record")
        frame.grid(row=0,column=0, pady=10)
        

        
        self.tree = ttk.Treeview(columns=tree_columns, show="headings", height=20, style="mystyle.Treeview")

        #self.tree.heading('#0', text='ID')
        self.tree.heading('#1', text='Product ID')#New Entry
        self.tree.heading('#2', text='Brand Name')
        self.tree.heading('#3', text='Product Name')
        self.tree.heading('#4', text='SKU ID')
        self.tree.heading('#5', text='EAN Code')
        self.tree.heading('#6', text='HSC Code')
        self.tree.heading('#7', text='CU/TU')#Previous name: "CU per TU"
        self.tree.heading('#8', text='Net weight/CU')#New Entry
        self.tree.heading('#9', text='Gross weight/CU')#New Entry
        self.tree.heading('#10', text='Gross weight/TU')#Previous name: "Weight per TU"
        self.tree.heading('#11', text='Cost/CU(CHF)')#Previous name: "Price per CU"
        self.tree.heading('#12', text='Shelf Life')#Previous name: "Shelf life"
        self.tree.heading('#13', text='Chinese Name')
        self.tree.column('#1', stretch=Tkinter.YES, width=70)
        self.tree.column('#2', stretch=Tkinter.YES, width=75)
        self.tree.column('#3', stretch=Tkinter.YES, width=120)
        self.tree.column('#4', stretch=Tkinter.YES, width=100)
        self.tree.column('#5', stretch=Tkinter.YES, width=130)
        self.tree.column('#6', stretch=Tkinter.YES, width=100)
        self.tree.column('#7', stretch=Tkinter.YES, width=50)
        self.tree.column('#8', stretch=Tkinter.YES, width=100)
        self.tree.column('#9', stretch=Tkinter.YES, width=110)
        self.tree.column('#10', stretch=Tkinter.YES, width=110)
        self.tree.column('#11', stretch=Tkinter.YES, width=95)
        self.tree.column('#12', stretch=Tkinter.YES, width=75)
        self.tree.column('#13', stretch=Tkinter.YES, width=90)
        #self.tree.column('#0', stretch=Tkinter.YES, width=80)
        self.tree.grid(row=20, columnspan=14, rowspan=10, sticky=W+E+N+S)
        self.treeview = self.tree
        #self.treeview.grid_columnconfigure(20, weight=20)
        # Initialize the counter
        self.i = 0       
        


        self.viewing_records()
    
    #SQLite ==> WORKS
    def run_query2(self, query, parameters =()):
        with sqlite3.connect(self.db_name) as conn:
            cursor = conn.cursor()
            query_result = cursor.execute(query, parameters)
            conn.commit()
        return query_result
    
    #mySQL (doesnt work)
    def run_query(self, query, parameters =()):
        try:
            cnx = mysql.connector.connect(host='localhost',
                                 database='AC',
                                 user='Test',
                                 password='12345678')
            if cnx.is_connected():
                cursor = cnx.cursor()
                query_result = cursor.fetchall(query, parameters)
        finally:
            #closing database connection.
            if(cnx.is_connected()):
                cnx.close()
        return query_result  
    
    def viewing_records(self):
        records = self.tree.get_children()
        for element in records:
            self.tree.delete(element)
        query = "SELECT * FROM alpine_source"
        db_rows = self.run_query(query)          
        for item in db_rows:
            self.tree.insert('', 'end', values=item)
        
          
    def validation(self):
        return len(self.brand_name.get()) !=0 and len(self.product_name.get())!=0

        
        
if __name__ =="__main__":
    wind = Tk()
    application = Product(wind)
    wind.mainloop()
but if I change
query_result = cursor.execute(query, parameters)
to
query_result = cursor.execute(query, parameters)
then I get this error:
Error:
TypeError: 'NoneType' object is not iterable
Dunno what to do, looked everywhere, but I guess I'm mussing some key knowledge here
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql and mysql.connector error lostintime 2 664 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,993 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  Program stuck at mysql.connector.connect zazas321 1 2,076 Jul-29-2021, 10:49 AM
Last Post: zazas321
  how to connect mysql from txt 1 line goes good but not all lines in text kingceasarr 4 2,852 Mar-24-2021, 05:45 AM
Last Post: buran
  How to connect Mysql databse with python and VSCode IDE madhusoodhananER 1 8,710 Oct-31-2019, 10:15 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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