不知道為什麼在嘗試使用 f 字串執行 SQL UPDATE 查詢時會出現語法錯誤
我正在嘗試執行基本的sql 查詢來更新名為bartonhill 的資料庫中schoolnum 的值,使用f 字串將值放入
import sqlite3 name = [input('enter your firstname: ')] conn = sqlite3.connect('revision.db') c = conn.cursor() # create table c.execute('''CREATE TABLE IF NOT EXISTS bartonHill ( firstName TEXT NOT NULL, lastName TEXT NOT NULL, schoolNum INTEGER NOT NULL PRIMARY KEY )''') conn.commit() c.execute("INSERT INTO bartonHill VALUES ('Ollie','Burgess','20314')") conn.commit() new_number = tuple[(int(input('enter your new school number')))] c.execute(f"UPDATE bartonHill set schoolNum = '{new_number}' WHERE firstName = '{name}'") conn.commit c.execute('SELECT lastName, schoolNum FROM bartonHill WHERE firstName = (?)',name) print(c.fetchall())
但是,如果我輸入我的名字 ollie(作為 firsname 存在於資料庫中),則執行此程式碼會出現錯誤「 sqlite3.operationalerror:near「ollie」:語法錯誤」
我嘗試更改 name 和 new_number 的資料類型,但這只更改了錯誤
#正確答案
還有很多需要改進的地方。我只做了一些小改動,你可以進一步改進。始終插入元組。更改主鍵是可能的,但不是那麼容易,谷歌一下。首次初始化值後請求新值。也請仔細閱讀上面的評論!
import sqlite3 conn = sqlite3.connect('revision.db') c = conn.cursor() # create table c.execute('''create table if not exists bartonhill ( id integer primary key autoincrement, schoolnum integer not null, firstname text not null, lastname text not null )''') conn.commit() c.execute("insert into bartonhill (schoolnum,firstname,lastname) values (?,?,?); ", (20314, 'ollie','burgess')) conn.commit() # ask for changes here firstname = input('enter your firstname: ') lastname = input('enter your lastname: ') new_number = int(input('enter your new school number: ')) c.execute("update bartonhill set schoolnum = ? where firstname = ?;", (new_number, firstname)) conn.commit c.execute("select * from bartonhill where firstname = ?;", (firstname,)) print(c.fetchall())
輸出:
>>> enter your firstname: ollie >>> enter your lastname: burgess >>> enter your new school number: 20000 >>> [(1, 20000, 'ollie', 'burgess')]
或另一個選擇:
c.execute("select schoolnum, lastname from bartonhill where firstname = ?;", (firstname,)) print(c.fetchone())
僅給您特定值:
(20000, 'Burgess')
以上是不知道為什麼在嘗試使用 f 字串執行 SQL UPDATE 查詢時會出現語法錯誤的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Linux終端中查看Python版本時遇到權限問題的解決方法當你在Linux終端中嘗試查看Python的版本時,輸入python...

在使用Python的pandas庫時,如何在兩個結構不同的DataFrame之間進行整列複製是一個常見的問題。假設我們有兩個Dat...

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Uvicorn是如何持續監聽HTTP請求的? Uvicorn是一個基於ASGI的輕量級Web服務器,其核心功能之一便是監聽HTTP請求並進�...

本文討論了諸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和請求等流行的Python庫,並詳細介紹了它們在科學計算,數據分析,可視化,機器學習,網絡開發和H中的用途

在Python中,如何通過字符串動態創建對象並調用其方法?這是一個常見的編程需求,尤其在需要根據配置或運行...
