- Home /
How to connect game to SQLite
I've been trying to connect my game (which will mainly run in WebGL) to a database in order to store highscores and such. After some reading I found that SQLite would be the better choice since it requires little setup, but I haven't been able to connect the game to a database properly.
I've already tried following this link specifically for Unity and this one for a C# library, yet neither one seemed to work for me. I also looked into this plugin but am unsure about how to use it after I've pasted the files into the Plugins folder.
Is there any other way or something I might be missing? For code there's nothing much to show, my test class only has what I copied from the first link:
public DBInterface()
{
string conn = "URI=file:" + Application.dataPath + "/Database/highscores.s3db"; //Path to database.
Debug.Log(conn);
IDbConnection dbconn;
dbconn = (IDbConnection)new SqliteConnection(conn);
dbconn.Open(); //Open connection to the database.
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "SELECT * FROM HIGHSCORES";
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
Debug.Log(reader);
while (reader.Read())
{
string name = reader.GetString(0);
int rand = reader.GetInt32(1);
Debug.Log("name =" + name + " random =" + rand);
}
//SqliteConnection connection = new SqliteConnection(Application.dataPath + "/Database/highscores.s3db");
//connection.Query("SELECT * from HIGHSCORES");
}
Or would it be easier to use another database? Perhaps MongoDB?
Your answer
