简体   繁体   中英

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

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',)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM