Python 如何在没有硬代码的情况下将数据插入特定行
本规范旨在实现以下目标: -输入用户名 -检查数据库上是否存在 -如果没有,请创建新用户 -将货币和余额添加到新用户行 问题在于我的insert声明。我不确定在使用定义的变量插入数据时如何使用WHERE条件。欢迎帮助 错误消息:TypeError:函数最多接受2个参数4Python 如何在没有硬代码的情况下将数据插入特定行,python,python-3.x,sqlite,Python,Python 3.x,Sqlite,本规范旨在实现以下目标: -输入用户名 -检查数据库上是否存在 -如果没有,请创建新用户 -将货币和余额添加到新用户行 问题在于我的insert声明。我不确定在使用定义的变量插入数据时如何使用WHERE条件。欢迎帮助 错误消息:TypeError:函数最多接受2个参数4 def add_user():strong text print("enter new username") UserLogin = str(input()) c.execute("SELECT username F
def add_user():strong text
print("enter new username")
UserLogin = str(input())
c.execute("SELECT username FROM FinanceDBTable")
enter code here
for row in c.fetchall():
print()
#Convers tuple to string without spaces
UserNameDB = ''.join(row)
if UserNameDB == UserLogin:
print('This username already exisits. please try again')
add_user()
else:
print("Please enter your balanace\n")
userbalance = input()
print("Please enter currency\n")
userCurrency = input()
c.execute("INSERT INTO FinanceDBTable (balance, currency) VALUES (?,?)",
(userbalance, userCurrency), "WHERE (username) VALUES (?)", (UserLogin))
conn.commit()
一种方法是这样做,也不使用被视为非音速的递归,并且最终也会抛出错误。加上使它有点像蟒蛇。 我不确定你是否可以在INSERT语句中使用WHERE,你可以在UPDATE中使用WHERE,也许你是从哪里得到这个想法的,但是我认为,通过与其他人一起给出用户名值将有助于实现你想要实现的目标
def add_user():
while True:
username = input("Username: ")
c.execute("SELECT username FROM FinanceDBTable WHERE username = ?", (username,))
results = c.fetchall()
if len(results) < 1:
# Add user as there is not user with that username.
user_balance = int(input("Please enter your balance: "))
user_currency = input("Please enter currency: ")
c.execute("INSERT INTO FinanceDBTable(username, balance, currency) VALUES (?,?,?)", (username, user_balance, user_currency))
conn.commit()
break
else:
# User already exists
print('This username already exists, please try again.')
我认为insert语句需要是一个字符串,其中只有一个3元素元组作为唯一的其他参数。