Python:IF在SQLITE3

时间:2017-12-04 15:25:42

标签: python database sqlite

当我创建DataBase 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

我插入DATA:

    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", "renato.lenon@Outlook.com", 2005-4-21, "Plugxyvj9");
""")


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"命令块" ..

拜托,谁可以帮助我?

谢谢你的一切......

打印错误:

enter image description here

1 个答案:

答案 0 :(得分: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。您不需要以分号结束语句。 : - )