Case where python sqlite3 judges whether the result of cursor is empty

  • 2021-10-11 18:55:58
  • OfStack

len (list (cursor) is null if it is 0

Examples:


import sqlite3
 
# CREATE
 db = sqlite3.connect("E:/Pycharm/workspace/django_test/user.db")
 db.execute("create table user(info primary key,flag,result )")
 db.execute("insert into user(info,flag,result) values('RikkaTakanashi','0','DarkFlameMaster')")
 db.commit()
 db.close()
db = sqlite3.connect("E:/Pycharm/workspace/django_test/user.db")
c=db.cursor()
cursor=c.execute("select info,flag,result from user where info='123'")
print(len(list(cursor)))

The result is 0


db = sqlite3.connect("E:/Pycharm/workspace/django_test/user.db")
c=db.cursor()
cursor=c.execute("select info,flag,result from user where info='RikkaTakanashi'")
print(len(list(cursor)))

The result was 1

Additional: SQLite cursor in python (cursor)

We know that relational database management system is essentially set-oriented, and there is no one expression to describe a single record in a table in Sqlite, unless the where clause is used to limit only one record to be selected. Therefore, we must use cursors to process data for a single record. Thus, the cursor allows the application to perform the same or different operations on every 1 row in the result set returned by the query statement select, instead of performing the same operation on the whole result set at one time; It also provides the ability to delete or update the data in the table based on the cursor position; It is cursors that connect the collection-oriented database management system with row-oriented programming, enabling the two data processing methods to communicate.

A cursor provides a flexible means of manipulating data retrieved from a table. In essence, a cursor is actually a mechanism that can extract one record at a time from a result set that includes multiple data records. The cursor is always associated with 1 SQL selection statement. Because cursors consist of a result set (which can be zero, one, or multiple records retrieved by a related selection statement) and the cursor position in the result set pointing to a specific record. When you decide to process a result set, you must declare a cursor pointing to the result set. If you have ever written a program to process a file in C, the cursor is like the file handle you get when you open the file, which can represent the file as long as the file is successfully opened. For cursors, the truth is the same. It can be seen that cursors can process the result set from the underlying table in a similar way as traditional programs read flat files, thus presenting the data in the table to the program in the form of flat files.

When using SQLite of Python, as long as you know Connection and Cursor objects clearly, remember to close them after opening them, and you can use them with confidence.

When using Cursor object to execute insert, update and delete statements, the execution result can be obtained by returning the number of rows affected by rowcount.

When an select statement is executed using an Cursor object, the full data result set of select is returned through featchall (). The result set is one list, and each element is one tuple, corresponding to one row of records, arranged in the order of the fields in which the table is built. fetchone () returns one result, which is one tuple with one field value per element.

It should be noted that the SQLite cursor is stateful, can only traverse the result set once, cannot return movement in the result set, and returns a null value at the end of traversal. featchall () returns all results once, fetchone () returns one result at a time in sequence, and when len () is performed on fetchone () to obtain the number of records obtained, it is equivalent to completing one traversal of the result set, and the result set will be emptied. Therefore, after obtaining the result set, if you need to take one step further on the result set, you can save the result set to a variable.


while cn.fetchone():
 print(cn.fetchone())

for line in cn.featchall():
 print(line)

Related articles: