Skip to content

Instantly share code, notes, and snippets.

@hitesh-dhamshaniya
Created November 19, 2015 12:11
Show Gist options
  • Save hitesh-dhamshaniya/2f28c77ed4c6114fb3bb to your computer and use it in GitHub Desktop.
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
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