如何解决这个 SQL Python 问题并以正确的方式获取它?

问题描述 投票:0回答:1

当我创建数据库 CURRENT_users.db 时:

    import sqlite3

conn = sqlite3.connect('CURRENT_users.db')

cursor = conn.cursor()

cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
          id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
          nome TEXT NOT NULL,
          email TEXT NOT NULL,
          created_in DATE NOT NULL,
          password TEXT NOT NULL

)
""")

print("Success! DATABASE created with success!")

conn.close()

import UserLoginUI_Part2_Tes

t1

我插入数据:

    import sqlite3

conn = sqlite3.connect("CURRENT_users.db")
cursor = conn.cursor()


cursor.execute("""
INSERT INTO users (id, nome, email, created_in, password)
VALUES (001, "Renatinho", "[email protected]", 2055-4-21, "passtest");
""")


conn.commit()

print("A new user has been incremented! Now,have fun!!!")

conn.close()

import UserInterface

在“UserInterface”中,我输入“Renatinho”(这是我的NOME数据),似乎“IF”不起作用!!

    import sqlite3

conn = sqlite3.connect("CURRENT_users.db")

cursor = conn.cursor()

user_INFO = cursor.execute(""" SELECT nome FROM users; """)

user_in_SCRIPT = str(input("Your credentials: USERNAME: \n>>>"))

logged_in = False;

if user_in_SCRIPT == user_INFO:
      print("You are logged in! Enjoy your new account...")
      logged_in = True;

else:
      print("Error: Not a valid user or USERNAME!!")
      conn.close()

它曾经向我展示过 ELSE“命令块”..

请问谁可以帮助我?

谢谢你所做的一切...

python database sqlite
1个回答
0
投票

您已调用 SQL SELECT 但需要获取数据。

cursor.execute("SELECT nome FROM users")
user_INFO = cursor.fetchone()

这将返回一个元组,因此要获取其中的字符串,请采用零索引:

if user_in_SCRIPT == user_INFO[0]:
    print("You are logged in! Enjoy your new account...")
    logged_in = True

顺便说一句,你使用的是 Python,而不是 JavaScript。您不需要以分号结束语句。 :-)

© www.soinside.com 2019 - 2024. All rights reserved.