Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Problem with SQLite database in Unity

Discussion in 'Scripting' started by JL00000, Dec 2, 2022.

  1. JL00000

    JL00000

    Joined:
    Apr 18, 2022
    Posts:
    3
    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!

    gestor.jpg
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,917
    But... why? That's is way overkill for something that could be encapsulated in one or two plain C# classes and JSON-ed out.
     
    All_American likes this.
  3. JL00000

    JL00000

    Joined:
    Apr 18, 2022
    Posts:
    3
    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!
     

    Attached Files:

  4. Oksana-Iashchuk

    Oksana-Iashchuk

    Joined:
    Sep 10, 2012
    Posts:
    126
    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 https://u3d.as/3ka for your next projects
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,539
    It makes no sense that you change the command text twice in a row. The usual procedure is to:
    1. create a command
    2. set the command text
    3. 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.