简体   繁体   English

sqlite3.OperationalError:“X”附近:CREATE TABLE 语句中的语法错误

[英]sqlite3.OperationalError: near "X": syntax error in CREATE TABLE statement

This code:这段代码:

conn = connect('emails.db')
curs = conn.cursor()

curs.execute('''create table items
  (integer primary key, X, Y)''')

curs.execute("INSERT INTO items (integer primary key, X, Y) VALUES ('today', 'X', 'Y')")

connection.commit()

returns:返回:

sqlite3.OperationalError: near "primary": syntax error sqlite3.OperationalError:靠近“主要”:语法错误

How come?怎么来的? I don't see what I'm doing wrong.我看不出我做错了什么。 The values I'm putting in there are all variables btw.顺便说一句,我输入的值都是变量。

Your CREATE TABLE is incorrect: it doesn't specify a name for the first ('integer primary key') column. 您的CREATE TABLE不正确:它没有为第一个(“整数主键”)列指定名称。 SQLite now thinks that the field is named integer , and has no data type defined. SQLite现在认为该字段名为 integer ,并且没有定义数据类型。 You probably wanted to have an INTEGER PRIMARY KEY field, because that is very efficient. 您可能想要一个INTEGER PRIMARY KEY字段,因为这非常有效。 To do so, respect the CREATE TABLE syntax , and give it a name: 为此,请尊重CREATE TABLE语法 ,并为其命名:

CREATE TABLE items
( id INTEGER PRIMARY KEY
, x  DOUBLE
, y  DOUBLE
);

As a side note: I've defined X and Y as doubles, since specifying the type is just good practice, and is also slightly more efficient. 作为旁注:我已经将X和Y定义为双精度,因为指定类型只是一种很好的做法,而且效率稍高。 Of course, if you want to put text in them, define them as TEXT. 当然,如果要将文本放入其中,请将它们定义为TEXT。 Or if you want them to contain primarily integers, define them as INTEGER. 或者,如果您希望它们主要包含整数,请将它们定义为INTEGER。 Only ever leave out the data type if you really don't know what kind of data you'll be putting in there. 如果你真的不知道你将在那里放什么样的数据,那么只能省略数据类型。

Next, since the INSERT statement only expects field names (and not their full definition), SQLite throws a Syntax Error -- rightly so. 接下来,由于INSERT语句只需要字段名称(而不是它们的完整定义),因此SQLite会抛出语法错误 - 这是正确的。

And finally, don't you think it a bit silly to put 'today' (a text value) into an integer column?!? 最后,你不觉得把'今天'(文本值)放入整数列有点傻吗?!?


Edit: since you say X and Y are variables, might as well put you on the right track at to binding those variables to the SQL statement: 编辑:既然你说X和Y是变量,那么也可以让你把这些变量绑定到SQL语句的正确轨道上:

curs.execute("INSERT INTO items (X, Y) VALUES (:X, :Y)", {X: X, Y: Y})

I've left out the id primary key field, since SQLite will automatically generate that if it's not present. 我遗漏了id主键字段,因为如果它不存在,SQLite将自动生成它。 If you want to pass an explicit value, you can. 如果要传递显式值,则可以。 But take care that it is an integer, and that it is unique! 但要注意它是一个整数,它是独一无二的!

The :X and :Y parameters in the SQL statement refer to the X and Y members of the dictionary that is passed as second parameter to the execute statement. SQL语句中的:X:Y参数引用作为第二个参数传递给execute语句的字典的XY成员。

Binding parameters is both safer and faster than including the parameters into the SQL string itself. 绑定参数比将参数包含在SQL字符串本身中更安全,更快捷

import sqlite3
conn=sqlite3.connect('sqlite.db')
conn.execute('''
        Creat: table student (
           st_id INT AUTO INCREMENT PRIMARY KEY,
           st_name VARCHAR(50),
           st_class VARCHAR(10),
           st_class VARCHAR(30)
        )
        ''')
conn.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM