In that first example you showed you’re passing back a value from the setUpDatabase() function, which means this line:
setUpDatabase("data.sqlite")
Should instead be like this:
db = setUpDatabase("data.sqlite")
Without that the db variable is never getting set so when you try to use it bad things happen.
Jay