Created
August 6, 2024 22:55
-
-
Save hernandohhoyos/66ada531bebba157b59a512c8bb33524 to your computer and use it in GitHub Desktop.
Base de datos con SQLite en Godot 4.2.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Control | |
# Referencias a los nodos ItemList y Button | |
@onready var item_list = $ItemList | |
@onready var add_button = $Button | |
var db : SQLite # usando el addon godot-sqlite | |
# Called when the node enters the scene tree for the first time. | |
func _ready(): | |
add_button.connect("pressed", _on_Button_pressed) | |
db = SQLite.new() | |
db.path = "res://data/test" | |
if not db.open_db(): | |
print("Failed to open database!") | |
return | |
var table_dict : Dictionary = Dictionary() | |
table_dict["description"] = {"data_type":"text", "primary_key": true, "not_null": true} | |
var create_table_result = db.create_table("tasks", table_dict) | |
if not create_table_result: | |
print("Failed to create table!") | |
return | |
get_tasks() | |
func add_task(description: String): | |
var row_dict : Dictionary = Dictionary() | |
row_dict["description"] = description | |
var result = db.insert_row("tasks", row_dict) | |
row_dict.clear() | |
if not result: | |
print("Failed to add task!") | |
func get_tasks(): | |
var existing_items = [] | |
for i in range(item_list.get_item_count()): | |
existing_items.append(item_list.get_item_text(i)) | |
var result = db.select_rows( | |
"tasks", | |
"where description no in ({})".format(existing_items) if existing_items else "", | |
["description"] | |
) | |
if result: | |
for row in result: | |
item_list.add_item(row["description"]) | |
func _exit_tree(): | |
db.close() | |
func _on_Button_pressed(): | |
add_task($LineEdit.text) | |
get_tasks() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment