Speed problem with Replit Database

I have a speed problem while writing things to the replit database, so I want to change my code to write to a txt file, then ADD whats in that file into the database, then remove whats in the txt file and do it all again.
Note: I can’t make it go all in a txt file due to the fact it EATS my ram and cpu, I also want this to happen with the conversion from hexadecimal to decimal (change.py)

Repl link:

with open("pixel_data.txt", "w") as file:
      # Initialize hex_value based on image mode
      if img.mode == "RGB":
        hex_value = "{:02X}{:02X}{:02X}00"
      elif img.mode == "RGBA":
        hex_value = "{:02X}{:02X}{:02X}{:02X}"

    # Iterate through the pixels and write the pixel data to the Replit Database
    for y in range(height):
      for x in range(width):
        # Get the RGBA values of the pixel
        pixel = img.getpixel((x, y))

        # Separate the pixel values based on the image mode
        if img.mode == "RGB":
          r, g, b = pixel
          db["pixel_data"] += hex_value.format(r, g, b) + " "
        elif img.mode == "RGBA":
          r, g, b, a = pixel
          db["pixel_data"] += hex_value.format(r, g, b, a) + " "
    print("Pixel data saved to pixel_data DataBase")