使用Python和SQL时获取NamError

问题描述 投票:1回答:1

我正在运行学生数据库并在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语句看起来像这样

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
python mysql sql sqlite nameerror
1个回答
1
投票

我的猜测是你正在使用Python2

从用户接收String输入时,需要使用raw_input()方法:

FirstName = raw_input("Enter the Student's first name")
© www.soinside.com 2019 - 2024. All rights reserved.