Symbianize Forum

Most of our features and services are available only to members, so we encourage you to login or register a new account. Registration is free, fast and simple. You only need to provide a valid email. Being a member you'll gain access to all member forums and features, post a message to ask question or provide answer, and share or find resources related to mobile phones, tablets, computers, game consoles, and multimedia.

All that and more, so what are you waiting for, click the register button and join us now! Ito ang website na ginawa ng pinoy para sa pinoy!

Need Help for Thesis Project about Unity Thanks sa tutulong <3 :)

hustlahuno25

Professional
Advanced Member
Messages
197
Reaction score
3
Points
28
My Problem is Database about unity android. Meron na po kong code.
Ito po yun. Ang problema ko po paano ko po sya mabbuild sa android na nakakapag insert
ako. Samantalang pag binuild ko sa PC nag iinsert sya sa database. Thankyou ng marami sa ttulong :) :D

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Data;
using Mono.Data.Sqlite;
using System.Collections.Generic;
using UnityEngine.UI;

public class HighScoreManager : MonoBehaviour {

private string connectionString;

private List<HighScore> highScores = new List<HighScore>();

public GameObject scorePrefab;

public Transform scoreParent;

public int topRanks;

public int saveScores;

public InputField enterName;

public GameObject nameDialog;

public string backtoMainMenu;

public void Back()
{
Application.LoadLevel(backtoMainMenu);
}

void Start () {

connectionString = "URI=file:" + Application.persistentDataPath + "/Plugins/HighScoreDB.s3db";

CreateTable();

DeleteExtraScore();

//InsertScore("Test", 800);


ShowScores();

}

// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Back();

}

}

private void CreateTable()
{
using (IDbConnection dbConnection = new SqliteConnection(connectionString))
{
dbConnection.Open();

using (IDbCommand dbCmd = dbConnection.CreateCommand())
{
string sqlQuery = String.Format("CREATE TABLE if not exists HighScores (PlayerID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , Name TEXT NOT NULL , Score INTEGER NOT NULL)");

dbCmd.CommandText = sqlQuery;
dbCmd.ExecuteScalar();
dbConnection.Close();

}
}
}

public void EnterName()
{
if(enterName.text != string.Empty)
{
int score = 0;
InsertScore(enterName.text, score);
enterName.text = string.Empty;

ShowScores();
}
}

private void InsertScore(String name, int newScore)
{
GetScores();

int hsCount = highScores.Count;

if (highScores.Count > 0)
{
HighScore lowestScore = highScores[highScores.Count - 1];
if (lowestScore != null && saveScores > 0 && highScores.Count >= saveScores && newScore > lowestScore.Score)
{
DeleteScore(lowestScore.ID);
hsCount--;
}

}
if (hsCount < saveScores)
{
using (IDbConnection dbConnection = new SqliteConnection(connectionString))
{
dbConnection.Open();

using (IDbCommand dbCmd = dbConnection.CreateCommand())
{
string sqlQuery = String.Format("INSERT INTO HighScores(Name,Score) VALUES ("{0}","{1}")", name, newScore);

dbCmd.CommandText = sqlQuery;
dbCmd.ExecuteScalar();
dbConnection.Close();

}
}

}

}

private void GetScores()
{

highScores.Clear();
using (IDbConnection dbConnection = new SqliteConnection(connectionString))
{
dbConnection.Open();

using (IDbCommand dbCmd = dbConnection.CreateCommand())
{
string sqlQuery = "SELECT * FROM HighScores";

dbCmd.CommandText = sqlQuery;

using (IDataReader reader = dbCmd.ExecuteReader())
{
while (reader.Read())
{
highScores.Add(new HighScore(reader.GetInt32(0), reader.GetInt32(2), reader.GetString(1)));
}

dbConnection.Close();
reader.Close();
}
}
}

highScores.Sort();
}

private void DeleteScore(int id)
{

using (IDbConnection dbConnection = new SqliteConnection(connectionString))
{
dbConnection.Open();

using (IDbCommand dbCmd = dbConnection.CreateCommand())
{
string sqlQuery = String.Format("DELETE FROM HighScores WHERE PlayerID = "{0}"", id);

dbCmd.CommandText = sqlQuery;
dbCmd.ExecuteScalar();
dbCmd.Dispose();
dbConnection.Close();
Debug.Log("Success");

}
}
}

private void ShowScores()
{
GetScores();

foreach (GameObject score in GameObject.FindGameObjectsWithTag("Score"))
{
Destroy(score);
}
for (int i = 0; i < topRanks; i++)
{
if (i <= highScores.Count - 1)
{

GameObject tmpObjec = Instantiate(scorePrefab);

HighScore tmpScore = highScores;

tmpObjec.GetComponent<HighScoreScript>().SetScore(tmpScore.Name, tmpScore.Score.ToString(), "#" + (i + 1).ToString());

tmpObjec.transform.SetParent(scoreParent);

tmpObjec.GetComponent<RectTransform>().localScale = new Vector3(1, 1, 1);


}

}
}

private void DeleteExtraScore()
{
GetScores();
if (saveScores <= highScores.Count)
{
int deleteCount = highScores.Count - saveScores;
highScores.Reverse();

using (IDbConnection dbConnection = new SqliteConnection(connectionString))
{
dbConnection.Open();

using (IDbCommand dbCmd = dbConnection.CreateCommand())
{
for (int i = 0; i < deleteCount; i++)
{
string sqlQuery = String.Format("DELETE FROM HighScores WHERE PlayerID = "{0}"", highScores.ID);

dbCmd.CommandText = sqlQuery;
dbCmd.ExecuteScalar();

}

dbConnection.Close();


}
}
}

}
}
 
connectionString = "URI=file:" + Application.persistentDataPath + "/Plugins/HighScoreDB.s3db";

try nyo po gumawa ng folder named "Android" sa loob ng Plugins folder tapos dun nyo po ilagay yung "HighScoreDB.s3db".

this --> /Plugins/Android/HighScoreDB.s3db


then alisin nyo po yung "/Plugins" sa connection string

connectionString = "URI=file:" + Application.persistentDataPath + "/HighScoreDB.s3db";
 
Back
Top Bottom