简体   繁体   English

如何使用ROWID在Python SQLite3中编辑特定行的内容

[英]How to edit the contents of a specific row in Python SQLite3 using ROWID

I have the following database (test.db): Test Database 我有以下数据库(test.db): 测试数据库

As seen in the picture,I created and populated a table ("stuffToPlot"). 如图所示,我创建并填充了一个表格(“ stuffToPlot”)。

I want to edit a specific row (the 5th row for example), and change all the values therein. 我要编辑特定的一行(例如第5行),并更改其中的所有值。 I tried the following code to do this: 我尝试了以下代码来做到这一点:

import sqlite3

conn = sqlite3.connect('test.db')
c = conn.cursor()

tableToEdit = 'stuffToPlot'
rowToEdit = '5'
unixVar = 5.5
dateStampVar ='feb-2018'
keywordVar = 'Hello World'
valueVar = 25

c.execute("""INSERT INTO """+tableToEdit+""" (unix, datestamp, keyword, value)
VALUES (?,?,?,?) WHERE ROWID =""" +rowToEdit),(unixVar, dateStampVar, keywordVar, valueVar) 


conn.commit()

c.close()
conn.close()

I get the following error when trying to run the code: 尝试运行代码时出现以下错误:

Traceback (most recent call last): File "C:\\Users\\Bob\\Documents\\Eclipse Workspace\\Python Test\\SQLite3\\SQLite3-3.py", line 19, in VALUES (?,?,?,?) WHERE ROWID =""" +rowToEdit),(unixVar, dateStampVar, keywordVar, valueVar) sqlite3.OperationalError: near "WHERE": syntax error 追溯(最近一次通话):文件“ C:\\ Users \\ Bob \\ Documents \\ Eclipse Workspace \\ Python Test \\ SQLite3 \\ SQLite3-3.py”,第19行,位于VALUES(?,?,?,?)中=“”“ + rowToEdit),(unixVar,dateStampVar,keywordVar,valueVar)sqlite3.OperationalError:“ WHERE”附近:语法错误

I have also tried using the UPDATE/SET method, but get a different error: 我也尝试过使用UPDATE / SET方法,但是得到了另一个错误:

c.execute("""UPDATE """+tableToEdit+""" (unix, datestamp, keyword, value)
SET (?,?,?,?) WHERE ROWID =""" +rowToEdit),(unixVar, dateStampVar, keywordVar, valueVar)

File "C:\\Users\\Shaun\\Documents\\Eclipse Workspace\\Python Test\\SQLite3\\SQLite3-3.py", line 24, in SET (?,?,?,?) WHERE ROWID =""" +rowToEdit),(unixVar, dateStampVar, keywordVar, valueVar) sqlite3.OperationalError: near "(": syntax error 文件“ C:\\ Users \\ Shaun \\ Documents \\ Eclipse Workspace \\ Python Test \\ SQLite3 \\ SQLite3-3.py”,位于SET(?,?,?,?)WHERE ROWID =“”“ + rowToEdit)中的第24行, (unixVar,dateStampVar,keywordVar,valueVar)sqlite3.OperationalError:“(”附近:语法错误

I just want to edit a specific row (using ROWID) ,any help would be greatly appreciated. 我只想编辑一个特定的行(使用ROWID),任何帮助将不胜感激。

INSERT always adds a new row, you can't use a WHERE clause when using INSERT . INSERT总是添加行,使用INSERT时不能使用WHERE子句。 So yes, you have to use UPDATE here. 所以是的,您必须在此处使用UPDATE Your UPDATE syntax is wrong however. 但是,您的UPDATE语法错误。

UPDATE uses columname=value pairs, see the official documentation for UPDATE : UPDATE使用columname=value对,请参见UPDATE官方文档

c.execute(
    """UPDATE {} SET unix=?, datestamp=?, keyword=?, value=?
       WHERE ROWID = ?""".format(tableToEdit),
    (unixVar, dateStampVar, keywordVar, valueVar, int(rowToEdit)))

I switched from using concatenation to str.format() , but only to put the table name in. The ROWID value can be passed in as a query parameter, so do so. 我使用连接来交换str.format()但只在把表名, ROWID值可以被传递作为查询参数,所以这样做。

Demo: 演示:

>>> import sqlite3
>>> conn = sqlite3.connect(':memory:')
>>> conn.execute('''
... CREATE TABLE stuffToPlot (unix REAL, datestamp TEXT, keyword TEXT, value INTEGER)
... ''')
<sqlite3.Cursor object at 0x10f049ce0>
>>> with conn:
...     for _ in range(10):
...         c = conn.execute('''
...             INSERT INTO stuffToPlot VALUES (42.0, "mar-2010", "The quick brown fox", 81)
...         ''')
...
>>> tableToEdit = 'stuffToPlot'
>>> rowToEdit = '5'
>>> unixVar = 5.5
>>> dateStampVar ='feb-2018'
>>> keywordVar = 'Hello World'
>>> valueVar = 25
>>> with conn:
...     conn.execute(
...         """UPDATE {} SET unix=?, datestamp=?, keyword=?, value=?
...            WHERE ROWID = ?""".format(tableToEdit),
...         (unixVar, dateStampVar, keywordVar, valueVar, int(rowToEdit)))
...
<sqlite3.Cursor object at 0x10f049ce0>
>>> print(*conn.execute('SELECT * FROM stuffToPlot WHERE ROWID=5'))
(5.5, 'feb-2018', 'Hello World', 25)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM