如何解决sqlite3.connect.cursor.execute()函数的语法错误

huangapple go评论81阅读模式
英文:

How to Resolve Syntax Error of the sqlite3.connect.cursor.execute() Function

问题

我正在创建一个使用SQLite3库的Python练习数据库脚本,遵循一个非常有帮助的YouTube指南,并且在创建数据库的最初阶段遇到了问题。请看一下我的代码:

```python
import sqlite3

#connect = sqlite3.connect(':memory:') # 如果想要在内存中创建数据库。
con = sqlite3.connect('Exercise Database.db') # 连接到数据库。
c = con.cursor() # 创建一个游标。

c.execute('''CREATE TABLE IF NOT EXISTS Exercises(
Name TEXT,
Force TEXT,
Equipment TEXT,
OneRM REAL,
Difficulty TEXT,
Primary TEXT,
Secondary TEXT,
Tertiary TEXT,
Animation BLOB
)''') # 创建一个表格。这里使用了5种数据类型:NULL、INTEGER、REAL、TEXT和BLOB。

con.commit() # 提交我们的命令。
con.close() # 关闭连接。

在运行上述代码块时,我收到了以下错误消息:

Traceback (most recent call last):
  File "/Users/johncre752/Desktop/Exercise Database.py", line 49, in <module>
    c.execute('''CREATE TABLE IF NOT EXISTS Exercises(
sqlite3.OperationalError: near "TEXT": syntax error

我原本希望在运行时不会出现错误,因为我已经根据教程以及Python库的官方网站仔细检查了我的代码,但似乎找不到任何导致此错误的差异。在网上查看其他人与此语法错误相关的帖子时,我注意到所有这些帖子都涉及到不同的原因,导致他们看到了这条消息(内存不足,处理了“?”变量而不是这个等等)。
我是一名新手程序员,所以我会感激任何人能够识别出问题所在。谢谢!


<details>
<summary>英文:</summary>

I&#39;m creating an exercise database Python script using the SQLite3 library &amp; following a [very helpful YouTube guide](https://www.youtube.com/watch?v=byHcYRpMgI4), &amp; I&#39;m running into issues near the very beginning of just creating the database in the first place. Please take a look at my code below:

import sqlite3

#connect = sqlite3.connect(':memory:') # If want database in memory.
con = sqlite3.connect('Exercise Database.db') # Connect to database.
c = con.cursor() # Create a cursor.

c.execute('''CREATE TABLE IF NOT EXISTS Exercises(
Name TEXT,
Force TEXT,
Equipment TEXT,
OneRM REAL,
Difficulty TEXT,
Primary TEXT,
Seconary TEXT,
Tertiary TEXT,
Animation BLOB
)''') # Create a table. The 5 datatypes are NULL, INTEGER, REAL, TEXT, & BLOB.

con.commit() # Commit our command.
con.close() # Close our connection.

Upon running the above block, I get the following error message:

Traceback (most recent call last):
File "/Users/johncre752/Desktop/Exercise Database.py", line 49, in <module>
c.execute('''CREATE TABLE IF NOT EXISTS Exercises(
sqlite3.OperationalError: near "TEXT": syntax error

I was expecting no errors to occur while running this, as I&#39;ve closely examined my code in accordance w/ the tutorial as well as [Python&#39;s own website for the library](https://docs.python.org/3/library/sqlite3.html), &amp; I can&#39;t seem to find any discrepancies that would warrant this. Looking online at others&#39; posts related to this syntax error, I noticed all of them involved different reasons as to why they experienced that message. (Not enough size for memory, dealing w/ the &quot;?&quot; variable instead of this, etc.)
I&#39;m a newbie programmer, so I&#39;d appreciate anyone able to identify the issue at hand. Thank you!

</details>


# 答案1
**得分**: 0

"Primary" 在 SQLite 中是一个保留关键字,您可以在[这里][1]查看关键字列表:

如果您修改查询,它就可以正常工作:

```sql
CREATE TABLE IF NOT EXISTS Exercises(
Name TEXT,
Force TEXT,
Equipment TEXT,
OneRM REAL,
Difficulty TEXT,
`Primary` TEXT,
Secondary TEXT,
Tertiary TEXT,
Animation BLOB
)
英文:

Primary is a reserved key in SQLite, you can check the keywords here:

If you modify your query it works:

CREATE TABLE IF NOT EXISTS Exercises(
Name TEXT,
Force TEXT,
Equipment TEXT,
OneRM REAL,
Difficulty TEXT,
`Primary` TEXT,
Seconary TEXT,
Tertiary TEXT,
Animation BLOB
)

huangapple
  • 本文由 发表于 2023年7月4日 20:07:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76612453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定