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()
它会抛出sqlite3.OperationalError: near "VALUES": syntax error
,但我不明白为什么,因为它与我在示例中找到的语法相同。
答案 0 :(得分:2)
您需要指定要插入的值:
INSERT INTO STATIONS VALUES (value1, value2, ...)
你不能省略那部分。使用SQL参数将Python值映射到SQL语法中的那些位置:
cr.execute(
"INSERT INTO STATIONS VALUES (?, ?, ?, ?, ?)",
(None, station_number, name, longitude, altitude))
答案 1 :(得分:1)
INSERT
命令未完成。您需要通过编写
INSERT
cr.execute("INSERT INTO STATIONS VALUES (?, ?, ?, ?, ?)", (None, station_number, name, longitude, altitude))