Unity3D SQLlite的高级用法
2021/7/18 2:05:59
本文主要是介绍Unity3D SQLlite的高级用法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
SQLlite的高级用法=>用更简洁的方式去完成增删改查四个功能
一、创建表
public void CreatUserTabel() { if (connection!=null) { 接收 //SQLiteCommand liteCommand= connection.CreateCommand("CREATE TABLE User(UserName varchar(255),Password varchar(255))"); 执行 //liteCommand.ExecuteNonQuery(); connection.CreateTable<User>(); } }
二、增加数据
public void InsertUser() { if (connection!=null) { //SQLiteCommand liteCommand = connection.CreateCommand("INSERT INTO User VALUES("+"\"admin1\",\"111111\")"); //liteCommand.ExecuteNonQuery(); User user = new User() { UserName = "admin1", Password = "11111" }; connection.Insert(user); } }
三、删除数据
public void UpdateUser() { if (connection!=null) { //SQLiteCommand liteCommand = connection.CreateCommand("UPDATE User SET Password=\"22222\"WHERE UserName=\"admin1\""); //liteCommand.ExecuteNonQuery(); User user = new User() { UserName = "admin", Password = "22222" }; connection.Update(user); } }
四、查询数据
public void SelecUser() { if (connection!=null) { //SQLiteCommand liteCommand = connection.CreateCommand("SELECT * FROM User"); //List<User> list = liteCommand.ExecuteQuery<User>(); TableQuery<User> user = connection.Table<User>(); foreach (var item in user) { print("UserName:" + item.UserName + ",Password:" + item.Password); } } } public void SelectUserById() { if (connection!=null) { TableQuery<User> user = connection.Table<User>().Where(t => t.UserName == "admin"); foreach (var item in user) { print("UserName:" + item.UserName + ",Password:" + item.Password); } } }
五、修改数据
(1)对数据模块User进行修改(添加主键)
using System.Collections; using System.Collections.Generic; using SQLite4Unity3d; public class User { [PrimaryKey] public string UserName { get; set; } public string Password { get; set; } }
(2)修改数据
public void UpdateUser() { if (connection!=null) { //SQLiteCommand liteCommand = connection.CreateCommand("UPDATE User SET Password=\"22222\"WHERE UserName=\"admin1\""); //liteCommand.ExecuteNonQuery(); User user = new User() { UserName = "admin", Password = "22222" }; connection.Update(user); } }
六、整体代码展示
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(); connection.CreateTable<User>(); } } //数据库添加数据 public void InsertUser() { if (connection!=null) { //SQLiteCommand liteCommand = connection.CreateCommand("INSERT INTO User VALUES("+"\"admin1\",\"111111\")"); //liteCommand.ExecuteNonQuery(); User user = new User() { UserName = "admin1", Password = "11111" }; connection.Insert(user); } } //查询数据库数据 public void SelecUser() { if (connection!=null) { //SQLiteCommand liteCommand = connection.CreateCommand("SELECT * FROM User"); //List<User> list = liteCommand.ExecuteQuery<User>(); TableQuery<User> user = connection.Table<User>(); foreach (var item in user) { print("UserName:" + item.UserName + ",Password:" + item.Password); } } } public void SelectUserById() { if (connection!=null) { TableQuery<User> user = connection.Table<User>().Where(t => t.UserName == "admin"); foreach (var item in user) { print("UserName:" + item.UserName + ",Password:" + item.Password); } } } //修改数据库数据 public void UpdateUser() { if (connection!=null) { //SQLiteCommand liteCommand = connection.CreateCommand("UPDATE User SET Password=\"22222\"WHERE UserName=\"admin1\""); //liteCommand.ExecuteNonQuery(); User user = new User() { UserName = "admin", Password = "22222" }; connection.Update(user); } } //删除数据库数据 public void DeleteUser() { if (connection!=null) { //SQLiteCommand liteCommand = connection.CreateCommand("DELETE FROM User WHERE UserName=\"admin1\""); //liteCommand.ExecuteNonQuery(); connection.Delete<User>("admin"); } } //断开连接 public void OnDestroy() { if (connection!=null) { connection.Close(); connection.Dispose(); GC.Collect(); } } private void Start() { CreatSQLite(); } }
这篇关于Unity3D SQLlite的高级用法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2023-10-15【Unity3D】Shader Graph简介
- 2023-06-19【Unity3D】魔方
- 2022-08-10【学习打卡】第六天 Unity3D地形编辑器
- 2022-03-21Unity3d 基于xlua热更新实现系列二:Xlua脚本动态加载(半热更新)(含源码)
- 2022-02-24Unity3d FPS射击游戏案例 - 消灭病毒
- 2022-02-11Unity3D 升级2020版VR开发的一些问题
- 2022-01-25unity3D学习笔记1
- 2021-12-14Unity3d笔记
- 2021-12-05Unity3D的RPG游戏中攻击的判定
- 2021-10-15【Unity3D】多个Button(UI)共用一个响应函数