응용프로그램/유니티(Unity)

Unity와 SQLite연동하기

zelkova 2017. 1. 13. 11:38

<목차로 돌아가기>



    

  SQLite와 Unity 연동하기


C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0 여기에 있는

System.Data.dll 과 Mono.Data.Sqlite 를 복사


 Asset\Plugins에 붙여넣기


http://www.sqlite.org/download.html 여기에서 

sqlite3.def, sqlite3.dll를 다운받기


 Asset\Plugins에 넣기




Table 생성하기

  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Data;
  5. using Mono.Data.Sqlite;

  6. public class dbScript : MonoBehaviour {

  7. void Start()
  8.     {
  9.         string conn = "URI=file:" + Application.dataPath + "/testDB.s3db"; //Path to database.
  10.         IDbConnection dbconn;
  11.         dbconn = (IDbConnection) new SqliteConnection(conn);
  12.         dbconn.Open(); //Open connection to the database.

  13.         IDbCommand dbcmd = dbconn.CreateCommand();
  14.         string sqlQuery;
  15.         sqlQuery = "CREATE TABLE COMPANY(";
  16.         sqlQuery += "ID INT PRIMARY KEY     NOT NULL,";
  17.         sqlQuery += "NAME           TEXT    NOT NULL,";
  18.         sqlQuery += "AGE            INT     NOT NULL,";
  19.         sqlQuery += "ADDRESS        CHAR(50),";
  20.         sqlQuery += "SALARY         REAL";
  21.         sqlQuery += ");";

  22.         dbcmd.CommandText = sqlQuery;
  23.         dbcmd.ExecuteNonQuery ();

  24.         dbcmd.Dispose();
  25.         dbcmd = null;
  26.         dbconn.Close();
  27.         dbconn = null;
  28.     }
  29. }


테이블에 데이터 넣기

  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Data;
  5. using Mono.Data.Sqlite;

  6. public class dbScript : MonoBehaviour {

  7. void Start()
  8.     {
  9.         string conn = "URI=file:" + Application.dataPath + "/testDB.s3db"; //Path to database.
  10.         IDbConnection dbconn;
  11.         dbconn = (IDbConnection) new SqliteConnection(conn);
  12.         dbconn.Open(); //Open connection to the database.

  13.         IDbCommand dbcmd = dbconn.CreateCommand();
  14.         string sqlQuery;
  15.         sqlQuery = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)";
  16.         sqlQuery += "VALUES (1, 'Paul', 32, 'California', 20000.00 );";

  17.         dbcmd.CommandText = sqlQuery;
  18.         dbcmd.ExecuteNonQuery ();

  19.         dbcmd.Dispose();
  20.         dbcmd = null;
  21.         dbconn.Close();
  22.         dbconn = null;
  23.     }
  24. }


데이터 읽어오기

  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Data;
  5. using Mono.Data.Sqlite;

  6. public class dbScript : MonoBehaviour {

  7. void Start()
  8.     {
  9.         string conn = "URI=file:" + Application.dataPath + "/testDB.s3db"; //Path to database.
  10.         IDbConnection dbconn;
  11.         dbconn = (IDbConnection)new SqliteConnection (conn);
  12.         dbconn.Open (); //Open connection to the database.

  13.         IDbCommand dbcmd = dbconn.CreateCommand ();
  14.         string sqlQuery = "SELECT *" + "FROM COMPANY";
  15.         dbcmd.CommandText = sqlQuery;
  16.         IDataReader reader = dbcmd.ExecuteReader ();
  17.         while (reader.Read ())
  18.         {
  19.             int ID = reader.GetInt32 (0);
  20.             string NAME = reader.GetString (1);
  21.             int AGE = reader.GetInt32 (2);
  22.             string ADDRESS = reader.GetString (3);
  23.             //ID,NAME,AGE,ADDRESS,SALARY
  24.             Debug.Log ("ID= " + ID + "  NAME =" + NAME + "  AGE =" + AGE + "  ADDRESS =" + ADDRESS);
  25.         }
  26.         reader.Close ();
  27.         reader = null;
  28.         dbcmd.Dispose ();
  29.         dbcmd = null;
  30.         dbconn.Close ();
  31.         dbconn = null;
  32.     }
  33. }








반응형