简体   繁体   English

SQLite NameError:名称“光标”未定义

[英]SQLite NameError: name 'cursor' is not defined

I am trying to make a virtual machine, but in a CLI.我正在尝试制作虚拟机,但在 CLI 中。 I am using SQLite for the login data and potentially for other data in the future.我将 SQLite 用于登录数据,将来可能用于其他数据。 Sadly, I keep getting this error and I don't know how to fix it.可悲的是,我不断收到此错误,我不知道如何解决它。 Could you guys help me?你们能帮帮我吗? I plan on releasing the first version of this on GitHub when I fixed this error.当我修复此错误时,我计划在 GitHub 上发布此版本的第一个版本。

from os import system, name
import sqlite3
from sqlite3 import Error

db_file = "userdata"

def create_connection(db_file):
    """ create a database connection to a SQLite database """
    conn = None
    try:
        conn = sqlite3.connect(db_file)
        print(sqlite3.version)
    except Error as e:
        print(e)
    finally:
        if conn:
            conn.close()

def clear():
    if name == 'nt':
        _ = system('cls')
    else: _ = system('clear')

def todolist():
    print("     Backlog | To-do                     | In progress     | Finished (coming in next update)")
    print("     --------|---------------------------|-----------------|---------------------------------")
    print("     -       | Register function         | SQLite database | Basis build")
    print("             | Forgot password function  |                 |")
    print("             | Login function            |                 |")

print("Made by The Celt")

print("Welcome back!")
print("Login (l)")
print("Register (r)")
print("Forgot password (fp)")

starterAction = input("What do you want to do? ")

create_connection(db_file)

if starterAction == "l":
    s.sleep(0.8)
    clear()
    insertUsername = input("Username:")
    for name in ('bar','foo'): 
        cursor.execute("SELECT rowid FROM components WHERE name = %s"%insertUsername, (name,))
        data=cursor.fetchone()
    if data is None:
        print("Could not find this username in the database!")
    else:
        insertPassword = input("Password: ")
        for name in ('bar','foo'): 
            cursor.execute("SELECT rowid FROM components WHERE name = %s"%insertPassword, (name,))
            data=cursor.fetchone()
        if data is None:
            print("Could not find this password in the database!")
        else:
            print("Welcome back, %s"%insertUsername)

elif starterAction == "r":
    s.sleep(0.8)
    clear()
    print("Still in progress!")
elif starterAction == "todolist":
    todolist()

It is not a sqlite error, it is a python error: You did not defined "cursor"这不是 sqlite 错误,而是 python 错误:您没有定义“光标”

You have to define conn and the cursor within the global scope, near to db_file您必须在 db_file 附近的全局 scope 中定义 conn 和 cursor

db_file = "userdata"
conn = None
cursor = None

and then, after connection has been established (within create_connection routine), you need to add cursor creation然后,在建立连接后(在 create_connection 例程中),您需要添加 cursor 创建

conn = sqlite3.connect(db_file)
cursor = conn.cursor()    

see here for details详情见这里

And the way you're selecting from db is also wrong:而且您从 db 中选择的方式也是错误的:

for security's sake use bind variables where possible, like that (from documentaion)为了安全起见,尽可能使用绑定变量,就像那样(来自documentaion)

cur.execute('SELECT * FROM stocks WHERE symbol=?', t)  

if you still insist on concatenating the query string (which I NOT recommend) you need to place value you're searching for within two apostrophes (I've updated %s to '%s')如果您仍然坚持连接查询字符串(我不推荐),您需要将要搜索的值放在两个撇号内(我已将 %s 更新为“%s”)

... WHERE name = '%s'"%insertPassword, (name,) ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM