• Building
  • How to include SQLite database (*.db) in export

Hi, I've recently switched my data for my game over to a SQLite database. When I run the game in the editor it runs fine. Here's the code:

const SQLite = preload("res://addons/godot-sqlite/bin/gdsqlite.gdns")

var db

func _ready():
	db = SQLite.new()
	db.path = Constants.DATABASE_PATH # "res://data/data.db"
	# db.verbose_mode = true
	db.open_db()

However, when I now export the game and run the .exe file I get the following error:

ERROR: bool __cdecl godot::SQLite::open_db(void): GDSQLite Error: Can't open database: unable to open database file At: src\gdsqlite.cpp:123

I'm guessing that my .db file is not by default included in exporting? I've added ".db" to the export settings (see below) but it doesn't make a difference:

What do I need to do, or how can I check if the data.db file is included in the exe file? Alternaively, what's the preferred way to include SQLite database files?

Are you using this: https://godotengine.org/asset-library/asset/444 ?

I saw some notes in its documentation about exports not being to access a database directly from res://. I don't know if that applies to your platform. For example:

NOTE: On mobile platforms (Android & iOS) and for web builds, the method discussed here is not possible and the contents of the res://data/-folder have to be copied to the user://-folder in its entirety instead (see FAQ above). https://github.com/2shady4u/godot-sqlite

Hi. Yes it is this library. Actually, it's windows platform I'm exporting to. Is it possible to write the database to the user dir when the user first runs the game? I should probably try running the database from there first, but not even sure if the database is included in the exe file to do so.

I think if you export the game as a ZIP file, then you can see if the database is included in the export by looking at the zip file. If it exists in res://, you could try something like this to move it to user://, though I'm not sure if it would work:

var original_sql_file = File;
original_sql_file.open("res://sql_database.db", File.READ)
var original_sql_file_buffer = original_sql_file.get_buffer(original_sql_file.get_len())
original_sql_file.close()

var new_sql_file = File;
new_sql_file.open("user://sql_database.db", File.WRITE)
new_sql_file.store_buffer(original_sql_file_buffer)
new_sql_file.close()

You could also, potentially, have the database included as a download alongside the executable. Then you could use something like this to get the path to it:

var executable_folder = OS.get_executable_path.get_base_dir()
var sql_database = executable_folder + "/sql_database.db"
# then you should be able to load it from the file path

However, this has the downside of needing to have the database included in the download next to the executable, which may be undesired and I'm not sure if it would work on Android or iOS.

8 days later

Hi, I tried exporting the project as a ZIP but the folder/file 'data/data.db' isn't there. Have I configure the export settings properly to include it?

Is it not sometimes common to use a database in games development? Seems strange that I can't manage to include it in the export.

@martynbiz said: Hi, I tried exporting the project as a ZIP but the folder/file 'data/data.db' isn't there. Have I configure the export settings properly to include it?

Hmm, strange. Maybe it's not exporting it then, though the screenshot you showed does have the file extension in, so I'm not sure why it wouldn't be exporting it. Not that I think this is the case, but is it possible the extension is .DB instead of .db? I doubt this is the case, but it's the only thing I can think of right off.

I'm not sure if this is related at all, but if it comes up in the Godot editor at all, you may be able to set it's import settings to Keep File, which according to what I can gather from this issue, may help? I think this only applies to files Godot normally can support though.

Is it not sometimes common to use a database in games development? Seems strange that I can't manage to include it in the export.

It is somewhat common, from my understanding. However, normally just including the file extension in the export settings works, so I'm not sure why it is not working in this case.

Got it working with this code:

# must have file extension (.db) for the copy to work
const DATABASE_PATH_RES = "res://data/data.db"
const DATABASE_PATH = "user://data.db"

# Check if file exists, move it to user path if not 
var dir = Directory.new();
if !dir.file_exists(DATABASE_PATH):
	dir.copy(DATABASE_PATH_RES, DATABASE_PATH);
	print("Copied db file to users dir")

# connect to db 
db = SQLite.new()
db.path = DATABASE_PATH
db.open_db()