📜  如何使用Python更新 SQLite 表特定列的所有值?

📅  最后修改于: 2022-05-13 01:54:54.701000             🧑  作者: Mango

如何使用Python更新 SQLite 表特定列的所有值?

在本文中,我们将使用Python更新给定 SQLite 表的特定列的所有值。为了更新 SQL 中特定表的所有列,我们使用 UPDATE 查询。 SQL 中的 UPDATE 语句用于更新数据库中现有表的数据。我们可以根据我们的要求使用 UPDATE 语句更新单列和多列。

句法:

我们将创建一个表,然后在其中执行更新操作。



Python3
# importing sqlite module
import sqlite3
  
# create connection to the database my_database
connection = sqlite3.connect('my_database.db')
  
# create table named address of customers 
# with 4 columns id,name age and address
connection.execute('''CREATE TABLE ship (ship_id INT, ship_name \
TEXT NOT NULL, ship_destination CHAR(50) NOT NULL); ''')
  
print("Ship table created successfully")
  
# close the connection
connection.close()


Python3
# import sqlite module database
import sqlite3
  
# create connection to the database
# my_database
connection = sqlite3.connect('my_database.db')
  
# insert query to insert values
connection.execute("INSERT INTO ship  VALUES (1, 'tata-hitachi','noida' )")
connection.execute("INSERT INTO ship  VALUES (2, 'tata-mumbai','mumbai' )")
connection.execute("INSERT INTO ship  VALUES (3, 'tata-express','hyderabad' )")
  
# query to display all data in the table
cursor = connection.execute("SELECT * from ship")
print("before updation")
  
# display row by row
for row in cursor:
    print(row)
  
# query to update all data in ship_name 
# column to manoji
connection.execute("UPDATE ship set ship_name='manoji'")
  
print("After  updation")
  
# display row by row
cursor = connection.execute("SELECT * from ship")
for row in cursor:
    print(row)
  
# close the connection
connection.close()


Python3
# import sqlite module database
import sqlite3
  
# create connection to the database 
# my_database
connection = sqlite3.connect('my_database.db')
  
# insert query to insert values
connection.execute("INSERT INTO ship  VALUES (1, 'tata-hitachi','noida' )")
connection.execute("INSERT INTO ship  VALUES (2, 'tata-mumbai','mumbai' )")
connection.execute("INSERT INTO ship  VALUES (3, 'tata-express','hyderabad' )")
  
# query to display all data in the table
cursor = connection.execute("SELECT * from ship")
print("before updation of ship address")
  
# display row by row
for row in cursor:
    print(row)
  
# query to update all data in  ship_address
connection.execute("UPDATE ship set ship_destination='delhi'")
  
print("After  updation of ship address")
  
# display row by row
cursor = connection.execute("SELECT * from ship")
for row in cursor:
    print(row)
  
# close the connection
connection.close()


输出:

Ship table created successfully

示例 1:

用于插入记录和执行更新查询的Python程序。在这里,我们将 ship_name 列中的所有数据更新为 manoji。

蟒蛇3

# import sqlite module database
import sqlite3
  
# create connection to the database
# my_database
connection = sqlite3.connect('my_database.db')
  
# insert query to insert values
connection.execute("INSERT INTO ship  VALUES (1, 'tata-hitachi','noida' )")
connection.execute("INSERT INTO ship  VALUES (2, 'tata-mumbai','mumbai' )")
connection.execute("INSERT INTO ship  VALUES (3, 'tata-express','hyderabad' )")
  
# query to display all data in the table
cursor = connection.execute("SELECT * from ship")
print("before updation")
  
# display row by row
for row in cursor:
    print(row)
  
# query to update all data in ship_name 
# column to manoji
connection.execute("UPDATE ship set ship_name='manoji'")
  
print("After  updation")
  
# display row by row
cursor = connection.execute("SELECT * from ship")
for row in cursor:
    print(row)
  
# close the connection
connection.close()

输出:

示例 2:

在这个程序中,我们首先插入数据,然后将ship_address中的所有数据更新到同一张表中的Delhi。

蟒蛇3

# import sqlite module database
import sqlite3
  
# create connection to the database 
# my_database
connection = sqlite3.connect('my_database.db')
  
# insert query to insert values
connection.execute("INSERT INTO ship  VALUES (1, 'tata-hitachi','noida' )")
connection.execute("INSERT INTO ship  VALUES (2, 'tata-mumbai','mumbai' )")
connection.execute("INSERT INTO ship  VALUES (3, 'tata-express','hyderabad' )")
  
# query to display all data in the table
cursor = connection.execute("SELECT * from ship")
print("before updation of ship address")
  
# display row by row
for row in cursor:
    print(row)
  
# query to update all data in  ship_address
connection.execute("UPDATE ship set ship_destination='delhi'")
  
print("After  updation of ship address")
  
# display row by row
cursor = connection.execute("SELECT * from ship")
for row in cursor:
    print(row)
  
# close the connection
connection.close()

输出: