+1 vote

Hi there. So my program needed a database and I used SQLite from the godot assets library. Source: https://godotengine.org/asset-library/asset/444. I used this guide to save the image to the database: https://youtu.be/8gtAZuE8mBU. I saved the image to a successful database. The program worked only for very small size photos. Problem: When I try to save an image larger than 100 kb, the program freezes (very long wait). Please tell me an effective way to save an image to sqlite or a solution to the above problem. This is my diploma work. Thank you for trying to reply

in Engine by (45 points)

1 Answer

0 votes

Looks like your performing IO on the main thread and freezing all the processing till it is complete.
You should try multithreading to make sure you aren't freezing the main thread.

var thread

# The thread will start here.
func _ready():
    thread = Thread.new()

func save_data_to_database_async(data, database):
    if thread != null:
            thread.wait_to_finish()
    thread.start(self, "save_data_to_database", {"data": data, "database": database})

func save_data_to_database(data2):
    var database = data2["database"]
    var data = data2["data"]
    database.save(data)

# Thread must be disposed (or "joined"), for portability.
func _exit_tree():
    thread.wait_to_finish()

As long as you are not saving very often, you shouldn't get many ui hangups.

by (98 points)
edited by

In addition to the above, I'd note that tutorial doesn't seem overly efficient in the first place. For instance, there should be no reason to convert the image content to a string prior to storing it in the DB. You can store the binary image data directly in a SQLite BLOB field, which should be much more efficient and more compact.

I expect the example where each byte is iterated over, converted to a string rep, and then concatenated to a larger string is highly inefficient - especially with larger amounts of data (larger images).

I don't see what you are seeing. It seems like you just need to send the data to the function you want to use asynchronously. There is no need to convert your byte to a string. Please show where your are seeing this?

@codelyok13 - my comments aren't related to your above threading example. Rather, they are in regard to the video tutorial linked in the original question posted by @Gollum. The image storage code shown in that video seems very inefficient. Sorry for the confusion.

Ok.
Your right, it is weird that you have to convert an image to a string.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.