Problem with SQLite database in Unity

Hello everyone, I am creating a very simple 2d platform game in which there is only one level and you have to catch 5 bombs scattered around the stage to complete it.
I would like to save the score, remaining lives, fruits obtained and some more data in a sqlite database.

In the game everything works fine, but when you have to save the data at the end of the game does not do it, only the tables are created, but does not save the records, someone can help me?

I attach the code used for the file that creates the database. thank you very much in advance!

But… why? That’s is way overkill for something that could be encapsulated in one or two plain C# classes and JSON-ed out.

1 Like

Thanks for answering! It’s for a class project, they ask us to use SQLite databases.

command.CommandText = “CREATE TABLE IF NOT EXISTS Games(Lives_Remaining INT, Score INT, Bombs_Lotted INT, Cherries_Collected INT, Bananas_Collected INT, Apples_Collected INT);”;

-If I add this below:
command.CommandText = “CREATE TABLE IF NOT EXISTS Fruits(Fruits_Collected INT, Cherries_Collected INT, Bananas_Collected INT, Apples_Collected INT);”;

If I do this, only one table appears in the file generated by the database. Only the second table appears, the fruits table, the games table does not appear…

I know that you can put everything in a table, but I want to know how to make them appear correctly.

The following photo shows the changes I mentioned above.
I am also attaching the database file that it creates for me when I run the game.

But I also can’t get the records to be saved in any of the ways to the file
“URI=file:GameDataBase.db”
Thank you!


8629209–1159845–BaseDatosJuegos.rar (279 Bytes)

you need to force flash (close database with writing of journals transaction- sqlite not store to file immediately, just when it decide to do so.)

you could look at SQLiteKit | Integration | Unity Asset Store for your next projects

It makes no sense that you change the command text twice in a row. The usual procedure is to:

  • create a command
  • set the command text
  • Call one of the Execute methods to actually execute the query.

Changing the command text before you called Execute has no effect. So only the last query would apply. The one that was set when you called ExecuteNonQuery. Seperate queries should use seperate commands. As expected your DB file only contains the “Frutas” table. So you never create that “Partidas” table as you never executed that command. Likewise, since the table does not exist, the insert query would fail.