Python Forum
Create SQLite columns from a list or tuple?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create SQLite columns from a list or tuple?
#7
This is really helpful, thank you.

The code I posted was one iteration of the 500 I've tried!

I think I have the sql code correct now but my problem is that entry_value is created using a for loop so the tuple is only ever one value long. I'll try to work out how build the tuple outside the loop.

Thank you once again!

EDIT:
Done it! Thank you.
I'm sure my code it structured incorrectly but so far it's doing what I wanted. The database stuff is in the method on_save_button. No doubt vendor will cause problems because there aren't enough ?'s.
from tkinter import *
from tkinter.ttk import *
import sqlite3

### Create SQLite database and connect to it ###
conn = sqlite3.connect('Bookkeeping_Database.sqlite3')
cur = conn.cursor()

### Create tkinter window ###
root = Tk()
root.title("Bookkeeping")
root.geometry("800x600") 

### Add a Notebook to window ###
home_screen = Notebook(root)
home_screen.pack(fill="both", expand=1, pady=15)

### Class to add tabs to Notebook with a method to add entry boxes and a save button ###
class Contacts_tab:
    def __init__(self, tab):
        self.tab_name = str(tab)
        self.tab = tab
        self.tab = Frame(home_screen)
        self.tab.pack(fill="both", expand=1)
        home_screen.add(self.tab, text=self.tab_name)
        self.detail_name_dictionary = {}
        self.detail_name_list_keys = []
        
    def create_entry_boxes(self, detail_name):
        self.detail_name = detail_name
        self.entry_box = Entry(self.tab)
        self.entry_box.grid(column=1)
        self.entry_box.insert(0, detail_name)   
        self.detail_name_dictionary[self.detail_name] = self.entry_box
        self.detail_name_list_keys.append(detail_name)      
        
    def save_button(self):
        self.save_button = Button(self.tab, text="Save")
        self.save_button.grid(column=1)    
    
    def on_save_button(self, event):
        inputted_data=[]
        for detail_name in self.detail_name_list_keys:
            entry_value = self.detail_name_dictionary[detail_name].get()
            print(f' {detail_name} = {entry_value}') 
            inputted_data.append(entry_value)
        inputted_data=tuple(inputted_data)
        print(inputted_data)
        print(type(inputted_data))
        print(self.detail_name_list_keys)
        
        cur.execute("CREATE TABLE IF NOT EXISTS " + self.tab_name + " (" + " TEXT, ".join(self.detail_name_list_keys) +" TEXT)")
        cur.execute("INSERT INTO "+ self.tab_name +"(" + ", ".join(self.detail_name_list_keys) + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", inputted_data,)
        conn.commit()

### Create Customers tab ###
def create_customer_tab():
    customer_tab = Contacts_tab("Customers")

    entry_boxes = [ "Customer_ID",
                    "Company",
                    "Name",
                    "Address_1",
                    "Address_2",
                    "Address_3",
                    "Address_4", 
                    "Email", 
                    "Phone_1", 
                    "Phone_2",
                    ]
    for detail_name in entry_boxes:
        customer_tab.create_entry_boxes(detail_name)
        
    customer_tab.save_button()
    customer_tab.save_button.bind('<Button-1>', customer_tab.on_save_button)

def create_vendor_tab():
    vendor_tab = Contacts_tab("Vendors")

    entry_boxes = [ "Vendor_ID",
                    "Company",
                    "Name",
                    "Address_1",
                    "Address_2",
                    "Address_3",
                    "Address_4", 
                    "Email", 
                    "Phone_1", 
                    "Phone_2",
                    "VAT_number"
                    ]
    for detail_name in entry_boxes:
        vendor_tab.create_entry_boxes(detail_name)
        
    vendor_tab.save_button()
    vendor_tab.save_button.bind('<Button-1>', vendor_tab.on_save_button)

#Main
create_customer_tab()
create_vendor_tab()
root.mainloop()
conn.close()
ibreeden likes this post
Reply


Messages In This Thread
RE: Create SQLite columns from a list or tuple? - by snakes - May-04-2021, 12:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  using > < for tuple , list,... akbarza 3 568 Feb-05-2024, 01:18 PM
Last Post: deanhystad
  Create Choices from .ods file columns cspower 3 716 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  How to create a table with different sizes of columns in MS word pepe 8 1,834 Dec-08-2023, 07:31 PM
Last Post: Pedroski55
  Create csv file with 4 columns for process mining thomaskissas33 3 854 Nov-06-2023, 09:36 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 544 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Change font in a list or tuple apffal 4 2,785 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  Delete strings from a list to create a new only number list Dvdscot 8 1,697 May-01-2023, 09:06 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,647 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  search a list or tuple for a specific type ot class Skaperen 8 2,049 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  why is my list a tuple CompleteNewb 7 2,347 Mar-17-2022, 10:09 PM
Last Post: CompleteNewb

Forum Jump:

User Panel Messages

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