Question by
iPewds · Apr 17, 2016 at 12:31 PM ·
androidandroid buildsqlite
SQLLITE for android
Hello i am a complete noob in importing sqllite in unity. my code works well in desktop games but it won't work on android :( can someone tell me what's wrong? or better a solution?
here is my code:
using UnityEngine; using System.Collections; using System; using System.Data; using Mono.Data.Sqlite; using System.Collections.Generic;
public class inventoryManager : MonoBehaviour {
private List<string> inventoryArray = new List<string>();
public GameObject inventoryItemPrefab;
public GameObject InventoryPanel;
void Awake()
{
createTable();
}
public void insertData(string nameOfItem)
{
string connectionString = "jar:file://" + Application.dataPath + "/colorblockdatabase.sqlite";
using (IDbConnection con = new SqliteConnection(connectionString.ToString()))
{
con.Open();
using (IDbCommand dbCmd = con.CreateCommand())
{
string sql = string.Format("Insert into playerInventory(itemName) Values ('{0}')", nameOfItem);
dbCmd.CommandText = sql;
dbCmd.ExecuteScalar();
con.Close();
}
}
}
public void selectData()
{
string connectionString = "jar:file://" + Application.dataPath + "/colorblockdatabase.sqlite";
using (IDbConnection con = new SqliteConnection(connectionString))
{
con.Open();
using (IDbCommand dbCmd = con.CreateCommand())
{
string sql = "Select * from playerInventory";
dbCmd.CommandText = sql;
using (IDataReader reader = dbCmd.ExecuteReader())
{
while (reader.Read())
{
inventoryArray.Add(reader.GetString(1));
}
Debug.Log("Data Loaded SQLLITE");
con.Close();
reader.Close();
}
}
}
}
public void displayItems()
{
for (int i = 0; i < inventoryArray.Count; i++)
{
GameObject tmpObj = Instantiate(inventoryItemPrefab);
string tmpName = inventoryArray[i];
tmpObj.GetComponent<itemProperties>().setPanel(tmpName, tmpName);
tmpObj.transform.SetParent(InventoryPanel.transform);
tmpObj.GetComponent<RectTransform>().localScale = new Vector3(1, 1, 1);
}
}
public void createTable()
{
string connectionString = "jar:file://" + Application.dataPath + "/colorblockdatabase.sqlite";
using (IDbConnection con = new SqliteConnection(connectionString))
{
con.Open();
using (IDbCommand dbCmd = con.CreateCommand())
{
string sql = string.Format("CREATE TABLE if not exists playerInventory (inventoryID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , itemName VARCHAR)");
dbCmd.CommandText = sql;
dbCmd.ExecuteScalar();
con.Close();
}
}
}
public void truncateArray()
{
inventoryArray.Clear();
foreach (Transform child in InventoryPanel.transform)
{
GameObject.Destroy(child.gameObject);
}
}
}
Comment