python-3.x - 如何修复python sqlite数据库不存储数据
问题描述
我的代码将只存储数据“长号”和按钮单击的第一个“x”值,但是一旦再次单击按钮,它将不会用正确的值替换 x 的当前值,该值作为一个输出成为 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
解决方案
您在函数内部增加 x ,它是不可变的。
考虑下面的代码
def buttonClick(x):
x = x + 1
print "X = " + str(x)
return
x = 0
buttonClick(x)
buttonClick(x)
buttonClick(x)
结果是
X = 1
X = 1
X = 1
def buttonClick(x):
x[0] = x[0] + 1
print "X = " + str(x[0])
return
x = [0]
buttonClick(x)
buttonClick(x)
buttonClick(x)
结果是
X = 1
X = 2
X = 3
推荐阅读
- c++ - 普通变量可以保存 C++ 中其他变量的内存地址吗?
- sql - SQL:如何添加一个虚拟列(每个单元格包含“A”,或者一个整数或其他一些字符串
- curl - 从谷歌开发者那里获取授权令牌
- angular - NgrX 效果重定向角度
- java - Java中的数值集成
- excel - 根据唯一的“ID”列将行从一个工作表复制到另一个工作表?
- javascript - 我如何计算然后在 Web 表单上显示已选中但并非全部选中的特定复选框
- c# - .Net 5+ 创建平台无关的 RSA 密钥对的方法
- javascript - 如何在 React Native 中保持 FastImage 的图像行为?
- xamarin - 如何请求 Xamarin 表单的权限?