不知道为什么在尝试使用 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个小时来教计算机小白一些编程知识,你会选择教些什么�...

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

在Python中,如何通过字符串动态创建对象并调用其方法?这是一个常见的编程需求,尤其在需要根据配置或运行...

本文讨论了诸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和请求等流行的Python库,并详细介绍了它们在科学计算,数据分析,可视化,机器学习,网络开发和H中的用途
