简体   繁体   中英

What does "Inspection 'Unresolved reference' actually mean?

PyCharm. Here's the code:

# db_create.py
#
# Create the database and tables
#

import sqlite3
from config import DATABASE_PATH

with sqlite3.connect(DATABASE_PATH) as connection:
    cursor = connection.cursor()

    # Create table
    cursor.execute("""
                    CREATE TABLE ftasks(
                    task_id INTEGER PRIMARY KEY AUTOINCREMENT,
                    name TEXT NOT NULL,
                    due_date TEXT NOT NULL,
                    priority INTEGER NOT NULL,
                    status INTEGER NOT NULL)
                    """)

    # Insert dummy data into the table
    cursor.execute("""
                    INSERT INTO ftasks (name, due_date, priority, status)
                    VALUES("Finish this tutorial", "02/03/2014", 10, 1)
                    """)

    cursor.execute("""
                    INSERT INTO ftasks (name, due_date, priority, status)
                    VALUES("Finish Real Python Course 2", "02/03/2014", 10, 1)
                    """)

The code works just fine. The db is created and all is well. However, I get this message on the IDE. Not sure what it is referring to:

在此处输入图片说明

I got the error message to go away by simply changing this code from how it appears above:

# Insert dummy data into the table
cursor.execute("""
                INSERT INTO ftasks (name, due_date, priority, status)
                VALUES('Finish this tutorial', '02/03/2014', 10, 1)
                """)

cursor.execute("""
                INSERT INTO ftasks (name, due_date, priority, status)
                VALUES('Finish Real Python Course 2', '02/03/2014', 10, 1)
                """)

The only change is to use single quotes inside the SQL statement.

Why is the double-quoted version an unresolved reference?

I should note that both versions of the code result in exactly the same database file and contents.

Further research based on the suggestion that the double quotes inside the triple quotes cold be causing problems with PyCharm's editor/parser.

I tried this code on the editor:

a = """
INSERT INTO atable (column1, column2)
VALUES ("test",'test')
"""

b = """
BLAH BLAH atable (column1, column2)
BLAH ("test",'test')
"""

and also ran it on the Python command line:

>>> a = """
INSERT INTO atable (column1, column2)
VALUES ("test",'test')
"""
>>> b = """
BLAH BLAH atable (column1, column2)
BLAH ("test",'test')
"""
>>> a
'\nINSERT INTO atable (column1, column2)\nVALUES ("test",\'test\')\n'

>>> b
'\nBLAH BLAH atable (column1, column2)\nBLAH ("test",\'test\')\n'

Both of the above definitions produce strings with exactly the same structure. I am not seeing any issues regarding the use of the two types of quotes inside a triple-quoted string either at the Python command line, on IDLE or in PyCharm.

The IDE editor only has an issue with the string SQL version of the string and not the other:

在此处输入图片说明

If I hover the pointer over the brown highlights I get the error message. The "BLAH BLAH" version has no issues.

This tells me it has something to do with SQL/SQLite. Right?

When you using double quotes inside of a string bounded by """ , you are asking for trouble.

Python is able to handle a double-quote in these types of situations, but it appears your editor doesn't like it - most likely your editor uses a simpler parser than the Python interpreter, and chokes on String in the format """ x"x """ .

"Unresolved Reference" in this context means that PyCharm thinks the word Finish is a reference variable, since it incorrectly believes that the double-quote ended your string. Of course you do not have a reference variable named Finish .

Got word back from JetBrains. It's a bug. They are working on it.

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