Dance Party2
Dance Party2

Reputation: 7536

SQLite3 OperationalError: near "(": syntax error

I have looked everywhere and cannot figure out why this is not working.

import sqlite3

path = '/Users/...'
con = sqlite3.connect(path+'YSD.sqlite')
cur = con.cursor()
cmd = 'SELECT ISD_NAME replace(ISD_NAME,"Statewide"," Statewide") FROM enr'
cur.execute(cmd)
con.commit()
con.close()

OperationalError: near "(": syntax error

I have also tried this, to no avail:

cmd = 'SELECT ISD_NAME replace(ISD_NAME,?,?) FROM enr'
cur.execute(cmd, ("\"Statewide\"","\" Statewide\""))

Upvotes: 0

Views: 842

Answers (1)

Andrea Baldini
Andrea Baldini

Reputation: 1057

If your intent is to fetch ISD_NAME column values and replace "Statewide" with " Statewide" remove ISD_NAME after SELECT statement.

For instance, assuming you have a table like this:

CREATE TABLE enr("id" PRIMARY KEY, "ISD_NAME" VARCHAR);
INSERT INTO enr VALUES (1, "somethingStatewide");

Your script becomes:

cmd = 'SELECT replace(ISD_NAME,"Statewide"," Statewide") FROM enr'
cur.execute(cmd)

Test with print:

records = cur.fetchall()
print(records)

Here the result:

[(u'something Statewide',)]

Upvotes: 1

Related Questions