简体   繁体   中英

sqlite3.OperationalError - no column exists

I am having an issue running a simple select statement within SQLite.

One of the columns in my database has "" quotes in its name. How would I go about selecting this column in my query?

Here's the table schema:

在此处输入图片说明

I've tried a variety of Select statements but no dice.

Here's my code so far:

在此处输入图片说明

Here's the error I'm running into:

在此处输入图片说明

What's the proper syntax to select this column: ["Address"]?

Sqlite uses square brackets and double quotes as a way to denote that the enclosed text is an identifier ( docs ). However by using both, like this:

CREATE TABLE (["Address"] TEXT...

the double quotes become part of the identifier.

If you want to use the column in a query you will need to put square brackets around the name including the double quotes, as they are part of the name, as seen in Q1 in this script.

import sqlite3    
    
DDL1 = """DROP TABLE IF EXISTS test"""    
DDL2 = """\    
CREATE TABLE test (    
 ["Address"] text)    
"""    
    
Q1 = """SELECT ["Address"] FROM test"""    
    
    
with sqlite3.connect('68132004.db') as conn:    
    cur = conn.cursor()    
    cur.execute(DDL1)    
    cur.execute(DDL2)    
    cur.execute(Q1)    
    

However you can make your life easier by renaming the column using an ALTER TABLE instruction (the double quotes here signify that the enclosed text is an identifier):

DDL3 = """ALTER TABLE test RENAME COLUMN ["Address"] TO "Address" """    
    
Q2 = """SELECT Address FROM test"""    
    
    
with sqlite3.connect('68132004.db') as conn:    
    cur = conn.cursor()    
    cur.execute(DDL3)    
    cur.execute(Q2) 

Or you could drop the table and recreate it without the doubly-identifier-quoted column name.

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