I am creating a game on Android where I am storing the players high scores in an SQLite database. It seems to work fine on the PC but I wondering if I need to make any changes to the path (to the Database) when running the application on android.
Here is my db Access class:
#pragma strict
/*
This will be the Database Access Class that has the implementations to the CRUD operations required
by our Database. We will create a Table that stores the players stats (name, score and picture) and store
it in a database for persistence.
*/
import System.Data; //import the Data class
import Mono.Data.Sqlite; //import SQLite
import System.Collections.Generic;
class dbAccess {
private var connection : String;
private var dbcon : IDbConnection;
private var dbcmd : IDbCommand;
private var reader : IDataReader;
/*
Opens the database connection
*/
function OpenDB(p : String) {
connection = "URI=file:" + p; // we set the connection to our database
dbcon = new SqliteConnection(connection);
dbcon.Open();
}
/*
Closes the database connection
*/
function CloseDB() {
reader.Close(); // clean everything up
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbcon.Close();
dbcon = null;
}
/*
Creates the Table of our choice
*/
function CreateTable(name : String, col : Array, colType : Array) {
var query : String;
query = "CREATE TABLE " + name + "(" + col[0] + " " + colType[0];
for(var i=1; i<col.length; i++) {
query += ", " + col _+ " " + colType*;*_
}
query += “)”;
dbcmd = dbcon.CreateCommand(); // create empty command
dbcmd.CommandText = query; // fill the command
reader = dbcmd.ExecuteReader(); // execute command which returns a reader
}
/*
Implements the SQL Query that retrives all the information for our table
*/
function ReadFullTable(tableName : String) {
var query : String;
query = "SELECT * FROM " + tableName;
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
reader = dbcmd.ExecuteReader();
var readArray = new ArrayList();
while(reader.Read()) {
var lineArray = new ArrayList();
for (var i:int = 0; i < reader.FieldCount; i++)
lineArray.Add(reader.GetValue(i)); // This reads the entries in a row
readArray.Add(lineArray); // This makes an array of all the rows
}
return readArray; // return matches
}
/*
Implements the SQL Query that deletes all the data from our table
*/
function DeleteTableContents(tableName : String) {
* var query : String;*
* query = "DELETE FROM " + tableName;*
* dbcmd = dbcon.CreateCommand();*
* dbcmd.CommandText = query;*
* reader = dbcmd.ExecuteReader();*
}
/*
Allows us to insert a row to the database
*/
function InsertInto(tableName : String, values : Array) { // basic Insert with just values
var query : String;
query = “INSERT INTO " + tableName + " VALUES (” + values[0];
for(var i=1; i<values.length; i++) {
query += ", " + values*;*
}
query += “)”;
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
reader = dbcmd.ExecuteReader();
}
function ReadTopFiveScores(tableName : String) {
var query : String;
query = "SELECT * FROM " + tableName + " ORDER BY highScore DESC LIMIT 5 ";
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
reader = dbcmd.ExecuteReader();
var readArray = new ArrayList();
while(reader.Read()) {
var lineArray = new ArrayList();
for (var i:int = 0; i < reader.FieldCount; i++)
lineArray.Add(reader.GetValue(i)); // This reads the entries in a row
readArray.Add(lineArray); // This makes an array of all the rows
}
return readArray; // return matches
}
}
Currently the Database is created in the project root folder. I am wondering if I need to change “connection = “URI=file:” + p;” when running it on Android. Currently the ‘p’ variable refers to the name of the Database