Getting the error "Invalid get index 'x' (on base: 'String')."

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Excel

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 _on_Button_pressed():
# Create gdsqlite instance
var db = SQLite.new();

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

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

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

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]);
:bust_in_silhouette: Reply From: Zylann

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.

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?

Excel | 2019-08-09 18:28

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.

Zylann | 2019-08-09 18:32