簡體   English   中英

Python SQLite中的語法錯誤

[英]Syntax Error In Python SQLite

下面的代碼有效的更新代碼

嘗試運行此命令時遇到語法錯誤。 以下是相關代碼:

import sqlite3
mydatabase="/Users/noahclark/cities.db"
connection=sqlite3.connect(mydatabase)
cur = connection.cursor()

def getDaysURLS(self):
    day = cur.execute('select state from cities where city = "pointer"')
    day = cur.fetchone()[0]
    print day
    cur.execute('select URL from cities where day = ?', (str(day),))

運行此代碼時,出現以下錯誤。

  Tkinter.py", line 1410, in __call__
  return self.func(*args)
  File "tkinter.py", line 50, in getDaysURLS
  cur.execute(urlstring)
  OperationalError: near "<": syntax error

我可以在sqlite命令行中運行-從城市是“指針”的城市中選擇狀態-行。

有什么想法或建議嗎? 提前致謝。

SQLite遵循SQL標准:字符串用單引號引起來,而不是雙引號引起來。 另外,通常使用=檢查字符串是否相等。

檢查null時僅在sql中使用

  day = cur.execute('select state from cities where city = "pointer"')

或者更好:

  day = cur.execute('select state from cities where city = ?',("pointer",))

編輯

 urlstring = "select URL from cities where day is" + str(day)
 cur.execute(urlstring)
  1. 使用 ? 我以前展示的方法
  2. 之后需要一個空格
  3. cur.execute()不會返回這樣的值。 我不確定返回的內容。 您需要使用提取方法。

不要使用雙引號。 SQLite使用這些字符來轉義標識符,例如字段和表名。 請改用單引號。 此外,您應該使用=代替is

day = cur.execute("select state from cities where city = 'pointer'")

UPDATEcur.execute()返回游標cur ,而不是查詢的結果。 您需要調用cursor.fetchone()來獲取值:

# Execute the query.  If all went well, you can use the cursor to navigate through
# the results.
cur.execute("select state from cities where city = 'pointer'")
# Fetch the next row and extract the first value in the row.
day = cur.fetchone()[0]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM