Unity3D SQLlite的高级用法

2021/7/18 2:05:59

本文主要是介绍Unity3D SQLlite的高级用法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

SQLlite的高级用法=>用更简洁的方式去完成增删改查四个功能

一、创建表

1
2
3
4
5
6
7
8
9
10
11
public void CreatUserTabel()
{
    if (connection!=null)
    {
        接收
        //SQLiteCommand liteCommand= connection.CreateCommand("CREATE TABLE User(UserName varchar(255),Password varchar(255))");
        执行
        //liteCommand.ExecuteNonQuery();
        connection.CreateTable<User>();
    }
}

二、增加数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
    }
}

三、删除数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
    }
}

四、查询数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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进行修改(添加主键)

1
2
3
4
5
6
7
8
9
10
using System.Collections;
using System.Collections.Generic;
using SQLite4Unity3d;
 
public class User
{
    [PrimaryKey]
    public string UserName { get; set; }
    public string Password { get; set; }
}

(2)修改数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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);
    }
}

 六、整体代码展示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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的高级用法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


原文链接: https://blog.csdn.net/qq_48091873/article/details/118864293
扫一扫关注最新编程教程