📜  Python MySQL - 如果表中不存在则插入记录

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

Python MySQL - 如果表中不存在则插入记录

先决条件:将 MySQL 数据库连接到Python

在本文中,我们将尝试插入记录并检查它是否存在。 SQL 中的 EXISTS 条件用于检查相关嵌套查询的结果是否为空(不包含元组)。它可用于INSERTSELECT 、 UPDATEDELETE语句。

我们使用Python程序的查询是:

假设我们有一个名为test的数据库和一个名为geeksfoegeeks的表。以下是表的架构和数据:

数据库

由于每个人都有一个唯一的标识号,我们将插入记录并检查该人的ID_NO 。如果表中不存在ID_NO ,则将插入该记录,否则该记录将被丢弃。让我们通过下面的例子来理解:

示例 1:当表中不存在记录时。

Python3
# import required modules
import pymysql
  
  
# establish connection to MySQL
connection = pymysql.connect(
    # specify host name
    
    host="localhost",
      
    # specify username
    user="root",
      
    # enter password for above user
    password="1234",
      
    # default port number for MySQL is 3306
    port=3306,
      
    # specify database name
    db="test"
)
  
  
# make cursor for establish connection
mycursor = connection.cursor()
  
  
# display records before inserting
mycursor.execute("Select * from geeksfoegeeks")
myresult = mycursor.fetchall()
for i in myresult:
    print(i)
  
      
# statement to insert record
mycursor.execute(
    "Insert into geeksfoegeeks(name,address,age,mob_number,ID_NO) \
    select * from( Select 'Thomas','UK',30,1892345670,'IND100') as temp \
    where not exists \
    (Select ID_NO from geeksfoegeeks where ID_NO='IND100') LIMIT 1")
print("After inserting a record....")
  
  
# print records after insertion
mycursor.execute("Select * from geeksfoegeeks")
myresult = mycursor.fetchall()
for i in myresult:
    print(i)
mycursor.execute("Commit")
  
  
# close connection
connection.close()


Python3
# import required modules
import pymysql
  
  
# establish connection to MySQL
connection = pymysql.connect(
    # specify host name
    host="localhost",
     
    # specify username
    user="root",
     
    # enter password for above user
    password="",
      
    # default port number for MySQL is 3306
    port=3306,
      
    # specify database name
    db="geek"
)
  
  
# make cursor for establish connection
mycursor = connection.cursor()
  
  
# display records before inserting
mycursor.execute("Select * from geeksdemo")
myresult = mycursor.fetchall()
for i in myresult:
    print(i)
      
      
# statement to insert record
mycursor.execute(
    "Insert into geeksdemo(id,name,gender,dept) \
    select * from( Select 5,'Thomas','m','information technology') as temp \
    where not exists \
    (Select id from geeksdemo where id=5) LIMIT 1")
print("After inserting a record....")
  
  
# print records after insertion
mycursor.execute("Select * from geeksdemo")
  
  
myresult = mycursor.fetchall()
for i in myresult:
    print(i)
mycursor.execute("Commit")
  
  
# close connection
connection.close()


输出

插入后的Python输出

在上面的输出中,我们可以看到记录('Thomas','UK',30,1892345670,'IND100')被插入到 MySQL 表中。

示例 2:当记录已存在时

以下是geek数据库中geeksdemo表的schema和数据

现在,我们将尝试插入一个已经存在的记录。

蟒蛇3

# import required modules
import pymysql
  
  
# establish connection to MySQL
connection = pymysql.connect(
    # specify host name
    host="localhost",
     
    # specify username
    user="root",
     
    # enter password for above user
    password="",
      
    # default port number for MySQL is 3306
    port=3306,
      
    # specify database name
    db="geek"
)
  
  
# make cursor for establish connection
mycursor = connection.cursor()
  
  
# display records before inserting
mycursor.execute("Select * from geeksdemo")
myresult = mycursor.fetchall()
for i in myresult:
    print(i)
      
      
# statement to insert record
mycursor.execute(
    "Insert into geeksdemo(id,name,gender,dept) \
    select * from( Select 5,'Thomas','m','information technology') as temp \
    where not exists \
    (Select id from geeksdemo where id=5) LIMIT 1")
print("After inserting a record....")
  
  
# print records after insertion
mycursor.execute("Select * from geeksdemo")
  
  
myresult = mycursor.fetchall()
for i in myresult:
    print(i)
mycursor.execute("Commit")
  
  
# close connection
connection.close()

输出

我们观察到记录(5,'Thomas','m','information technology')没有再次插入到表中。下图是运行上述Python脚本后MySQL数据库的输出。

我们可以看到有 0 行受到影响。因此,没有插入任何记录,因为它已经存在于带有另一条记录的表中。