-1

my code will only store the data "trombones" and the first "x" value from the button click, but once the button is clicked again it will not replace the current value of x which is outputed as one with the correct value which is supposed to be 2

import sqlite3
import tkinter as tk
from tkinter import *

connecter = sqlite3.connect('IAworker.db')
c = connecter.cursor()

def create_security():
    c.execute('CREATE TABLE IF NOT EXISTS security(username TEXT, password INT, teacherID INT, studentID INT, instrumentID INT)')
def create_Teacher_Info():
    c.execute('CREATE TABLE IF NOT EXISTS teacher_info(teacherName TEXT, teacherID INT PRIMARY KEY)')

def create_Student_Info():
    c.execute(
        'CREATE TABLE IF NOT EXISTS student_info(Form TEXT, studentID INT PRIMARY KEY, studentName TEXT)')

def create_Instrument_Info():
    c.execute('CREATE TABLE IF NOT EXISTS instrument_info( instrumentName TEXT, instrumentID INT PRIMARY KEY)')
    c.close
create_security()
create_Teacher_Info()
create_Student_Info()
create_Instrument_Info()

def inserterInstTest(name, instID):
     c = connecter.cursor()
     c.execute('SELECT * FROM instrument_info')
     [print(row) for row in c.fetchall()]
     c.execute('INSERT OR REPLACE INTO instrument_info (instrumentName, instrumentID) VALUES (?, ?)', (name, instID))
     # c.execute('UPDATE instrument_info SET instrumentName = (?) WHERE instrumentID = (?)', (name, instID))

     connecter.commit()
     c.close()
     connecter.close


butt4 = tk.Button(root3_2, text="trombone+1",)#instrument buttons
                butt4.grid(row=6, column=1, columnspan=3)#placement on window for buttons



def buttonClick3(x): #commands for buttons
                    print('button was clicked')
                    tromName = 'trombone'
                    x = x + 1
                    x = str(x)
                    print('days read =',x)
                    SqliteIA.inserterInstTest(tromName,x)
                butt4.config(command=buttonClick3(trombones2))#configuring button commands


SQLiteIA.InserterInstTest(tromName, x) was used to call the function as its in another py file. but the code should replace the current 
jim leahy
  • 35
  • 2
  • Have you checked that your code properly increments `x`? What is the output of your code in the `days read =` lines? I would assume that your code never reconfigures the `buttonClick3` command properly to accept a value. – Corion Jan 15 '19 at 15:57

1 Answers1

0

You are incrementing x inside of a function, where it is immutable.

Consider the code below

def buttonClick(x):
    x = x + 1
    print "X = " + str(x)
    return

x = 0

buttonClick(x)
buttonClick(x)
buttonClick(x)

Results in

X = 1
X = 1
X = 1

See this answer for an explanation of immutable vs mutable

Here is a way around that

def buttonClick(x):
    x[0] = x[0] + 1
    print "X = " + str(x[0])
    return

x = [0]

buttonClick(x)
buttonClick(x)
buttonClick(x)

Results in

X = 1
X = 2
X = 3
Kenneth
  • 535
  • 2
  • 17