1

Basically I wrote a getter for the inventory for my game, where I use databank to store the saves (it's a school project and we needed to use databases). I can write the data into the database, but I can not get data from it.

public int[] GetInventoryByID(int ID)
{
    using(var connection = new SqliteConnection(DB_Name))
    {
        connection.Open();

        using(var command = connection.CreateCommand())
        {
            command.CommandText = "SELECT * FROM Inventory WHERE ID = " + ID +" ;";
            //command.ExecuteNonQuery();
            using(IDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    Debug.Log("InventoryEntity");
                        inventorySave = new InventoryEntity(
                        int.Parse(reader[0].ToString()),
                        int.Parse(reader[1].ToString()),
                        int.Parse(reader[2].ToString()),
                        int.Parse(reader[3].ToString()),
                        int.Parse(reader[4].ToString()),
                        int.Parse(reader[5].ToString()),
                        int.Parse(reader[6].ToString())
                    );
                    Debug.Log("Invetory potion Small" + inventorySave.Potion_Small);
                }
                reader.Close();
            }
        }
        connection.Close();
    }
    Debug.Log("Inventory :" +"\n"
    + "Potion_Small :" + inventorySave.Potion_Small +"\n" 
    + "Potion_Middle :" +inventorySave.Potion_Middle +"\n"
    + "Potion_Grand :" +inventorySave.Potion_Grand + "\n"
    + "Pokeball :" +inventorySave.Pokeball + "\n"
    + "Superball :" +inventorySave.Superball + "\n"
    + "Ultraball :" +inventorySave.Ultraball);
    return inventorySave.ReturnInventoryData();
}

This is my getter method:

using UnityEngine;
using System.Data;
using Mono.Data.Sqlite;

    void LoadGamestate(int number)
    {
        f = db.GetInventoryByID(0);
        PlayerPrefs.SetInt("LittlePotion", f[0]);
        PlayerPrefs.SetInt("MediumPotion", f[1]);
        PlayerPrefs.SetInt("HyperPotion", f[2]);
        PlayerPrefs.SetInt("Pokeball", f[3]);
        PlayerPrefs.SetInt("Superball", f[4]);
        PlayerPrefs.SetInt("Hyperball", f[5]);
    }

This is the error I get:

My error message

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Grilya
  • 11
  • 1
  • thank you very much, I was too concentrated on my code, that I forgot, that a new save isn't even made yet. Very embarassing ><. Thank you again. If not for you I would have worked ob it for the next few hours untill I find, what is wrong. – Grilya Jun 27 '21 at 08:51

0 Answers0