Unity使用Sqlite3

环境

Unity:Unity2021.3.6f1c1
OS:Window10 64
Plugins:Mono.Data.Sqlite、Sqlite

插件准备

  1. Sqlite3官方网址
    Sqlite3有x64和x86版本,根据发布的架构使用不同版本的Sqlite3。
    该文档使用x64版本,发布架构为64位。
    在这里插入图片描述
  2. Mono.Data.Sqlite
    使用Unity Hub打开Unity Editor目录,进入该路径
    Data\MonoBleedingEdge\lib\mono\unityjit-win32

Mono.Data.Sqlite源码

mono/mcs/class/Mono.Data.Sqlite

示例

  1. 打印表中数据显示在屏幕
  2. Mono.Data.Sqlite和MySql.Data的使用方法类似
using UnityEngine;
using System.Data;
using Mono.Data.Sqlite;
using System.Collections.Generic;
public class SqliteHelper : MonoBehaviour
{
    void Start()
    {
    	//连接数据库
        SqliteConnection connection = new SqliteConnection
            ("Data Source=" + Application.streamingAssetsPath + "/AssetLibrary.db");
        try
        {
        	//打开数据库
            connection.Open();
            Debug.Log(connection.State);
            if (connection.State == ConnectionState.Open)
            {
            	//sql命令
                SqliteCommand serarch = new SqliteCommand("select * from furniture", connection);
                //执行命令
                SqliteDataReader reader = serarch.ExecuteReader();
                //读取表中数据
                while (reader.Read())
                {
                	//读取数据
                    var info = (reader[0], reader[1], reader[2]);
                    dataInfos.Add(info.ToString());
                }
            }
            //关闭数据库
            connection.Close();
        }
        catch (System.Exception e)
        {
            Debug.LogException(e);
        }
    }
    List<string> dataInfos = new List<string>();
    Vector2 scrollPos;
    private void OnGUI()
    {
        if(dataInfos.Count > 0)
        {
            scrollPos=GUILayout.BeginScrollView(scrollPos);
            var length = dataInfos.Count;
            for (int i = 0; i < length; i++)
            {
                GUILayout.Label(dataInfos[i]);
            }
            GUILayout.EndScrollView();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值