简体   繁体   中英

Unable to append dataframe into sqlite3

I am trying to insert a dataframe into sqlite. My existing dataframe is as follows:

---------------------------------------
   |date        |location|location_code|
---------------------------------------
0  |12/14/2016  | FL     | 2
1  |12/15/2016  | NY     | 3

My python 3 code:

import sqlite3

conn = sqlite3.connect('transaction.sqlite')
cur = conn.cursor()

cur.executescript('''
    DROP TABLE IF EXISTS New;

    CREATE TABLE New (
        index  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
        date   TEXT,
        location TEXT,
        location_code INTEGER)
''')

df.to_sql("Records", conn, if_exists="append")

conn.commit()
conn.close()

When I run the code, I get the following error:

Traceback (most recent call last):
  File "C:/dbtest.py", line 15, in <module>
    ''')
sqlite3.OperationalError: near "index": syntax error

I know that when I change the word index to another word, it works.

However, I have no problem creating a field called index in the DB Browser for sqlite.

As you've noticed, index is a reserved word, and you cannot use it for a column name, at least not as-is - you'd have to escape it, using double quotes ( " ):

cur.executescript('''
    DROP TABLE IF EXISTS New;

    CREATE TABLE New (
        "index"  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
        date   TEXT,
        location TEXT,
        location_code INTEGER)
''')

The DB Browser probably escapes names automatically, allowing you to call a column you create there by a reserved word.

index is a reserved keyword . You need to quote it for it to be an identifier:

CREATE TABLE New (
    "index"  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    date     TEXT,
    location TEXT,
    location_code INTEGER)

From the keywords documentation:

If you want to use a keyword as a name, you need to quote it. There are four ways of quoting keywords in SQLite:

'keyword'
A keyword in single quotes is a string literal.

"keyword"
A keyword in double-quotes is an identifier.

[keyword]
A keyword enclosed in square brackets is an identifier. This is not standard SQL. This quoting mechanism is used by MS Access and SQL Server and is included in SQLite for compatibility.

`keyword`
A keyword enclosed in grave accents (ASCII code 96) is an identifier. This is not standard SQL. This quoting mechanism is used by MySQL and is included in SQLite for compatibility.

A GUI client typically takes care of quoting identifiers for you when it generates the SQL to create a table.

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