python sqlite3 save value out of spinbox into database by pushing button
<p>Trying to save a value from a spinbox into my database. This is a part of my code.</p>

<pre><code>numericupdownLL = tk.Spinbox(self, from_=0, to=300)
numericupdownLL.pack()

def saveDate(title):
        vLL = int(numericupdownLL.get()) ## convert to int because output of spinbox= str and database=int
        c.execute("UPDATE settings SET ll=? WHERE name=?",(vLL, title))
        conn.commit()

buttonSave = tk.Button(self, text='save', command=saveData(something)
buttonSave.pack()
</code></pre>

<p>Now I don't get any error, but the code writes always a zero to my db instead of the value of the spinbox.</p>

<p>Any idea?</p>

<p>I'll correct the typos in the code in order to be consistent in the explanation</p>

<pre><code>numericupdownLL = tk.Spinbox(self, from_=0, to=300)
numericupdownLL.pack()

# Changed saveDate to saveData that it's actually called in the button
def saveData(title):
    # Corrected indentation
    vLL = int(numericupdownLL.get())
    c.execute("UPDATE settings SET ll=? WHERE name=?",(vLL, title))
    conn.commit()

# Added a missing parentheses
buttonSave = tk.Button(self, text='save', command=saveData(something))
buttonSave.pack()
</code></pre>

<p>Two notes:</p>

<ul>
<li>I'll be referencing this code instead of yours mainly because the name of the function called in the button and the name of the declared function does not match.</li>
<li>As far as I don't know how looks like your I will assume that the rest of the code works.</li>
</ul>

<p>So let's start:</p>

<p>The first thing to note is that you're calling the function only once and not each time the button is clicked. That's because instead of passing the function you're passing a None which is the return of that function call. So to fix that you should remove the parentheses and just pass the function as a callback</p>

<pre><code># WARNING! This code does not work! Keep reading!
buttonSave = tk.Button(self, text='save', command=saveData)
</code></pre>

<p><strong>After correcting that each time you click the button an exception will be risen telling you that the function needs 1 argument and 0 is passed</strong>.
This exception is because of the <code>title</code> parameter in the <code>saveData</code> function so if you want something to be passed as a parameter you will need to return a callback that uses that parameter inside but that it does not accept any parameter:</p>

<pre><code>def get_save_data_fn(title):
    def saveData():
        vLL = int(numericupdownLL.get())
        c.execute("UPDATE settings SET ll=? WHERE name=?",(vLL, title))
        conn.commit()
    return saveData  # Returns a function with 0 parameters that uses the given title

# Now you can retrieve the function that takes no arguments and has 'something' used 
# in the sql statement
buttonSave = tk.Button(self, text='save', command=get_save_data_fn(something))
</code></pre>

<p>So the final code should look like:</p>

<pre><code>numericupdownLL = tk.Spinbox(self, from_=0, to=300)
numericupdownLL.pack()

def get_save_data_fn(title):
    def saveData():
        vLL = int(numericupdownLL.get())
        c.execute("UPDATE settings SET ll=? WHERE name=?",(vLL, title))
        conn.commit()
    return saveData  # Returns a function with 0 parameters that uses the given title

# Now you can retrieve the function that takes no arguments and has 'something' used 
# in the sql statement
buttonSave = tk.Button(self, text='save', command=get_save_data_fn(something))
buttonSave.pack()
</code></pre>

<p>Summarizing:</p>

<ul>
<li>The reason the function seems not to be called is because it's called only once when creating the button and it's not called anymore as far as you are not passing that function but None to the command argument.</li>
<li>In order to use that <code>something</code> in the callback you must retrieve a function without parameters and use that variable as a closure.</li>
</ul>

<p>If any part of the code doesn't work for you, let me know!</p>

python sqlite3