Unity3D持久化数据存储

一、向Unity导入sql插件

sql插件地址:链接:https://pan.baidu.com/s/1yooPgwdsXQlxbpjF6edbLw 
提取码:awsl

二、建立数据库连接

1、连接数据库

(1)引用U3D=>sql的命名空间

(2)定义数据库的名称

(3)申明SQLite连接对象

(4)创建数据库连接:

//命名空间
using SQLite4Unity3d;
using System;
using System.IO;
//数据库连接
    public string dataBaseName;

    public SQLiteConnection connection;

    public void CreatSQLite()
    {
        string path = Path.Combine(Application.streamingAssetsPath,dataBaseName);
        connection = new 
        SQLiteConnection(path,SQLiteOpenFlags.ReadWrite|SQLiteOpenFlags.Create);
    }

2、创建表格

(1)判断数据库是否连接

(2)创建并接收表格

(3)执行

    public void CreatUserTabel()
    {
        if (connection!=null)
        {
            //接收
            SQLiteCommand liteCommand= connection.CreateCommand("CREATE TABLE User(UserName varchar(255),Password varchar(255))");
            //执行
            liteCommand.ExecuteNonQuery();
        }
    }

3、增加数据

(1)判断数据库是否连接

(2)添加数据

(3)执行

    public void InsertUser()
    {
        if (connection!=null)
        {
            SQLiteCommand liteCommand = connection.CreateCommand("INSERT INTO User VALUES("+"\"admin1\",\"111111\")");
            liteCommand.ExecuteNonQuery();
        }
    }

4、删除数据

(1)判断数据库是否连接

(2)删除数据

(3)执行

    public void DeleteUser()
    {
        if (connection!=null)
        {
            SQLiteCommand liteCommand = connection.CreateCommand("DELETE FROM User WHERE UserName=\"admin1\"");
            liteCommand.ExecuteNonQuery();
        }
    }

5、查询数据

(1)建立一个存放User的脚本

using System.Collections;
using System.Collections.Generic;

public class User
{

    public string UserName { get; set; }
    public string Password { get; set; }
}

(2)判断数据库是否连接

(3)查询数据

(4)执行

    public void SelecUser()
    {
        if (connection!=null)
        {
            SQLiteCommand liteCommand = connection.CreateCommand("SELECT * FROM User");
            List list = liteCommand.ExecuteQuery();
            foreach (var item in list)
            {
                print(item.UserName +":"+ item.Password);
            }
        }
    }

6、修改数据

(1)判断数据库是否连接

(2)修改数据

(3)执行

    public void UpdateUser()
    {
        if (connection!=null)
        {
            SQLiteCommand liteCommand = connection.CreateCommand("UPDATE User SET Password=\"22222\"WHERE UserName=\"admin1\"");
            liteCommand.ExecuteNonQuery();
        }
    }

7、关闭连接

如果连接对象不为空,表示数据库已经连接成功

(1)关闭连接

(2)销毁

(3)垃圾回收(防止指针的溢出)

//断开连接
     public void OnDestroy()
    {
        if (connection!=null)
        {
            connection.Close();
            connection.Dispose();
            GC.Collect();
        }
    }

8、整体代码展示

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SQLite4Unity3d;
using System;
using System.IO;

public class SQLiteController : MonoBehaviour {

    //定义数据库名称
    public string dataBaseName;
    //申明连接对象
    public SQLiteConnection connection;

    //创建数据库连接
    public void CreatSQLite()
    {
        string path = Path.Combine(Application.streamingAssetsPath,dataBaseName);
        connection = new SQLiteConnection(path,SQLiteOpenFlags.ReadWrite|SQLiteOpenFlags.Create);
    }

    //数据库建表
    public void CreatUserTabel()
    {
        if (connection!=null)
        {
            //接收
            SQLiteCommand liteCommand= connection.CreateCommand("CREATE TABLE User(UserName varchar(255),Password varchar(255))");
            //执行
            liteCommand.ExecuteNonQuery();
        }
    }

    //数据库添加数据
    public void InsertUser()
    {
        if (connection!=null)
        {
            SQLiteCommand liteCommand = connection.CreateCommand("INSERT INTO User VALUES("+"\"admin1\",\"111111\")");
            liteCommand.ExecuteNonQuery();
        }
    }

    //查询数据库数据
    public void SelecUser()
    {
        if (connection!=null)
        {
            SQLiteCommand liteCommand = connection.CreateCommand("SELECT * FROM User");
            List list = liteCommand.ExecuteQuery();
            foreach (var item in list)
            {
                print(item.UserName +":"+ item.Password);
            }
        }
    }

    //修改数据库数据
    public void UpdateUser()
    {
        if (connection!=null)
        {
            SQLiteCommand liteCommand = connection.CreateCommand("UPDATE User SET Password=\"22222\"WHERE UserName=\"admin1\"");
            liteCommand.ExecuteNonQuery();
        }
    }

    //删除数据库数据
    public void DeleteUser()
    {
        if (connection!=null)
        {
            SQLiteCommand liteCommand = connection.CreateCommand("DELETE FROM User WHERE UserName=\"admin1\"");
            liteCommand.ExecuteNonQuery();
        }
    }

    //断开连接
    public void OnDestroy()
    {
        if (connection!=null)
        {
            connection.Close();
            connection.Dispose();
            GC.Collect();
        }
    }

    private void Start()
    {
        CreatSQLite();
    }
}

你可能感兴趣的:(c#,vscode)