如何解决此sqlite3.OperationalError / sql语法错误?

时间:2018-10-19 12:36:39

标签: python sqlite

我的sqllite数据库无法正常工作,并且我有一些我认为与SQL语法有关的错误。

import sqlite3
conn = sqlite3.connect("SONGS.db")

conn.execute("""CREATE TABLE SONGS
         (ID INT PRIMARY KEY     NOT NULL,
         Song-Name       TEXT     NOT NULL,
         Artist       TEXT    NOT NULL,
         Released       TEXT    NOT NULL,
         Views       TEXT    NOT NULL);""")
conn.close()

conn = sqlite3.connect("SONGS.db")

conn.execute("INSERT INTO SONGS (ID,Song-Name,Artist,Released,Views) \
      VALUES (1, 'Counting Stars', 'OneRepublic', 'May 31, 2013', '2,458,427,299' )");

conn.execute("INSERT INTO SONGS (ID,Song-Name,Artist,Released,Views) \
      VALUES (2, 'Waka Waka', 'Shakira', 'June 4, 2010', '2,052,927,789' )");

conn.execute("INSERT INTO SONGS (ID,Song-Name,Artist,Released,Views) \
      VALUES (3, 'I Want It That Way', 'Backstreet Boys', 'October 25, 2009', '515.894,015' )");

conn.execute("INSERT INTO SONGS (ID,Song-Name,Artist,Released,Views) \
      VALUES (4, 'Psycho', 'Post Malone', 'March 22, 2018', '400,195,600' )");

conn.execute("INSERT INTO SONGS (ID,Song-Name,Artist,Released,Views) \
      VALUES (5, 'Till I Collapse', 'Eminem', 'September 16, 2015', '335,842,184' )");

conn.execute("INSERT INTO SONGS (ID,Song-Name,Artist,Released,Views) \
      VALUES (6, 'These Days', 'Rudimental', 'January 25, 2018', '270,529,823' )");

conn.execute("INSERT INTO SONGS (ID,Song-Name,Artist,Released,Views) \
      VALUES (7, 'Juice WRLD', 'Black & White', 'October 3, 2018', '5,268,766' )");

conn.execute("INSERT INTO SONGS (ID,Song-Name,Artist,Released,Views) \
      VALUES (8, 'Shotgun', 'George Ezra', 'June 14, 2018', '18,291,200' )");


conn.commit()
conn.close()

有我的代码,这是错误

  

第25行,在       查看TEXT NOT NULL);“”“)sqlite3.OperationalError:“-”附近:语法错误

1 个答案:

答案 0 :(得分:0)

,此错误是由于数据库表中名为View

的字段引起的

我认为您不需要;来声明conn.execute

因此error line 25, in Views TEXT NOT NULL

另一个建议,使用字符串替换:

insert = "INSERT INTO SONGS (ID,Song-Name,Artist,Released,Views) VALUES ({}, {}, {}, {}, {})"

插入时,请执行以下操作:

conn.execute(insert.format(1, 'Counting Stars', 'OneRepublic', 'May 31, 2013', '2,458,427,299'))