0 votes

I am using an SQLite plugin for Godot so I'm not sure if this is the correct place to ask this but I have no idea why I am getting this error. Here is the script for more details.

extends Control

const SQLite = preload("res://lib/gdsqlite.gdns");

func onButton_pressed():
# Create gdsqlite instance
var db = SQLite.new();

# Open database
if (!db.open_db("res://ShapeBase.db")):
    print("Cannot open database.");

var usernametext = get_node("MarginContainer/VBoxContainer/HBoxContainer/Username").get_username()

var passwordtext = get_node("MarginContainer/VBoxContainer/HBoxContainer2/Password").get_password()

db.query("CREATE TABLE IF NOT EXISTS user (userid INTEGER PRIMARY KEY, username TEXT, password TEXT, dateofbirth TEXT, emailaddress TEXT, numberofsimulations INTEGER);");

while usernametext != null:
    db.query("INSERT INTO user (username) VALUES (?);"[usernametext]);
asked Aug 8 in Projects by Excel (20 points)

1 Answer

+1 vote
Best answer

Which line is causing the error?
Let's try guessing...

db.query("INSERT INTO user (username) VALUES (?);"[usernametext]);

Simplified, this code looks like this:

"a string"[an_array_index]

Which could be legit, because strings can be indexed using integers and return the character at that index. But here that's not the case.
Maybe your usernametext contains "x". So Godot is telling you, it cannot index that string using such string index.

answered Aug 8 by Zylann (18,381 points)
selected Aug 10 by Excel

Ah, thank you had no idea I was trying to index an array there, I may have created an array on accident. I am just trying to insert the value contained by the variable usernametext into the database. Could you provide some guidance on how I might do this?

Was that array indexing really in your code? Because it looks plain wrong. I guess you want this:

db.query(str("INSERT INTO user (username) VALUES (\"", usernametext, "\");");

Other than that, I see nowhere else in your code where the error could be.

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.