在SQLite中创建函数时光标处的操作错误

时间:2019-01-08 11:31:13

标签: database python-3.x sqlite

我正在尝试将数据插入我的SQLite数据库中,直到我在为DB创建函数时收到错误为止,一切正常。它发送OperationalError at cursor

我找不到解决问题的方法。

我正在使用的代码:

import sqlite3
from sqlite3 import *

SQL_CREATE_STATEMENT = '''CREATE TABLE password
             (id integer PRIMARY KEY NOT NULL,username text, password text, source text)'''
SQL_INSERT_STATEMENT = '''INSERT INTO password (username, password, source)VALUES({},{},{});'''

DATABASE_PATH = 'home/taha/lessons/projects/passStorage/passDB.db'

DATA = dict()


def create_connection(db_file):
    try:
        conn = sqlite3.connect(db_file)
        return conn
    except Error as e:
        return e

def create_table(connection, sql_commands):
    c = connection.cursor()
    c.execute(sql_commands)
    print('done')

def get_input():
    USERNAME = input('username: ')
    PASSWORD = input('password: ')
    SOURCE = input('source: ')
    return USERNAME,PASSWORD,SOURCE

def insert_date(connection, data):
    c = connection.cursor()
    c.execute(SQL_INSERT_STATEMENT.format(data.values))

def main():
    conn = create_connection(DATABASE_PATH)
    create_table(conn, SQL_CREATE_STATEMENT)

    user_info = get_input()
    DATA['username'], DATA['password'], DATA['SOURCE'] = user_info

    insert_date(conn, DATA)

if __name__ == '__main__':
    main()

我希望没有错误,但是会发送以下消息:

c = connection.cursor()

AttributeError: 'OperationalError' object has no attribute 'cursor'

1 个答案:

答案 0 :(得分:2)

def create_connection(db_file):
    try:
        conn = sqlite3.connect(db_file)
        return conn
    except Error as e:
        return e # <-- here you return OperationalError instance
  

AttributeError:“ OperationalError”对象没有属性“ cursor”

表明OperationalError没有属性cursor

在此处添加其他检查连接的逻辑。

我相信您问题的核心是错误的文件路径:

DATABASE_PATH = 'home/taha/lessons/projects/passStorage/passDB.db'

但是我相信应该是

DATABASE_PATH = '/home/taha/lessons/projects/passStorage/passDB.db'