I have a database in sqlite, now when inserting into this data, some spaces are defined as null. I need a script that finds nulls and puts the value of the same row but the next column in null to be filled (the value on the right).
in python
enter image description here
# Fetch all data from your table
cursor.execute("SELECT * FROM my_table")
rows = cursor.fetchall()
# Get the column names
column_names = [description[0] for description in cursor.description]
# Loop through each row and update NULL values
for row in rows:
# Prepare the data to be updated
updated_row = list(row)
# Replace NULL values with the value to the right
for i in range(len(updated_row) - 1):
if updated_row[i] is None:
# Find the next non-NULL value to the right
for j in range(i + 1, len(updated_row)):
if updated_row[j] is not None:
updated_row[i] = updated_row[j]
break
# Update the row in the database
update_query = f"UPDATE your_table_name SET {', '.join(f'{col} = ?' for col in column_names)} WHERE rowid = ?"
cursor.execute(update_query, (*updated_row, row[0])) # Assuming the first column is the rowid
but i have This Error:
cursor.execute(update_query, (*updated_row, row[0])) # Assuming the first column is the rowid
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: near "20553975": syntax error