我正在尝试运行基本的 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中文网其他相关文章!

Python在游戏和GUI开发中表现出色。1)游戏开发使用Pygame,提供绘图、音频等功能,适合创建2D游戏。2)GUI开发可选择Tkinter或PyQt,Tkinter简单易用,PyQt功能丰富,适合专业开发。

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。 Python以简洁和强大的生态系统着称,C 则以高性能和底层控制能力闻名。

2小时内可以学会Python的基本编程概念和技能。1.学习变量和数据类型,2.掌握控制流(条件语句和循环),3.理解函数的定义和使用,4.通过简单示例和代码片段快速上手Python编程。

Python在web开发、数据科学、机器学习、自动化和脚本编写等领域有广泛应用。1)在web开发中,Django和Flask框架简化了开发过程。2)数据科学和机器学习领域,NumPy、Pandas、Scikit-learn和TensorFlow库提供了强大支持。3)自动化和脚本编写方面,Python适用于自动化测试和系统管理等任务。

两小时内可以学到Python的基础知识。1.学习变量和数据类型,2.掌握控制结构如if语句和循环,3.了解函数的定义和使用。这些将帮助你开始编写简单的Python程序。

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

Python3.6环境下加载Pickle文件报错:ModuleNotFoundError:Nomodulenamed...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

WebStorm Mac版
好用的JavaScript开发工具

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

Dreamweaver Mac版
视觉化网页开发工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Atom编辑器mac版下载
最流行的的开源编辑器