Created
November 19, 2015 12:11
-
-
Save hitesh-dhamshaniya/2f28c77ed4c6114fb3bb to your computer and use it in GitHub Desktop.
Script for Database Connection and Read & Write Operation in Unity3d for store Data i.e High score of Players
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System; | |
using System.Data; | |
using Mono.Data.Sqlite; | |
public class HighscoreManager : MonoBehaviour { | |
private String connectionString; | |
// Use this for initialization | |
void Start () { | |
connectionString = "URI=file:" + Application.dataPath + "/DBHighscore.db"; | |
InsertScore("HD", 100); | |
GetScrore(); | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
private void InsertScore(String name,int newScore) | |
{ | |
using(IDbConnection dbConnection = new SqliteConnection(connectionString)) | |
{ | |
dbConnection.Open(); | |
using(IDbCommand dbCommand = dbConnection.CreateCommand()) | |
{ | |
String sqliteQuery = String.Format("Insert Into HighscoreC(Name,Score) VALUES(\"{0}\",\"{1}\")",name,newScore); | |
dbCommand.CommandText = sqliteQuery; | |
dbCommand.ExecuteScalar(); | |
dbConnection.Close(); | |
} | |
} | |
} | |
void GetScrore() | |
{ | |
using(IDbConnection dbConnection = new SqliteConnection(connectionString)) | |
{ | |
dbConnection.Open(); | |
using(IDbCommand dbCmd = dbConnection.CreateCommand()) | |
{ | |
String sqliteQuery = "Select * From HighscoreC"; | |
dbCmd.CommandText = sqliteQuery; | |
using(IDataReader reader = dbCmd.ExecuteReader()) | |
{ | |
while (reader.Read()) | |
{ | |
Debug.Log("==> " + reader.GetString(1) +" Score => "+reader.GetValue(2)); | |
} | |
dbConnection.Close(); | |
reader.Close(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment