Hướng dẫn python if query is none - python nếu truy vấn không có

Tôi đã có một vấn đề tương tự khi tôi cần thực hiện nhiều truy vấn SQL. Vấn đề là một số truy vấn đã không trả về kết quả và tôi muốn in kết quả đó. Và đã có một sai lầm. Như đã viết, có một số giải pháp.

if cursor.description is None:
    # No recordset for INSERT, UPDATE, CREATE, etc
    pass
else:
    # Recordset for SELECT

Cũng như:

exist = cursor.fetchone[]
if exist is None:
  ... # does not exist
else:
  ... # exists

Một trong những giải pháp là:

Khối

# Using a while loop
cursor.execute["SELECT * FROM employees"]
row = cursor.fetchone[]
while row is not None:
  print[row]
  row = cursor.fetchone[]

# Using the cursor as iterator
cursor.execute["SELECT * FROM employees"]
for row in cursor:
  print[row]
1 và
# Using a while loop
cursor.execute["SELECT * FROM employees"]
row = cursor.fetchone[]
while row is not None:
  print[row]
  row = cursor.fetchone[]

# Using the cursor as iterator
cursor.execute["SELECT * FROM employees"]
for row in cursor:
  print[row]
2 cho phép bạn xử lý ________ 53/________ 54. Khối
# Using a while loop
cursor.execute["SELECT * FROM employees"]
row = cursor.fetchone[]
while row is not None:
  print[row]
  row = cursor.fetchone[]

# Using the cursor as iterator
cursor.execute["SELECT * FROM employees"]
for row in cursor:
  print[row]
5 cho phép bạn thực thi mã, bất kể kết quả của các khối
# Using a while loop
cursor.execute["SELECT * FROM employees"]
row = cursor.fetchone[]
while row is not None:
  print[row]
  row = cursor.fetchone[]

# Using the cursor as iterator
cursor.execute["SELECT * FROM employees"]
for row in cursor:
  print[row]
1 và
# Using a while loop
cursor.execute["SELECT * FROM employees"]
row = cursor.fetchone[]
while row is not None:
  print[row]
  row = cursor.fetchone[]

# Using the cursor as iterator
cursor.execute["SELECT * FROM employees"]
for row in cursor:
  print[row]
2. Vì vậy, vấn đề được trình bày có thể được giải quyết bằng cách sử dụng nó.

s = """ set current query acceleration = enable;
        set current GET_ACCEL_ARCHIVE = yes;
        SELECT * FROM TABLE_NAME;"""

query_sqls = [i.strip[] + ";" for i in filter[None, s.split[';']]]
for sql in query_sqls:
    print[f"Executing SQL statements ====> {sql}  {sql}  {sql}  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  {sql}  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  {sql}  {sql}  {sql}  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  set current GET_ACCEL_ARCHIVE = yes;  chọn * từ cộtValuenULLDEMO trong đó cộtName là null hoặc cộtName = '';Sau khi thực hiện truy vấn trên, đầu ra thu được là.Đầu ra này được lấy là điều kiện thứ hai là đúng đối với giá trị trống.mysql> SELECT * FROM ColumnValueNullDemo WHERE ColumnName IS NULL OR ColumnName = ' '; After executing the above query, the output obtained is. This output was obtained as the second condition is true for empty value.

Fetchone [] trong Python là gì?

fetchone [] Phương thức này lấy hàng tiếp theo của bộ kết quả truy vấn và trả về một chuỗi duy nhất hoặc không có nếu không có thêm hàng.Theo mặc định, bộ tuple được trả về bao gồm dữ liệu được trả về bởi máy chủ MySQL, được chuyển đổi thành các đối tượng Python.retrieves the next row of a query result set and returns a single sequence, or None if no more rows are available. By default, the returned tuple consists of data returned by the MySQL server, converted to Python objects.

Bài Viết Liên Quan

Chủ Đề