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

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

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

问题

  1. 我正在创建一个使用SQLite3库的Python练习数据库脚本,遵循一个非常有帮助的YouTube指南,并且在创建数据库的最初阶段遇到了问题。请看一下我的代码:
  2. ```python
  3. import sqlite3
  4. #connect = sqlite3.connect(':memory:') # 如果想要在内存中创建数据库。
  5. con = sqlite3.connect('Exercise Database.db') # 连接到数据库。
  6. c = con.cursor() # 创建一个游标。
  7. c.execute('''CREATE TABLE IF NOT EXISTS Exercises(
  8. Name TEXT,
  9. Force TEXT,
  10. Equipment TEXT,
  11. OneRM REAL,
  12. Difficulty TEXT,
  13. Primary TEXT,
  14. Secondary TEXT,
  15. Tertiary TEXT,
  16. Animation BLOB
  17. )''') # 创建一个表格。这里使用了5种数据类型:NULL、INTEGER、REAL、TEXT和BLOB。
  18. con.commit() # 提交我们的命令。
  19. con.close() # 关闭连接。

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

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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.

  1. 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

  1. 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.)
  2. I&#39;m a newbie programmer, so I&#39;d appreciate anyone able to identify the issue at hand. Thank you!
  3. </details>
  4. # 答案1
  5. **得分**: 0
  6. "Primary" SQLite 中是一个保留关键字,您可以在[这里][1]查看关键字列表:
  7. 如果您修改查询,它就可以正常工作:
  8. ```sql
  9. CREATE TABLE IF NOT EXISTS Exercises(
  10. Name TEXT,
  11. Force TEXT,
  12. Equipment TEXT,
  13. OneRM REAL,
  14. Difficulty TEXT,
  15. `Primary` TEXT,
  16. Secondary TEXT,
  17. Tertiary TEXT,
  18. Animation BLOB
  19. )
英文:

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

If you modify your query it works:

  1. CREATE TABLE IF NOT EXISTS Exercises(
  2. Name TEXT,
  3. Force TEXT,
  4. Equipment TEXT,
  5. OneRM REAL,
  6. Difficulty TEXT,
  7. `Primary` TEXT,
  8. Seconary TEXT,
  9. Tertiary TEXT,
  10. Animation BLOB
  11. )

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:

确定