NaturallyGreezy v2
NaturallyGreezy v2

Reputation: 65

Syntax error I can't figure out? python3

This is my program. It tells me exactly where the the syntax error is, but I literally have no idea what the problem is:

import sqlite3

def create_table(dbName, table_name, sql):
    with sqlite3.connect("ATM.db") as db:
        cursor = db.cursor()
        cursor.execute("select name from sqlite_master where name=?",(table_name,))
        result = cursor.fetchall()
        keep_table = True
        if len(result) == 1:
            response = input("The table{0} already exists, do you wish to recreate? (y/n) \n".format(table_name))
            if response.lower() == "y":
                keep_table = False
                print("the {0} table will be recreated".format(table_name))
                cursor.execute("drop table if exists {0}".format(table_name))
                db.commit()
                insert_data()
            else:
                print("The existing table was kept")
                insert_data()
        else:
            keep_table = False
        if not keep_table:
>>>>>>      cursor.execute(sql) #Problem is supposedly here?
            db.commit()
            insert_data()

def insert_data():
    with sqlite3.connect("ATM.db") as db:
        cursor = db.cursor()
        sql = """insert into ATM(Title, First Name, Surname, Balance) values (Mr., Jeremy, Clarkson, 172.16),
                                                                             (Miss, Suzanne, Perry, 15.62)"""
        cursor.execute(sql, values)
        db.commit()


dbName = "ATM.db"
sql = """ create table ATM
          (CustomerID integer,
          Title text
          First Name text,
          Surname text,
          Balance real,
          CustomerID(CustomerID))"""
create_table(dbName, "ATM", sql)

This is the syntax error message I am getting. I've put arrows in the code which highlights where it is pointing out:

line 23, in create_table
    cursor.execute(sql)
sqlite3.OperationalError: near "(": syntax error

Upvotes: 0

Views: 678

Answers (1)

Arya McCarthy
Arya McCarthy

Reputation: 8829

The error is in your SQL code, not the Python.

A few things:

  1. You need to end all of your sqlite3 commands with a semicolon, or else it just keeps reading as one command. That led to confusion about where the error was.
  2. Adding the semicolon, we see that the problem is actually in your CREATE TABLE command. This line makes no sense: "CustomerID(CustomerID))". Were you intending to set a PRIMARY KEY?

    create table ATM (
      CustomerID integer,
      Title text
      First Name text,
      Surname text,
      Balance real,
      PRIMARY KEY(CustomerID)
    );
    

Incidentally, you have another problem. This is your query:

insert into ATM(Title, First Name, Surname, Balance) values (Mr., Jeremy, Clarkson, 172.16),
                                                                         (Miss, Suzanne, Perry, 15.62)

The problem is that you need to wrap your strings in quotes, rather than treating them as bare words and hoping they'll be interpreted properly:

    insert into ATM(Title, First Name, Surname, Balance) values ("Mr.", "Jeremy", "Clarkson", 172.16),
                                                                         ("Miss", "Suzanne", "Perry", 15.62);

Otherwise, it's an invalid query because the bare words make it malformed.

Upvotes: 1

Related Questions