티스토리 뷰

 

1. 유니티 SQLite 안드로이드에 필요한 파일 

Plugins.zip
1.33MB

 

 

2. UI 만들기 Display를 1920x1080 Portrait 

3. 파일 구성

 

4.  DB 구성 

db 파일 만들어서 plugins와 streamingAssets폴더에 각각 복사해 넣기

 

5. DB 연결 코드

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


// #2 DB 관련 
using Mono.Data.Sqlite;
using System;
using System.Data;
using System.IO;
using UnityEngine.Networking;


public class Manager : MonoBehaviour
{

    // #1. ui 연동
    public Text DBText;
    public Text DBText2;
    public Text ListText;
    public Text NameText;
    public Text AgeText;
    public Button InsertButton;
    public Button DeleteButton;
    public Button UpdateButton;
    public Button SearchButton;


    // #3. DB 변수들
    private string conn, sqlQuery;
    IDbConnection dbconn;
    IDbCommand dbcmd;
    private IDataReader reader;
    string DBName = "Test.db";

    
    private void Awake()
    {
        StartCoroutine(DBCreate());
       
    }

    private void Start()
    {
        DBConnectionCheck();
    }

    IEnumerator DBCreate()
    {
        string filepath = string.Empty;
        if (Application.platform == RuntimePlatform.Android) //안드로이드 
        {
            filepath = Application.persistentDataPath + "/"+ DBName; //안드로이드용 경로설정
            if (!File.Exists(filepath))
            {
                UnityWebRequest unityWebRequest = UnityWebRequest.Get("jar:file://" + Application.dataPath + "!/assets/"+DBName);
                unityWebRequest.downloadedBytes.ToString();
                yield return unityWebRequest.SendWebRequest().isDone;
                File.WriteAllBytes(filepath, unityWebRequest.downloadHandler.data);


            }
        }
        else //기타 플랫폼 (pc일 경우)
        {
            filepath = Application.dataPath + "/" + DBName;
            if (!File.Exists(filepath))
            {
                File.Copy(Application.streamingAssetsPath + "/" + DBName, filepath);
            }
        }
        DBText.text = "DB Create: OK";


    }

    public string GetDBFilePath()
    {
        string str = string.Empty;
        if (Application.platform == RuntimePlatform.Android)
        {
            str = "URI=file:" + Application.persistentDataPath + "/" + DBName;

        }
        else
        {
            str = "URI=file:" + Application.dataPath + "/" + DBName;
        }
        return str;
    }

    public void DBConnectionCheck()
    {
        try
        {
            IDbConnection dbConnection = new SqliteConnection(GetDBFilePath());
            dbConnection.Open();

            if (dbConnection.State == ConnectionState.Open)
            {
                DBText2.text = "DB Conn: OK";

            }
            else
            {
                DBText2.text = "DB Conn: Fail";
            }
        }
        catch (Exception e)
        {
            Debug.Log(e);
        }

    }

}

 

6. DB연결 성공 실행화면 (플레이버튼 누르면 실행)

7. 안드로이드폰에서 실행하기

(1) 안드로이드폰을 개발자 모드로 활성화하기

(2) File -> BuildSetting -> Platform -> Android -> Switch Platform

(3) PlayerSetting -> Company Name, Other Setting -> Package Name 작성 

(4) PC와 안드로이드폰을 USB로 연결하고, Run Device 선택 --> Build And Run 

 

(5) 안드로이드 폰에서도 DB OK 확인 

 

(6) 파일탐색기에서 안드로이드 폴더로 들어가서 DB파일이 진짜로 생성됐는지 재확인

 

(7) Test.db를 PC로 복사해서 SQlite에서 열어본다. 마지막 확인사살 !

8. DB Insert 추가하기 

 

 //Insert To Database
    private void DBInsert(string query)
    {
        IDbConnection dbConnection = new SqliteConnection(GetDBFilePath());
        dbConnection.Open(); //DB열기
        IDbCommand dbCommand = dbConnection.CreateCommand();

        dbCommand.CommandText = query; //쿼리입력
        dbCommand.ExecuteNonQuery(); //쿼리실행

        dbCommand.Dispose();
        dbCommand = null;
        dbConnection.Close();
        dbConnection = null;

    }

    public void Insert_Button()
    {
        DBInsert("Insert into list(name, age) values('" + NameText.text + "','"+AgeText.text+"')");
    }

1. 입력하고

2. Assets 폴더안에 db파일을 열어서 

3. 실제로 입력이 잘됐는지 확인한다.

 

* mono 에서 해야함, il2cpp에서 하면 sqlite db 안됨 

댓글
최근에 달린 댓글
글 보관함
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Total
Today
Yesterday
    뽀로로친구에디
    최근에 올라온 글