【问题标题】:Python sqlite3 sqlite3.OperationalError: no such column: website even though column existsPython sqlite3 sqlite3.OperationalError:没有这样的列:网站即使存在列
【发布时间】:2021-08-25 22:51:47
【问题描述】:
import sqlite3

database_connection = sqlite3.connect("accounts.db")
database_cursor = database_connection.cursor()


def create_table():
    database_cursor.execute(
        """
        CREATE TABLE IF NOT EXISTS accounts (
        website VARCHAR(50) NOT NULL,
        username VARCHAR(50) NOT NULL,
        email VARCHAR(50) NOT NULL,
        password VARCHAR(50) NOT NULL);
    """
    )


def create_account():
    website = input("\nEnter Website: ")
    username = input("Enter Username: ")
    email = input("Enter Email: ")
    password = input("Enter Password: ")

    database_cursor.execute(
        """
        INSERT INTO accounts ('website', 'username', 'email', 'password')
        VALUES (website, username, email, password)
    """
)


def remove_account():
    pass


def lookup_account():
    pass


def all_accounts():
    database_cursor.execute(
        """
        SELECT * FROM accounts;
    """)

    return database_cursor.fetchall()


create_table()
create_account()
print(all_accounts())

database_connection.commit()
database_connection.close()

代码是杂乱无章的临时代码(抱歉),因为我想让数据库正常工作,但不知道为什么它给了我这个错误。我对 sqlite3 和 sql 很陌生,所以这可能是一个愚蠢和基本的错误,但我无法让它工作。帮助表示赞赏:)

【问题讨论】:

    标签: python sqlite


    【解决方案1】:

    您需要在查询之前连接到数据库。您正在执行此操作并将表名作为您的 db 文件名传递给它。

    def create_connection(db_file):
        """ create a database connection to the SQLite database
            specified by the db_file
        :param db_file: database file
        :return: Connection object or None
        """
        conn = None
        try:
            conn = sqlite3.connect(db_file)
        except Error as e:
            print(e)
    
        return conn
    

    这里是更多信息的链接:https://www.sqlitetutorial.net/sqlite-python/sqlite-python-select/

    【讨论】:

    • 我的数据库名称与表相同,但它具有 .db 文件扩展名,而且它说它没有列网站的事实我猜这意味着它已经连接?在图片中它表示该表位于“accounts.db”数据库中。
    【解决方案2】:

    对于遇到此问题的任何人,我解决了一个解决方案希望它有所帮助:)

    我变了:

    database_cursor.execute(
        """
        INSERT INTO accounts ('website', 'username', 'email', 'password')
        VALUES (website, username, email, password)
    """
    )
    

    收件人:

    database_cursor.execute(
        f"""
        INSERT INTO accounts (website, username, email, password)
        VALUES ('{user_website}', '{user_username}', '{user_email}', '{user_password}');
     """
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 2021-01-12
      • 2021-01-27
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多