Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
geniuses I am trying to insert all Treeview rows into the sqllite3 database, and instead of that, it only inserts the last row.

What I have tried:

I have tried the following:

Python
def buying_addcheck(self):
    self.conn = sqlite3.connect('car dealership.db')
    self.cursorObj = self.conn.cursor()
    self.checkid += 1
    self.buying_checkspaymenttree.insert("", 'end', values=(
    self.buying_check_date_var.get(), self.buying_check_value_var.get(),
    self.buying_check_num_var.get(),self.checkid))

     
     def checks_db(self):
        self.conn = sqlite3.connect('car dealership.db')
        self.cursorObj = self.conn.cursor()
        # Values variable from Treeview and other widget's
        sellingcarsinfchecks = (
            (
                self.checkid, self.makevar.get(), self.sellernamevar.get(),
                self.Buyingdate_var.get(),
                self.cashepayments.get(),
                self.buying_check_num_var.get(), self.buying_check_value_var.get(), self.buying_check_date_var.get(),
                self.buying_nocheckpic))
        self.cursorObj.execute(
            """INSERT INTO cars_buying_checksonly(checkid, carmake, sellername, buyingdate, entirepaymentmethod, checknum, checkvalue, checkdate, checkpic)VALUES(?,?,?,?,?,?,?,?,?)""",
            sellingcarsinfchecks)
        self.conn.commit()
Posted
Updated 18-Feb-22 6:12am
v2
Comments
Richard MacCutchan 14-Feb-22 3:59am    
Where is the code that calls the checks_db function?
asaad kittaneh 14-Feb-22 21:26pm    
@Richard MacCutchan, thanks for your reply it's in another button, my goal is to insert items into Treeview using a button and save them into Database with another

1 solution

The reason you see only the last entry saved is because you have only called the last record in the list.

You need to loop through all the list items and add them yo your database as you count through the list using a loop. Your code should look something like this -

import mysql.connector

try:
    connection = mysql.connector.connect(host='localhost',
                                         database='Electronics',
                                         user='pynative',
                                         password='pynative@#29')

    mySql_insert_query = """INSERT INTO Laptop (Id, Name, Price, Purchase_date) 
                           VALUES (%s, %s, %s, %s) """

    records_to_insert = [(4, 'HP Pavilion Power', 1999, '2019-01-11'),
                         (5, 'MSI WS75 9TL-496', 5799, '2019-02-27'),
                         (6, 'Microsoft Surface', 2330, '2019-07-23')]

    cursor = connection.cursor()
    cursor.executemany(mySql_insert_query, records_to_insert)
    connection.commit()
    print(cursor.rowcount, "Record inserted successfully into Laptop table")

except mysql.connector.Error as error:
    print("Failed to insert record into MySQL table {}".format(error))

finally:
    if connection.is_connected():
        cursor.close()
        connection.close()
        print("MySQL connection is closed")
 
Share this answer
 
Comments
asaad kittaneh 25-Feb-22 12:25pm    
@Andre Oosthuizen thank you very much
Andre Oosthuizen 26-Feb-22 2:02am    
Only a pleasure

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900