sqlite3.OperationalError: table users has no column named username
sqlite3.OperationalError: table users has no column named username
您好,我不知道该问题的解决方案。我在 google 上搜索但没有得到任何答案。
我是数据库的新手。所以这可能是个愚蠢的问题 :)。
class Users:
def __init__(self, tablename="users", userId="userId", password="password", username="username"):
self.__tablename = tablename
self.__userId = userId
self.__password = password
self.__username = username
conn = sqlite3.connect('test.db')
print("open database successfully")
str = "CREATE TABLE IF NOT EXISTS " + tablename + "(" + self.__userId + " " + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
str += " " + self.__password + "TEXT NOT NULL ,"
str += " " + self.__username + "TEXT NOT NULL )"
conn.execute(str)
print("Table created successfully")
conn.commit()
conn.close()
def insert_user(self, username, password):
conn = sqlite3.connect('test.db')
str_insert = "INSERT INTO " + self.__tablename + " (" + self.__username +"," + self.__password + ") VALUES (" + "'" +username + "'" + "," + "'" +password +"'" +");"
print(str_insert)
conn.execute(str_insert)
conn.commit()
conn.close()
print("Record created successfully")
u = Users()
u.insert_user("yonatan@gmail.com", "123456")
问题在这里:
str = "CREATE TABLE IF NOT EXISTS " + tablename + "(" + self.__userId + " " + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
str += " " + self.__password + "TEXT NOT NULL ,"
str += " " + self.__username + "TEXT NOT NULL )"
实际上,连接 "username"
和 "TEXT NOT NULL"
将得到 "usernameTEXT NOT NULL"
,而 "username"
和 "TEXT"
之间没有 space
如果您执行以下查询,您可以看到这一点:
conn.execute("SELECT * from sqlite_master").fetchall()
这给你:
[
('table', 'users', 'users', 2, 'CREATE TABLE users(userId INTEGER PRIMARY KEY AUTOINCREMENT , passwordTEXT NOT NULL , usernameTEXT NOT NULL )'),
('table', 'sqlite_sequence', 'sqlite_sequence', 3, 'CREATE TABLE sqlite_sequence(name,seq)')
]
这显然是一团糟。
当您使用 sqlite3 时,一个好的做法是使用以下语法,这将避免您犯很多 space 错误:
conn.execute(
f"CREATE TABLE IF NOT EXISTS {tablename}"
f"({self.__userId} INTEGER PRIMARY KEY AUTOINCREMENT,"
f"{self.__password} TEXT NOT NULL, {self.__username} TEXT NOT NULL)"
)
您好,我不知道该问题的解决方案。我在 google 上搜索但没有得到任何答案。 我是数据库的新手。所以这可能是个愚蠢的问题 :)。
class Users:
def __init__(self, tablename="users", userId="userId", password="password", username="username"):
self.__tablename = tablename
self.__userId = userId
self.__password = password
self.__username = username
conn = sqlite3.connect('test.db')
print("open database successfully")
str = "CREATE TABLE IF NOT EXISTS " + tablename + "(" + self.__userId + " " + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
str += " " + self.__password + "TEXT NOT NULL ,"
str += " " + self.__username + "TEXT NOT NULL )"
conn.execute(str)
print("Table created successfully")
conn.commit()
conn.close()
def insert_user(self, username, password):
conn = sqlite3.connect('test.db')
str_insert = "INSERT INTO " + self.__tablename + " (" + self.__username +"," + self.__password + ") VALUES (" + "'" +username + "'" + "," + "'" +password +"'" +");"
print(str_insert)
conn.execute(str_insert)
conn.commit()
conn.close()
print("Record created successfully")
u = Users()
u.insert_user("yonatan@gmail.com", "123456")
问题在这里:
str = "CREATE TABLE IF NOT EXISTS " + tablename + "(" + self.__userId + " " + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
str += " " + self.__password + "TEXT NOT NULL ,"
str += " " + self.__username + "TEXT NOT NULL )"
实际上,连接 "username"
和 "TEXT NOT NULL"
将得到 "usernameTEXT NOT NULL"
,而 "username"
和 "TEXT"
如果您执行以下查询,您可以看到这一点:
conn.execute("SELECT * from sqlite_master").fetchall()
这给你:
[
('table', 'users', 'users', 2, 'CREATE TABLE users(userId INTEGER PRIMARY KEY AUTOINCREMENT , passwordTEXT NOT NULL , usernameTEXT NOT NULL )'),
('table', 'sqlite_sequence', 'sqlite_sequence', 3, 'CREATE TABLE sqlite_sequence(name,seq)')
]
这显然是一团糟。
当您使用 sqlite3 时,一个好的做法是使用以下语法,这将避免您犯很多 space 错误:
conn.execute(
f"CREATE TABLE IF NOT EXISTS {tablename}"
f"({self.__userId} INTEGER PRIMARY KEY AUTOINCREMENT,"
f"{self.__password} TEXT NOT NULL, {self.__username} TEXT NOT NULL)"
)