國際慣例:先看效果
1.SQlite是一種嵌入型數(shù)據(jù)庫
在Unity開發(fā)游戲時(shí)使用SQLite有多種原因,以下是其中一些主要原因:
- 嵌入式數(shù)據(jù)庫:SQLite是一個(gè)嵌入式數(shù)據(jù)庫引擎,這意味著它不需要單獨(dú)的服務(wù)器進(jìn)程。這使得使用SQLite非常方便,并且可以輕松地在不同的平臺(tái)和操作系統(tǒng)上使用。
- 易于使用:SQLite易于學(xué)習(xí)和使用,具有簡(jiǎn)潔的SQL語法和強(qiáng)大的功能。這使得開發(fā)者可以更快地構(gòu)建和部署游戲,同時(shí)減少了對(duì)復(fù)雜的數(shù)據(jù)庫系統(tǒng)的需求。
- 兼容性:SQLite具有跨平臺(tái)的兼容性,可以在Windows、Mac、Linux、Android和iOS等不同平臺(tái)上使用。這意味著使用SQLite可以減少游戲在不同平臺(tái)上的差異,提高游戲的一致性。
- 安全性:SQLite支持事務(wù)和數(shù)據(jù)完整性,這使得它成為一種安全的數(shù)據(jù)庫選擇。它還提供了加密選項(xiàng),可以保護(hù)存儲(chǔ)在數(shù)據(jù)庫中的敏感數(shù)據(jù)。
- 可擴(kuò)展性、移植:SQLite具有很好的可擴(kuò)展性,可以處理大量的數(shù)據(jù)和并發(fā)訪問。這意味著使用SQLite可以滿足游戲開發(fā)中的各種需求,包括存儲(chǔ)大量的游戲數(shù)據(jù)和處理高并發(fā)的用戶訪問。
- 開源和免費(fèi):SQLite是開源的,可以免費(fèi)使用和修改。此外,SQLite還具有很好的文檔和社區(qū)支持,可以幫助開發(fā)者解決遇到的問
2.如何在服務(wù)器或者本地計(jì)算機(jī)安裝SQLite?
SQLite Download Page?官網(wǎng)下載
根據(jù)操作系統(tǒng)下載
C盤創(chuàng)建文件夾,把下載的文件解壓放進(jìn)去
?sqlite3.def 只是為了支持本地計(jì)算機(jī)的CMD命令窗口可以訪問的文件
3.如何在Unity中使用SQLite?
創(chuàng)建文件夾 (自己命名,原則就是自己能找到)
在自己電腦上搜這兩個(gè)文件,復(fù)制到自己剛剛創(chuàng)建的文件夾Plugins?
-
Mono.Data.Sqlite.dll
-
sqlite3.dll
4.重啟VS, 引用程序集即可!using Mono.Data.Sqlite;
5.學(xué)習(xí)該程序集的第一個(gè)類SQLiteConnection
SQLiteConnection是.NET Framework中的一個(gè)類,它是ADO.NET用于與SQLite數(shù)據(jù)庫進(jìn)行交互的主要接口之一。通過這個(gè)類,你可以執(zhí)行SQL查詢,修改數(shù)據(jù)庫中的數(shù)據(jù),處理數(shù)據(jù)庫中的事務(wù)等等。
如果你需要使用SQLiteConnection,首先你需要確保你的項(xiàng)目已經(jīng)引入了正確的SQLite .NET 包。你可以通過NuGet來添加這個(gè)包。在Visual Studio中,右擊項(xiàng)目 -> "Manage NuGet Packages..." -> 搜索 "System.Data.SQLite" -> "Install"。
SQLiteConnection類的主要屬性和方法有:
- ConnectionString:用于定義數(shù)據(jù)庫連接的字符串。
- Open():打開數(shù)據(jù)庫連接。
- Close():關(guān)閉數(shù)據(jù)庫連接。
- Execute(SqlCommand command):執(zhí)行一個(gè)SQL命令并返回受影響的行數(shù)。
- BeginTransaction():開始一個(gè)數(shù)據(jù)庫事務(wù)。
- Commit():提交數(shù)據(jù)庫事務(wù)。
- Rollback():回滾數(shù)據(jù)庫事務(wù)。
實(shí)戰(zhàn)案例:用戶注冊(cè)
用戶注冊(cè)并存儲(chǔ)賬號(hào)密碼到數(shù)據(jù)庫
?兩個(gè)腳本:
1.獲取用戶在UI界面輸入的賬號(hào)密碼字符串
2.創(chuàng)建一個(gè)數(shù)據(jù)鏈接SQLite!創(chuàng)建一個(gè)表!把獲取的賬號(hào)密碼插入數(shù)據(jù)庫!
場(chǎng)景搭建:
1.獲取用戶在UI界面輸入的賬號(hào)密碼字符串
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class UserLogin : MonoBehaviour
{
// 該腳本獲取場(chǎng)景中輸入框組件2.獲取用戶輸入內(nèi)容
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)
{
// 當(dāng)用戶輸入值發(fā)生改變時(shí)調(diào)用該方法
Debug.Log("用戶輸入的注冊(cè):" + Account);
UserAccount = Account;
}
public void Login()//該函數(shù)用來在外部登錄按鈕面板中觸發(fā)
{
Debug.Log("面板上的函數(shù)觸發(fā)成功,用戶點(diǎn)擊了注冊(cè)按鈕" + UserAccount + " " + UserPassword);
do
{
ConnectDataSQL.GetUserInput(UserAccount, UserPassword);//調(diào)用另一個(gè)類的方法:插入數(shù)據(jù)庫并存儲(chǔ)
Debug.Log("注冊(cè)成功");
} while (false);
}
}
2.創(chuàng)建一個(gè)數(shù)據(jù)鏈接SQLite!創(chuàng)建一個(gè)表!把獲取的賬號(hào)密碼插入數(shù)據(jù)庫!
using UnityEngine;
using System.Data;
using Mono.Data.Sqlite;
public class ConnectDataSQL : MonoBehaviour
{
private const string databaseName = "leoyang.db"; // 數(shù)據(jù)庫文件名稱
static SqliteConnection MyConnectionDB;//創(chuàng)建一個(gè)數(shù)據(jù)庫鏈接事件對(duì)象
string UserAccount;
string UserPassword;
private void Start()
{
// 定義數(shù)據(jù)庫連接字符串
string connectionString = "URI=file:" + Application.dataPath + "/" + databaseName;
// 創(chuàng)建數(shù)據(jù)庫連接
//傳入創(chuàng)建或者訪問SQLITE數(shù)據(jù)庫的路徑
MyConnectionDB = new SqliteConnection(connectionString);
if (MyConnectionDB.State == ConnectionState.Closed)//檢測(cè)當(dāng)前數(shù)據(jù)庫鏈接狀態(tài)是否關(guān)閉
{
MyConnectionDB.Open();//打開數(shù)據(jù)庫
Debug.Log("數(shù)據(jù)庫鏈接完畢已打開");
}
else
{
Debug.Log("數(shù)據(jù)庫連接失敗");
}
// 創(chuàng)建數(shù)據(jù)庫用戶表(如果不存在)
CreateTable(MyConnectionDB);
}
// 創(chuàng)建用戶表的方法
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);//數(shù)據(jù)庫創(chuàng)建命令
SQcommand.ExecuteNonQuery();
SQcommand.Dispose();
SQcommand = null;
}
public static void GetUserInput(string UserAccount, string UserPassword)//用戶點(diǎn)擊注冊(cè)開始插入數(shù)據(jù)庫
{
if (UserPassword!=null&&UserAccount!=null)
{
InsertUser(UserAccount,UserPassword);
}
}
// 插入用戶輸入的賬號(hào)和密碼的方法
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("我為您重新打開了數(shù)據(jù)庫");
}
else
{
SqliteCommand Insertuser = new SqliteCommand(sqlCreateTable, MyConnectionDB);
Insertuser.Parameters.AddWithValue("@Username", username);
Insertuser.Parameters.AddWithValue("@Password", password);
//在 try 塊中的代碼執(zhí)行期間,如果發(fā)生了異常,則會(huì)跳過后續(xù)的代碼,并進(jìn)入與異常類型匹配的 catch 塊中進(jìn)行處理。如果異常類型沒有與任何 catch 塊匹配,那么將會(huì)跳過所有的 catch 塊,但仍然可以選擇執(zhí)行 finally 塊。
try
{
Insertuser.ExecuteNonQuery();
Debug.Log("插入注冊(cè)成功.");
}
catch (SqliteException yichang)
{
Debug.LogError("插入注冊(cè)失敗 " + yichang.Message);
}
finally
{
// 釋放資源和清理操作
Insertuser.Dispose();
Insertuser = null;
MyConnectionDB.Close();
}
}
Debug.Log("注冊(cè)成功.");
}
// 讀取用戶表中的數(shù)據(jù)的方法
// 每次執(zhí)行完數(shù)據(jù)庫命令后,通常需要調(diào)用 ExecuteNonQuery() 方法來執(zhí)行命令,然后使用 Dispose() 方法釋放相關(guān)資源,
// 最后將對(duì)象置為 null。
public void ClearDB()//關(guān)閉數(shù)據(jù)庫
{
MyConnectionDB.Close();
MyConnectionDB = null;
}
}
?PS 用完數(shù)據(jù)庫要關(guān)閉和清空====================
?
這段代碼是用于關(guān)閉數(shù)據(jù)庫連接的方法。具體來說,MyConnectionDB.Close()
表示關(guān)閉當(dāng)前數(shù)據(jù)庫連接,并釋放與該連接關(guān)聯(lián)的任何資源。而MyConnectionDB = null;
則將數(shù)據(jù)庫連接對(duì)象置為null,以便垃圾回收器可以在適當(dāng)?shù)臅r(shí)候?qū)⑵浠厥铡?/p>
關(guān)閉數(shù)據(jù)庫連接對(duì)于提高應(yīng)用程序性能和防止資源泄漏非常重要。如果沒有正確關(guān)閉數(shù)據(jù)庫連接,可能會(huì)導(dǎo)致連接池被占用過多而影響應(yīng)用程序性能,或者導(dǎo)致系統(tǒng)資源不足而導(dǎo)致應(yīng)用程序出現(xiàn)異常。
在使用完數(shù)據(jù)庫連接后,應(yīng)該及時(shí)關(guān)閉它,以便讓其他應(yīng)用程序能夠通過連接池訪問數(shù)據(jù)庫。同時(shí),將對(duì)象置為null也有助于垃圾回收器及時(shí)回收不再使用的資源,以提高應(yīng)用程序的性能和穩(wěn)定性。
總之,本段代碼的作用是在關(guān)閉數(shù)據(jù)庫連接后,將連接對(duì)象置為null,以便垃圾回收器可以及時(shí)回收資源。
最終效果:
文章來源:http://www.zghlxwxcb.cn/news/detail-758558.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-758558.html
到了這里,關(guān)于Unity 制作登錄功能02-創(chuàng)建和鏈接數(shù)據(jù)庫(SQlite)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!