📜  Python MySQL – 删除查询

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

Python MySQL – 删除查询

Python数据库 API(应用程序接口)是标准Python的数据库接口。大多数Python数据库接口都遵守此标准。 Python数据库支持各种数据库服务器,如 MySQL、GadFly、PostgreSQL、Microsoft SQL Server 2000、Informix、Interbase、Oracle、Sybase 等。要从Python连接 MySQL 数据库服务器,我们需要导入mysql.connector接口.

下面是一个连接 MySQL 数据库geeks的程序。

# importing required library 
import mysql.connector
  
# connecting to the database 
dataBase = mysql.connector.connect(
                     host = "localhost",
                     user = "user",
                     passwd = "pswrd",
                     database = "geeks" ) 
  
# preparing a cursor object 
cursorObject = dataBase.cursor() 
  
  
# disconnecting from server
dataBase.close() 

上面的程序说明了与 MySQL 数据库geeks的连接,其中主机名是localhost ,用户名是user ,密码是pswrd

从表中删除查询

与 MySQL 中的数据库连接后,我们可以在其中创建表并对其进行操作。

语法声明:

DELETE FROM TABLE_NAME WHERE ATTRIBUTE_NAME = ATTRIBUTE_VALUE

示例 1:下面是一个从数据库中的表中删除查询的程序。

# importing required library 
import mysql.connector 
  
# connecting to the database 
dataBase = mysql.connector.connect(
                     host = "localhost",
                     user = "user",
                     passwd = "pswrd",
                     database = "geeks" )  
  
# preparing a cursor object 
cursorObject = dataBase.cursor() 
  
# creating table  
studentRecord = """CREATE TABLE STUDENT ( 
                   NAME  VARCHAR(20) NOT NULL, 
                   BRANCH VARCHAR(50), 
                   ROLL INT NOT NULL,
                   SECTION VARCHAR(5), 
                   AGE INT
                   )"""
  
# table created
cursorObject.execute(studentRecord)  
  
# inserting data into the table
query = "INSERT INTO STUDENT (NAME, BRANCH, ROLL, SECTION, AGE) VALUES (% s, % s)"
  
attrValues = ("Rituraj Saha", "Information Technology", "1706256", "IT-3", "20")
cursorObject.execute(query, attrValues)
  
attrValues = ("Ritam Barik", "Information Technology", "1706254", "IT-3", "21")
cursorObject.execute(query, attrValues)
  
attrValues = ("Rishi Kumar", "Information Technology", "1706253", "IT-3", "21")
cursorObject.execute(query, attrValues)
  
# deleting query
query = "DELETE FROM STUDENT WHERE ROLL = 1706256"
cursorObject.execute(query, attrValues)
  
dataBase.commit()
  
# disconnecting from server
dataBase.close()

输出:

python-mysql-删除

在上面的程序中,创建了一个名为STUDENT的表,具有NAMEBRANCHROLLSECTIONAGE属性。将多个数据插入STUDENT表,然后从具有ROLL属性值1706256的表中删除单个查询。

示例 2:让我们看一下表中查询的另一个示例。

# importing required library 
import mysql.connector
  
# connecting to the database 
dataBase = mysql.connector.connect(
                     host = "localhost",
                     user = "user",
                     passwd = "pswrd",
                     database = "geeks" ) 
  
# preparing a cursor object 
cursorObject = dataBase.cursor() 
  
# drop table if it already exists 
cursorObject.execute("DROP TABLE IF EXISTS PHONE_RECORD")
  
# creating table  
phoneRecord = """CREATE TABLE PHONE_RECORD ( 
                   NAME  VARCHAR(20) NOT NULL, 
                   PHONE VARCHAR(10) NOT NULL
                   )"""
  
# table created
cursorObject.execute(phoneRecord)  
  
# inserting data into the table
query = "INSERT INTO PHONE_RECORD (NAME, PHONE) VALUES (% s, % s)"
attrValues = ("Rituraj Saha", "9163089075")
cursorObject.execute(query, attrValues)
  
# deleting query
query = "DELETE FROM STUDENT WHERE NAME = 'Rituraj Saha'"
cursorObject.execute(query)
  
dataBase.commit()
  
# disconnecting from server
dataBase.close()

输出:

Python-MYSQL-DELETE1

在上面的程序中,在geeks数据库中创建了另一个名为PHONE_RECORD的表,具有NAMEPHONE属性。只有一列被插入到表中,然后使用DELETE语句将其删除。