|
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 생성하기
- using UnityEngine;
- using System.Collections;
- using System;
- using System.Data;
- using Mono.Data.Sqlite;
- public class dbScript : MonoBehaviour {
- void Start()
- {
- string conn = "URI=file:" + Application.dataPath + "/testDB.s3db"; //Path to database.
- IDbConnection dbconn;
- dbconn = (IDbConnection) new SqliteConnection(conn);
- dbconn.Open(); //Open connection to the database.
- IDbCommand dbcmd = dbconn.CreateCommand();
- string sqlQuery;
- sqlQuery = "CREATE TABLE COMPANY(";
- sqlQuery += "ID INT PRIMARY KEY NOT NULL,";
- sqlQuery += "NAME TEXT NOT NULL,";
- sqlQuery += "AGE INT NOT NULL,";
- sqlQuery += "ADDRESS CHAR(50),";
- sqlQuery += "SALARY REAL";
- sqlQuery += ");";
- dbcmd.CommandText = sqlQuery;
- dbcmd.ExecuteNonQuery ();
- dbcmd.Dispose();
- dbcmd = null;
- dbconn.Close();
- dbconn = null;
- }
- }
테이블에 데이터 넣기
- using UnityEngine;
- using System.Collections;
- using System;
- using System.Data;
- using Mono.Data.Sqlite;
- public class dbScript : MonoBehaviour {
- void Start()
- {
- string conn = "URI=file:" + Application.dataPath + "/testDB.s3db"; //Path to database.
- IDbConnection dbconn;
- dbconn = (IDbConnection) new SqliteConnection(conn);
- dbconn.Open(); //Open connection to the database.
- IDbCommand dbcmd = dbconn.CreateCommand();
- string sqlQuery;
- sqlQuery = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)";
- sqlQuery += "VALUES (1, 'Paul', 32, 'California', 20000.00 );";
- dbcmd.CommandText = sqlQuery;
- dbcmd.ExecuteNonQuery ();
- dbcmd.Dispose();
- dbcmd = null;
- dbconn.Close();
- dbconn = null;
- }
- }
데이터 읽어오기
- using UnityEngine;
- using System.Collections;
- using System;
- using System.Data;
- using Mono.Data.Sqlite;
- public class dbScript : MonoBehaviour {
- void Start()
- {
- string conn = "URI=file:" + Application.dataPath + "/testDB.s3db"; //Path to database.
- IDbConnection dbconn;
- dbconn = (IDbConnection)new SqliteConnection (conn);
- dbconn.Open (); //Open connection to the database.
- IDbCommand dbcmd = dbconn.CreateCommand ();
- string sqlQuery = "SELECT *" + "FROM COMPANY";
- dbcmd.CommandText = sqlQuery;
- IDataReader reader = dbcmd.ExecuteReader ();
- while (reader.Read ())
- {
- int ID = reader.GetInt32 (0);
- string NAME = reader.GetString (1);
- int AGE = reader.GetInt32 (2);
- string ADDRESS = reader.GetString (3);
- //ID,NAME,AGE,ADDRESS,SALARY
- Debug.Log ("ID= " + ID + " NAME =" + NAME + " AGE =" + AGE + " ADDRESS =" + ADDRESS);
- }
- reader.Close ();
- reader = null;
- dbcmd.Dispose ();
- dbcmd = null;
- dbconn.Close ();
- dbconn = null;
- }
- }
반응형
'응용프로그램 > 유니티(Unity)' 카테고리의 다른 글
Unity - 에러모음집 (0) | 2017.01.13 |
---|---|
Unity 그래픽 최적화 방법 참고 (0) | 2017.01.13 |
유니티(Unity) - SQLite3 설치 및 세팅 (0) | 2017.01.10 |
Unity - 자습서 - 필수개념 - 인스펙터 사용자정의 빌딩 (0) | 2016.12.16 |
Unity - 자습서 - 필수개념 - 태그 (0) | 2016.12.16 |