简体   繁体   中英

SQLite3 Python 2.7 sqlite3.OperationalError syntax error

import sqlite3
import codecs

sqlite3.connect("test.db")
db.execute("""CREATE TABLE IF NOT EXISTS STATIONS
       (StationUID INT PRIMARY KEY     NOT NULL,
       StationNumber           TEXT    NOT NULL,
       StationName             TEXT    NOT NULL,
       StationLongitude        REAL    NOT NULL,
       StationAltitude         REAL    NOT NULL);""")

print "Created Table STATIONS successfully"
cr = db.cursor()
name = "b"
station_number = "0123"
longitude = 13.4
altitude = 34.4
cr.execute("INSERT INTO STATIONS VALUES", (None, station_number, name, longitude, altitude))
db.commit()

It throws sqlite3.OperationalError: near "VALUES": syntax error , but I don't understand why, because it is the same syntax I found in the example.

You need to specify what values to insert :

INSERT INTO STATIONS VALUES (value1, value2, ...)

You cannot omit that part. Use SQL parameters to map Python values to those locations in the SQL syntax:

cr.execute(
    "INSERT INTO STATIONS VALUES (?, ?, ?, ?, ?)",
    (None, station_number, name, longitude, altitude))

The INSERT command is not complete. You need to insert the values into the INSERT by writing:

cr.execute("INSERT INTO STATIONS VALUES (?, ?, ?, ?, ?)", (None, station_number, name, longitude, altitude))

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