Skip to content

Commit

Permalink
Port persistency example from Godot 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
2shady4u committed Mar 12, 2023
1 parent 2d99cb9 commit 43b02f5
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions demo/database.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const verbosity_level : int = SQLite.VERBOSE

var db_name := "res://data/test"
var packaged_db_name := "res://data_to_be_packaged"
var peristent_db_name := "user://my_database"
var json_name := "res://data/test_backup"

var table_name := "company"
Expand All @@ -32,6 +33,7 @@ func _ready():
example_of_call_external_functions()
example_of_blob_io()
example_of_read_only_database()
example_of_database_persistency()

func cprint(text : String) -> void:
print(text)
Expand Down Expand Up @@ -379,3 +381,33 @@ func example_of_read_only_database():

# Close the current database
db.close_db()

func example_of_database_persistency():
var table_dict : Dictionary = Dictionary()
table_dict["id"] = {"data_type":"int", "primary_key": true, "not_null": true}
table_dict["count"] = {"data_type":"int", "not_null": true, "default": 0}

db = SQLite.new()
db.path = peristent_db_name
db.verbosity_level = verbosity_level
db.open_db()
db.create_table(table_name, table_dict)

# Does the row already exist?
db.select_rows(table_name, "id = 1", ["count"])
var query_result : Array = db.query_result
var count : int = 0
if query_result.is_empty():
# It doesn't exist yet! Add it!
db.insert_row(table_name, {"id": 1, "count": 0})
else:
var result : Dictionary = query_result[0]
count = int(result.get("count", count))

cprint("Count is: {0}".format([count]))

# Increment the value for the next time!
db.update_rows(table_name, "id = 1", {"count": count + 1 })

# Close the current database
db.close_db()

0 comments on commit 43b02f5

Please sign in to comment.