赞
踩
在Unity开发游戏时使用SQLite有多种原因,以下是其中一些主要原因:
SQLite Download Page 官网下载
根据操作系统下载
sqlite3.def 只是为了支持本地计算机的CMD命令窗口可以访问的文件
创建文件夹 (自己命名,原则就是自己能找到)
在自己电脑上搜这两个文件,复制到自己刚刚创建的文件夹Plugins
SQLiteConnection是.NET Framework中的一个类,它是ADO.NET用于与SQLite数据库进行交互的主要接口之一。通过这个类,你可以执行SQL查询,修改数据库中的数据,处理数据库中的事务等等。
如果你需要使用SQLiteConnection,首先你需要确保你的项目已经引入了正确的SQLite .NET 包。你可以通过NuGet来添加这个包。在Visual Studio中,右击项目 -> "Manage NuGet Packages..." -> 搜索 "System.Data.SQLite" -> "Install"。
SQLiteConnection类的主要属性和方法有:
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
-
-
- public class UserLogin : MonoBehaviour
- {
- // 该脚本获取场景中输入框组件2.获取用户输入内容
-
- public TMP_InputField userInputFieldAccount;
- public TMP_InputField userInputFieldPassword;
- public GameObject inputkuang;
-
-
-
- public static string UserAccount;
- public static string UserPassword;
- void Start()
- {
- userInputFieldAccount.onEndEdit.AddListener(OnUserInputChanged);
- userInputFieldPassword = inputkuang.GetComponent<TMP_InputField>();
- userInputFieldPassword.onEndEdit.AddListener(OnUserInputPassword);
- }
-
- private void OnUserInputPassword(string password)
- {
- Debug.Log("用户输入的密码是:" + password);
- UserPassword = password;
- }
-
- // Update is called once per frame
-
- private void OnUserInputChanged(string Account)
- {
- // 当用户输入值发生改变时调用该方法
- Debug.Log("用户输入的注册:" + Account);
- UserAccount = Account;
- }
- public void Login()//该函数用来在外部登录按钮面板中触发
- {
- Debug.Log("面板上的函数触发成功,用户点击了注册按钮" + UserAccount + " " + UserPassword);
- do
- {
- ConnectDataSQL.GetUserInput(UserAccount, UserPassword);//调用另一个类的方法:插入数据库并存储
- Debug.Log("注册成功");
- } while (false);
-
- }
- }
- using UnityEngine;
- using System.Data;
- using Mono.Data.Sqlite;
-
-
- public class ConnectDataSQL : MonoBehaviour
- {
-
- private const string databaseName = "leoyang.db"; // 数据库文件名称
- static SqliteConnection MyConnectionDB;//创建一个数据库链接事件对象
-
- string UserAccount;
- string UserPassword;
- private void Start()
- {
- // 定义数据库连接字符串
- string connectionString = "URI=file:" + Application.dataPath + "/" + databaseName;
-
- // 创建数据库连接
- //传入创建或者访问SQLITE数据库的路径
- MyConnectionDB = new SqliteConnection(connectionString);
- if (MyConnectionDB.State == ConnectionState.Closed)//检测当前数据库链接状态是否关闭
- {
- MyConnectionDB.Open();//打开数据库
- Debug.Log("数据库链接完毕已打开");
- }
- else
- {
- Debug.Log("数据库连接失败");
- }
- // 创建数据库用户表(如果不存在)
- CreateTable(MyConnectionDB);
- }
-
- // 创建用户表的方法
- private void CreateTable(SqliteConnection oneConnect)
- {
- string sqlCreateTable = "CREATE TABLE IF NOT EXISTS UserTable (" +
- "Id INTEGER PRIMARY KEY AUTOINCREMENT," +
- "Username TEXT NOT NULL," +
- "Password TEXT NOT NULL" +
- ");";
- SqliteCommand SQcommand = new SqliteCommand(sqlCreateTable, oneConnect);//数据库创建命令
- SQcommand.ExecuteNonQuery();
- SQcommand.Dispose();
- SQcommand = null;
- }
-
- public static void GetUserInput(string UserAccount, string UserPassword)//用户点击注册开始插入数据库
- {
- if (UserPassword!=null&&UserAccount!=null)
- {
- InsertUser(UserAccount,UserPassword);
- }
- }
- // 插入用户输入的账号和密码的方法
- public static void InsertUser(string username, string password)
- {
- string sqlCreateTable = "INSERT INTO UserTable (Username, Password) VALUES (@Username, @Password)";
- if (MyConnectionDB.State != ConnectionState.Open)
- {
- MyConnectionDB.Open();
- Debug.Log("我为您重新打开了数据库");
- }
- else
- {
-
- SqliteCommand Insertuser = new SqliteCommand(sqlCreateTable, MyConnectionDB);
- Insertuser.Parameters.AddWithValue("@Username", username);
- Insertuser.Parameters.AddWithValue("@Password", password);
- //在 try 块中的代码执行期间,如果发生了异常,则会跳过后续的代码,并进入与异常类型匹配的 catch 块中进行处理。如果异常类型没有与任何 catch 块匹配,那么将会跳过所有的 catch 块,但仍然可以选择执行 finally 块。
- try
- {
- Insertuser.ExecuteNonQuery();
- Debug.Log("插入注册成功.");
- }
- catch (SqliteException yichang)
- {
- Debug.LogError("插入注册失败 " + yichang.Message);
- }
- finally
- {
- // 释放资源和清理操作
- Insertuser.Dispose();
- Insertuser = null;
- MyConnectionDB.Close();
- }
- }
- Debug.Log("注册成功.");
-
- }
-
- // 读取用户表中的数据的方法
- // 每次执行完数据库命令后,通常需要调用 ExecuteNonQuery() 方法来执行命令,然后使用 Dispose() 方法释放相关资源,
- // 最后将对象置为 null。
- public void ClearDB()//关闭数据库
- {
-
- MyConnectionDB.Close();
- MyConnectionDB = null;
- }
- }
PS 用完数据库要关闭和清空====================
这段代码是用于关闭数据库连接的方法。具体来说,MyConnectionDB.Close()
表示关闭当前数据库连接,并释放与该连接关联的任何资源。而MyConnectionDB = null;
则将数据库连接对象置为null,以便垃圾回收器可以在适当的时候将其回收。
关闭数据库连接对于提高应用程序性能和防止资源泄漏非常重要。如果没有正确关闭数据库连接,可能会导致连接池被占用过多而影响应用程序性能,或者导致系统资源不足而导致应用程序出现异常。
在使用完数据库连接后,应该及时关闭它,以便让其他应用程序能够通过连接池访问数据库。同时,将对象置为null也有助于垃圾回收器及时回收不再使用的资源,以提高应用程序的性能和稳定性。
总之,本段代码的作用是在关闭数据库连接后,将连接对象置为null,以便垃圾回收器可以及时回收资源。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。