首页 > 解决方案 > 使用 Python 和 SQL 时出现 NamError

问题描述

我正在运行一个学生数据库并在 pycharm 上使用 python 2.7。这是脚本

FirstName = input("Enter the Student's first name")
    if not FirstName or type(FirstName) != str:
    print("Enter a real name silly")
    exit()

和 create Table stement 看起来像这样

drop table if exists StudentID;
drop table if exists FirstName;
drop table if exists LastName;
drop table if exists GPA;
drop table if exists Major;
drop table if exists FacultyAdvisor;
CREATE TABLE Student(
  StudentID int PRIMARY KEY AUTOINCREMENT ,
  FirstName varchar(25),
  LastName varchar(25),
  GPA NUMERIC,
  Major varchar(10),
  FacultyAdvisor varchar(25)
)

我得到的错误是

FirstName = input("Enter the Student's first name")
  File "<string>", line 1, in <module>
NameError: name 'john' is not defined

标签: pythonmysqlsqlsqlitenameerror

解决方案


我的猜测是你正在使用 Python2

raw_input()接收用户输入的字符串时需要使用该方法:

FirstName = raw_input("Enter the Student's first name")

推荐阅读