📜  Python SQLite – 删除表

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

Python SQLite – 删除表

在本文中,我们将使用Python讨论 SQLite 中的 DROP 命令。但首先,让我们简要介绍一下 drop 命令。

DROP用于删除整个数据库或表。它删除了表中的两条记录以及表结构。

对于删除表,我们将首先创建一个数据库和一个表。让我们在数据库中创建一个表。

Python3
# importing sqlite module
import sqlite3
  
# create connection to the database 
# geeks_database
connection = sqlite3.connect('geeks_database.db')
  
# create table named address of customers with
# 4 columns id,name age and address
connection.execute('''CREATE TABLE customer_address
         (ID INT PRIMARY KEY     NOT NULL,
         NAME           TEXT    NOT NULL,
         AGE            INT     NOT NULL,
         ADDRESS        CHAR(50)); ''')
  
# close the connection
connection.close()


Python3
# importing sqlite module
import sqlite3
  
# create connection to the database 
# geeks_database
connection = sqlite3.connect('geeks_database.db')
  
# insert records into table
connection.execute(
    "INSERT INTO customer_address VALUES (1, 'nikhil teja', 22, 'hyderabad' )")
  
connection.execute(
    "INSERT INTO customer_address VALUES (2, 'karthik', 25, 'khammam')")
  
connection.execute(
    "INSERT INTO customer_address VALUES (3, 'sravan', 22, 'ponnur' )")
  
connection.execute(
    "INSERT INTO customer_address VALUES (4, 'deepika', 25, 'chebrolu' )")
  
connection.execute(
    "INSERT INTO customer_address VALUES (5, 'jyothika', 22, 'noida')")
  
# close the connection
connection.close()


Python3
# importing sqlite module
import sqlite3
  
# create connection to the 
# database geek
connection = sqlite3.connect('geeks_database.db')
  
# drop table
connection.execute("DROP TABLE customers_address")
  
print("data dropped successfully")
  
# close the connection
connection.close()


输出:

现在,将 5 条记录插入到 customer_address 表中。

蟒蛇3

# importing sqlite module
import sqlite3
  
# create connection to the database 
# geeks_database
connection = sqlite3.connect('geeks_database.db')
  
# insert records into table
connection.execute(
    "INSERT INTO customer_address VALUES (1, 'nikhil teja', 22, 'hyderabad' )")
  
connection.execute(
    "INSERT INTO customer_address VALUES (2, 'karthik', 25, 'khammam')")
  
connection.execute(
    "INSERT INTO customer_address VALUES (3, 'sravan', 22, 'ponnur' )")
  
connection.execute(
    "INSERT INTO customer_address VALUES (4, 'deepika', 25, 'chebrolu' )")
  
connection.execute(
    "INSERT INTO customer_address VALUES (5, 'jyothika', 22, 'noida')")
  
# close the connection
connection.close()

输出:

插入后我们看看如何执行drop table命令。

蟒蛇3

# importing sqlite module
import sqlite3
  
# create connection to the 
# database geek
connection = sqlite3.connect('geeks_database.db')
  
# drop table
connection.execute("DROP TABLE customers_address")
  
print("data dropped successfully")
  
# close the connection
connection.close()

输出: