unity之SQLite学习总结
发布日期:2022-02-26 00:17:41 浏览次数:8 分类:技术文章

本文共 4076 字,大约阅读时间需要 13 分钟。

SQLite学习:

★语法:(建增删改查)

☆建:create table 表名(列名1 数据类型,列名2 数据类型,……) ->数据类型要符合SQLite的语法规定,与c#等不同。

☆增:1.insert into 表名 values(该列值,该列值,……)        

    2.insert into 表名(列1,列2,……)values(列1值,列2值,……)

☆删:1.delete from 表名                          ->删除整个表的内容

    2.delet from 表名 where 列=该列值                -> 删除 列=该列值 的

☆改:update 表名 set 列名=新值 where 列名=旧值

☆查:1.select 列名 from 表名                       ->查找列名对应的值

    2.select * from 表名 where 列名=某值              -> 查找某值所在的整行数据

    3.select 列1,列2…… from 表名 where 某列名=某值      ->查找某值所在行的列1,列2甚至更多

★数据库在Editor和Android的应用:

平台路径:

声明数据库链接对象(Connection)以及声明数据库指令(Command)在实例中表达。

执行SQL语句的三种方式:

☆准备工作:

1.发布到Android 、PC以及在Editor的应用:

Editor下:Plugins没有Android的文件夹;

2.引用命名空间:using.Mono.Data.Sqlite;

☆实例:

Editor:

Android:

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;using UnityEngine.SceneManagement;using Mono.Data.Sqlite;using System;using System.IO;public class MenuUI : MonoBehaviour {    public InputField UserName;    public InputField PassWord;    public Button Regist;    public Button Login;    public Text Tips;    //创建一个数据库链接对象    SqliteConnection con;    string path;    int id;    //sqlite语句对象、数据库命令/指令,它是sql语句的载体    SqliteCommand command;	// Use this for initialization	void Awake () {#if UNITY_EDITOR        path = "Data Source = " + Application.streamingAssetsPath + "/SQLData/user.sqlite";#elif UNITY_ANDROID        string localPath = "jar:file://" + Application.dataPath + "!/assets" + "/SQLData/user.sqlite";        string newPath = Application.persistentDataPath + "/user.sqlite";        //判断新目录的文件是否存在,存在的话直接对数据库进行操作,不存在的话,需要将localPath中的数据库文件拷贝到newPath中        if (!File.Exists(newPath))        {            WWW www = new WWW(localPath);            while (!www.isDone) {  }            //在手机沙盒中创建新的文件            File.WriteAllBytes(newPath, www.bytes);        }        path = "URI=file:" + newPath;#endif        //通过路径创建链接对象        con = new SqliteConnection(path);        con.Open();        //创建数据库指令        command = con.CreateCommand();        id=PlayerPrefs.GetInt("ID",1000);    }    void ClickLogin()    {        command.CommandText = "select password from UserData where name = '"+UserName.text+"'";        object obj = command.ExecuteScalar();        if (obj == null)        {            ShowTips("密码不能为空");        }        else if (obj.ToString() != PassWord.text)        {            ShowTips("密码错误");        }        else        {            ShowTips("登录成功");            command.CommandText = "select id from UserData where name = '" + UserName.text + "'";            obj = command.ExecuteScalar();            int id = System.Convert.ToInt32(obj);            UserData.ID = id;            UserData.NAME = UserName.text;            SceneManager.LoadScene("18_1_4_2");        }    }    private void ClickRegist()    {        command.CommandText = "select name from UserData where name = '" + UserName.text + "'";        object obj = command.ExecuteScalar();        if (obj!= null)        {            ShowTips("用户名已被占用");        }        else         {            if (PassWord.text == "")            {                ShowTips("密码不能为空");            }            else            {                print(111);                id++;                PlayerPrefs.SetInt("ID",id);                int num;                command.CommandText = "insert into UserData values(" + id + ",'" + UserName.text + "','" + PassWord.text + "')";                             //string.Format("insert into UserData values({0},'{1}','{2}')",id,UserName.text,PassWord.text);                num=command.ExecuteNonQuery();                print(num);                ShowTips("注册成功");            }        }    }    private void ShowTips(string v)    {        Tips.text = v;    }        private void OnEnable()    {        Login.onClick.AddListener(ClickLogin);        Regist.onClick.AddListener(ClickRegist);    }    private void OnDisable()    {        Login.onClick.RemoveListener(ClickLogin);        Regist.onClick.RemoveListener(ClickRegist);    }    private void OnDestroy()    {                //销毁数据库命令        command.Dispose();        //关闭数据库        con.Close();        //销毁链接对象        con.Dispose();    }}

转载地址:https://blog.csdn.net/TL_1203/article/details/78997719 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Unity之XML的学习总结:
下一篇:Unity之AssetBundle学习总结:

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月16日 03时26分15秒