- Home /
How do I get SQLite databases working in my build when they already work in my project?
Today I was supposed to begin testing my build, but I've found that I've done something wrong with SQLite integration and I'm not sure what that is.
Everything works in the unity editor, but I think I've missed a step to allow the databases to be created properly in the build folder. The core game systems rely on the databases working correctly, and they are not.
There is a lot of code in my project, so I'm not sure what to share, so here is my Database Manager script, and a screenshot of my project setup.
I think there is something wrong with the location of the databases or how they are being handled in the build, but I have no idea how to fix this type of issue.
Thanks in advance for any help. I am happy to provide any additional information you may need.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mono.Data.Sqlite;
using System;
using System.Data;
public class DBManager : MonoBehaviour
{
private string quotesDB = "URI=file:Quotes.db";
private string musicDB = "URI=file:Music.db";
public List<Quote> level1Quotes = new();
public List<Quote> level2Quotes = new();
public List<Quote> level3Quotes = new();
public List<Quote> level4Quotes = new();
public List<Quote> pythonQuotes = new();
public List<Quote> playQuotes = new();
public List<Track> trackList = new();
//public List<Track> lofiTracks = new();
//public List<Track> jazzTracks = new();
//public List<Track> classicalTracks = new();
PlayerInfo playerInfo;
public int quoteCount;
void Awake()
{
playerInfo = GameObject.Find("PlayerInfo").GetComponent<PlayerInfo>();
}
private void Start()
{
MakeTrackList();
MakeQuoteList();
MakePlayQuotes();
}
public void MakePlayQuotes()
{
playQuotes.Clear();
if (playerInfo.difficultyList[0])
{
//Debug.Log("Level 1 Enabled");
foreach (Quote quote in level1Quotes)
{
playQuotes.Add(quote);
}
}
if (playerInfo.difficultyList[1])
{
//Debug.Log("Level 2 Enabled");
foreach (Quote quote in level2Quotes)
{
playQuotes.Add(quote);
}
}
if (playerInfo.difficultyList[2])
{
//Debug.Log("Level 3 Enabled");
foreach (Quote quote in level3Quotes)
{
playQuotes.Add(quote);
}
}
if (playerInfo.difficultyList[3])
{
//Debug.Log("Level 4 Enabled");
foreach (Quote quote in level4Quotes)
{
playQuotes.Add(quote);
}
}
if (playerInfo.python)
{
//Debug.Log("Level 4 Enabled");
foreach (Quote quote in pythonQuotes)
{
playQuotes.Add(quote);
}
}
// shuffle
for (int i = 0; i < playQuotes.Count; i++)
{
Quote tempQuote = playQuotes[i];
int randomIndex = UnityEngine.Random.Range(i, playQuotes.Count);
playQuotes[i] = playQuotes[randomIndex];
playQuotes[randomIndex] = tempQuote;
}
}
private void MakeQuoteList()
{
using (var connection = new SqliteConnection(quotesDB))
{
connection.Open();
// set up an object called command for db control
using (var command = connection.CreateCommand())
{
// adding all query responses to this list
command.CommandText = "SELECT * FROM quotes;";
using (IDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Quote tempQuote = new Quote(Convert.ToString(reader["code"]), Convert.ToString(reader["text"]), Convert.ToString(reader["author"]), Convert.ToString(reader["category"]), Convert.ToInt32(reader["length"]), Convert.ToDouble(reader["difficulty"]));
//Debug.Log("Adding quote " + tempQuote.code + ", by " + tempQuote.author + ". Difficulty: " + tempQuote.difficulty);
if (tempQuote.difficulty < 2)
{
level1Quotes.Add(tempQuote);
//Debug.Log("Adding " + tempQuote.code + " to Level 1");
}
else if (tempQuote.difficulty < 3)
{
level2Quotes.Add(tempQuote);
//Debug.Log("Adding " + tempQuote.code + " to Level 2");
}
else if (tempQuote.difficulty < 4)
{
level3Quotes.Add(tempQuote);
//Debug.Log("Adding " + tempQuote.code + " to Level 3");
}
else if (tempQuote.difficulty >= 4 && tempQuote.difficulty < 10)
{
level4Quotes.Add(tempQuote);
//Debug.Log("Adding " + tempQuote.code + " to Level 4");
}
else if (tempQuote.difficulty == 10.0)
{
pythonQuotes.Add(tempQuote);
//Debug.Log("Adding " + tempQuote.code + " to python");
}
}
reader.Close();
}
}
connection.Close();
}
}
private void MakeTrackList()
{
using (var connection = new SqliteConnection(musicDB))
{
connection.Open();
// set up an object called command for db control
using (var command = connection.CreateCommand())
{
// adding all query responses to this list
command.CommandText = "SELECT * FROM music;";
using (IDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Track tempTrack = new Track(Convert.ToString(reader["code"]), Convert.ToString(reader["title"]), Convert.ToString(reader["artist"]), Convert.ToString(reader["playlist"]));
//Debug.Log("Adding track " + tempTrack.title + ", by " + tempTrack.artist);
if (tempTrack.playlist == "lofi")
{
trackList.Add(tempTrack);
//Debug.Log("Adding " + tempTrack.code + " to lofi");
}
else if (tempTrack.playlist == "jazz")
{
trackList.Add(tempTrack);
//Debug.Log("Adding " + tempTrack.code + " to jazz");
}
else if (tempTrack.playlist == "classical")
{
trackList.Add(tempTrack);
//Debug.Log("Adding " + tempTrack.code + " to classical");
}
}
reader.Close();
}
}
connection.Close();
}
}
private void ReadNumOfQuotes()
{
string stmt = "SELECT COUNT(*) FROM quotes;";
int count = 0;
// Create the connection
using (var connection = new SqliteConnection(quotesDB))
{
using(SqliteCommand quoteCount = new SqliteCommand(stmt, connection))
{
connection.Open();
count = Convert.ToInt32(quoteCount.ExecuteScalar());
}
}
quoteCount = count;
}
}
Your answer
Follow this Question
Related Questions
How do you disable Gear VR? 3 Answers
In Unity 2d is it possible to change which sprite sheet a player is using? 0 Answers
How to change sprite only on click or during shoot state? 1 Answer
increase speed every spawn wave 0 Answers
When I build my 2D game and set it to fullscreen, the resolution looks off 0 Answers