- Python 数据访问教程
- Python 数据访问 - 主页
- Python MySQL
- Python MySQL - 简介
- Python MySQL - 数据库连接
- Python MySQL - 创建数据库
- Python MySQL - 创建表
- Python MySQL - 插入数据
- Python MySQL - 选择数据
- Python MySQL -Where 子句
- Python MySQL - 排序依据
- Python MySQL - 更新表
- Python MySQL - 删除数据
- Python MySQL - 删除表
- Python MySQL - 限制
- Python MySQL - 加入
- Python MySQL - 游标对象
- Python PostgreSQL
- Python PostgreSQL - 简介
- Python PostgreSQL - 数据库连接
- Python PostgreSQL - 创建数据库
- Python PostgreSQL - 创建表
- Python PostgreSQL - 插入数据
- Python PostgreSQL - 选择数据
- Python PostgreSQL -Where 子句
- Python PostgreSQL - 排序依据
- Python PostgreSQL - 更新表
- Python PostgreSQL - 删除数据
- Python PostgreSQL - 删除表
- Python PostgreSQL - 限制
- Python PostgreSQL - 加入
- Python PostgreSQL - 游标对象
- Python SQLite
- Python SQLite - 简介
- Python SQLite - 建立连接
- Python SQLite - 创建表
- Python SQLite - 插入数据
- Python SQLite - 选择数据
- Python SQLite -Where 子句
- Python SQLite - 排序依据
- Python SQLite - 更新表
- Python SQLite - 删除数据
- Python SQLite - 删除表
- Python SQLite - 限制
- Python SQLite - 加入
- Python SQLite - 游标对象
- Python MongoDB
- Python MongoDB - 简介
- Python MongoDB - 创建数据库
- Python MongoDB - 创建集合
- Python MongoDB - 插入文档
- Python MongoDB - 查找
- Python MongoDB - 查询
- Python MongoDB - 排序
- Python MongoDB - 删除文档
- Python MongoDB - 删除集合
- Python MongoDB - 更新
- Python MongoDB - 限制
- Python 数据访问资源
- Python 数据访问 - 快速指南
- Python 数据访问 - 有用的资源
- Python 数据访问 - 讨论
Python SQLite - 更新表
对任何数据库的 UPDATE 操作都意味着修改数据库中已存在的表的一条或多条记录的值。您可以使用 UPDATE 语句更新 SQLite 中现有记录的值。
要更新特定行,您需要同时使用 WHERE 子句。
句法
以下是 SQLite 中 UPDATE 语句的语法 -
UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition];
例子
假设我们使用以下查询创建了一个名为 CRICKETERS 的表 -
sqlite> CREATE TABLE CRICKETERS ( First_Name VARCHAR(255), Last_Name VARCHAR(255), Age int, Place_Of_Birth VARCHAR(255), Country VARCHAR(255) ); sqlite>
如果我们使用 INSERT 语句插入 5 条记录:
sqlite> insert into CRICKETERS values('Shikhar', 'Dhawan', 33, 'Delhi', 'India');
sqlite> insert into CRICKETERS values('Jonathan', 'Trott', 38, 'CapeTown', 'SouthAfrica');
sqlite> insert into CRICKETERS values('Kumara', 'Sangakkara', 41, 'Matale', 'Srilanka');
sqlite> insert into CRICKETERS values('Virat', 'Kohli', 30, 'Delhi', 'India');
sqlite> insert into CRICKETERS values('Rohit', 'Sharma', 32, 'Nagpur', 'India');
sqlite>
以下语句修改了名字为Shikhar的板球运动员的年龄-
sqlite> UPDATE CRICKETERS SET AGE = 45 WHERE FIRST_NAME = 'Shikhar' ; sqlite>
如果您检索 FIRST_NAME 为 Shikhar 的记录,您会发现年龄值已更改为 45 -
sqlite> SELECT * FROM CRICKETERS WHERE FIRST_NAME = 'Shikhar'; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- ------------- Shikhar Dhawan 45 Delhi India sqlite>
如果没有使用WHERE子句,所有记录的值都会被更新。以下 UPDATE 语句将 CRICKETERS 表中所有记录的寿命增加 1 -
sqlite> UPDATE CRICKETERS SET AGE = AGE+1; sqlite>
如果您使用 SELECT 命令检索表的内容,您可以看到更新的值如下:
sqlite> SELECT * FROM CRICKETERS; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- ------------- Shikhar Dhawan 46 Delhi India Jonathan Trott 39 CapeTown SouthAfrica Kumara Sangakkara 42 Matale Srilanka Virat Kohli 31 Delhi India Rohit Sharma 33 Nagpur India sqlite>
使用 python 更新现有记录
将记录添加到 SQLite 数据库中的现有表 -
导入sqlite3包。
通过将数据库名称作为参数传递给它,使用connect()方法创建连接对象。
Cursor ()方法返回一个游标对象,您可以使用它与 SQLite3 进行通信。通过在(上面创建的)Connection 对象上调用cursor() 对象来创建游标对象。
然后,通过将 UPDATE 语句作为参数传递给游标对象,调用它的execute() 方法。
例子
以下 Python 示例创建一个名为 EMPLOYEE 的表,向其中插入 5 条记录,并将所有男性员工的年龄增加 1 -
import sqlite3
#Connecting to sqlite
conn = sqlite3.connect('example.db')
#Creating a cursor object using the cursor() method
cursor = conn.cursor()
#Doping EMPLOYEE table if already exists.
cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
#Creating table as per requirement
sql ='''CREATE TABLE EMPLOYEE(
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT
)'''
cursor.execute(sql)
#Inserting data
cursor.execute('''INSERT INTO EMPLOYEE
(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES
('Ramya', 'Rama priya', 27, 'F', 9000),
('Vinay', 'Battacharya', 20, 'M', 6000),
('Sharukh', 'Sheik', 25, 'M', 8300),
('Sarmista', 'Sharma', 26, 'F', 10000),
('Tripthi', 'Mishra', 24, 'F', 6000)''')
conn.commit()
#Fetching all the rows before the update
print("Contents of the Employee table: ")
cursor.execute('''SELECT * from EMPLOYEE''')
print(cursor.fetchall())
#Updating the records
sql = '''UPDATE EMPLOYEE SET AGE=AGE+1 WHERE SEX = 'M' '''
cursor.execute(sql)
print("Table updated...... ")
#Fetching all the rows after the update
print("Contents of the Employee table after the update operation: ")
cursor.execute('''SELECT * from EMPLOYEE''')
print(cursor.fetchall())
#Commit your changes in the database
conn.commit()
#Closing the connection
conn.close()
输出
Contents of the Employee table:
[('Ramya', 'Rama priya', 27, 'F', 9000.0),
('Vinay', 'Battacharya', 20, 'M', 6000.0),
('Sharukh', 'Sheik', 25, 'M', 8300.0),
('Sarmista', 'Sharma', 26, 'F', 10000.0),
('Tripthi', 'Mishra', 24, 'F', 6000.0)]
Table updated......
Contents of the Employee table after the update operation:
[('Ramya', 'Rama priya', 27, 'F', 9000.0),
('Vinay', 'Battacharya', 21, 'M', 6000.0),
('Sharukh', 'Sheik', 26, 'M', 8300.0),
('Sarmista', 'Sharma', 26, 'F', 10000.0),
('Tripthi', 'Mishra', 24, 'F', 6000.0)]