📜  Python SQLite – 限制条款

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

Python SQLite – 限制条款

在本文中,我们将使用Python讨论 SQLite 中的 LIMIT 子句。但首先,让我们简要介绍一下 LIMIT 子句。

如果有很多元组满足查询条件,一次只查看其中的几个元组可能会很足智多谋。 LIMIT 关键字用于限制 SELECT 语句给出的数据。

让我们创建一个数据库。

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 
# geeks_database
connection = sqlite3.connect('geeks_database.db')
  
# sql query to display top4 data from table
cursor = connection.execute("SELECT * FROM customer_address LIMIT 4")
  
# display data row by row
for i in cursor:
    print(i)
  
# 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()

输出:

添加数据后,让我们执行限制操作。在此示例中,我们将显示表中的前 4 条数据。

蟒蛇3

# importing sqlite module
import sqlite3
  
# create connection to the database 
# geeks_database
connection = sqlite3.connect('geeks_database.db')
  
# sql query to display top4 data from table
cursor = connection.execute("SELECT * FROM customer_address LIMIT 4")
  
# display data row by row
for i in cursor:
    print(i)
  
# close the connection
connection.close()

输出:

通过这种方式,我们可以通过将 LIMIT 设置为 N 来限制输出中的行并打印前 N 行。