Search code examples
pythonmysqlsqlsqlitenameerror

Getting a NamError when using Python and SQL


I'm running a student database and using python 2.7 on pycharm. Here is the script

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

and the create Table stement looks like so

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)
)

and the error I'm getting is

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

Solution

  • My guess is that you're using Python2

    You need to use the raw_input() method when receiving String input from the user:

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