Use Sql database in Unity to realize user login and registration

2017.10.21
The user login function is very common. What we need to do now is to let the user enter the user name and password, connect to the database, and then log in. This version is only a local database, and now I can only log in. The registration function will be added later. -_-||
2017.10.22
I didn't expect to push out the registration function so soon, haha. . . .
Just put the registration code here:

//User registration confirmation button
public void ConfirmBtn()
{ Debug.Log (ReginsterIDText .text); string s = "select ID from Works where ID = '" + ReginsterIDText .text + "'"; if (ContainName (ReginsterIDText .text )) { ResginsterState.text = "User already exists"; } else { ResginsterState.text = "Can register"; Debug.Log("No such person can register"); string[] Colvalues ​​= {ReginsterIDText .text ,ReginsterPasswordText . text }; InsertValues("Works",Colvalues); }









    Debug.Log ("用户注册确认按钮");
}

Registration method: To put it bluntly, insert data
protected void InsertValues ​​(string tableName, string [] ColValues)
{ string queryString = “INSERT INTO " + tableName + " VALUES ('" + ColValues[0]; for (int i = 1; i < ColValues.Length; i++) { queryString += "','" + ColValues[i]; } queryString += "')"; try { command.CommandText = queryString; command.ExecuteNonQuery (); } catch (System .Exception ex) { Debug.Log (ex); } } Okay, let's go to the project directly, first create a new project, the name doesn't matter. Create a new canvas, create an image, two input text boxes, one text and one button in the canvas. After finishing, as shown in the figure:













insert image description here

Create a new .PNG like this
and create two new folders: Plugins and StreamingAssets.
Import the Mono.Data.Sqlite file and sqlite3 file into the Plugins folder. If your unity version is 5.5.0+, you also need to add System. Data file.
sqlite3 file: http://pan.baidu.com/s/1bpvsI4J
The other two files can be found in the Unity installation folder.
Then the database file is placed in the StreamingAssets folder. First of all, how to build the database? Similarly, share a software for building a database: http://pan.baidu.com/s/1mhAtsTU
After installation, open this software and create a new sql data table.
It contains username and password.
Set up as shown in the figure:
insert image description here

Database Table.PNG
Save the database you created and return to Unity.
Create a new script named "denglu" and post the source code here for everyone to paste and test:
using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;
using UnityEngine.UI;
public class denglu : MonoBehaviour {

//数据库连接对象,通过该对象与数据库文件所在的路径
//进行连接,进而打开数据库文件
SqliteConnection con;
//数据库文件所在的路径
string path;
//数据库命令
SqliteCommand command;

SqliteDataReader reader;
//以下是登录练习的成员数据
public InputField input_name;
public InputField input_pwd;
public Text show;
new string name;
string pwd;
object obj;

void Start()
{
    //要连接的数据库文件路径
    /* path = "Data Source = " + Application.streamingAssetsPath
    + "/shunity.sqlite";*/
    path = "Data Source = " + Application.streamingAssetsPath
        + "/User.sqlite";
    //通过路径创建出连接对象
    con = new SqliteConnection(path);
    //打开数据库文件
    con.Open();
    command = con.CreateCommand();
    //Test();
}

void Test()
{
    #region 第一种执行方法,用在增/删/改
    //数据库语句
    //        command.CommandText = "insert into hero values('张三',10,20,1)";
    //该方法用在增/删/改语句
    //        int count = command.ExecuteNonQuery();

    #endregion
    #region 第二种执行方法,该方法用在查询结果只有一个的情况
    //        command.CommandText = "select *from hero";
    //        object obj = command.ExecuteScalar();
    //        Debug.Log(obj);
    #endregion
    #region 第三种方法
    command.CommandText = "select *from hero";
    reader = command.ExecuteReader();
    //如果读取了下一行,返回值为true,否则为false
    while (reader.Read())
    {
        //把每一行的每一列读取出来
        for (int i = 0; i < reader.FieldCount; i++)
        {
            Debug.Log(reader.GetName(i));
            //print(reader.GetValue(i).ToString() + " ");
        }
        print("\n");
    }
    #endregion
}

public void OnLoginClick()
{
    if (ContainName() == false)
    {
        show.text = "你输入的名字不正确";
    }
    else if (CorrectPwd() == false)
    {
        show.text = "你输入的密码不正确";
    }
    else
    {
        show.text = "登录成功";

    }
}

bool ContainName()
{
    name = input_name.text;
    command.CommandText = "select pwd from usertable where uname = '" + name + "'";
    obj = command.ExecuteScalar();
    if (obj != null)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool CorrectPwd()
{
    pwd = input_pwd.text;
    if (ContainName())
    {
        if (pwd == obj.ToString())
        {
            return true;
        }
    }
    return false;
}

}
Save and exit, create a new empty object, hang this script on it, and assign values ​​to the Ui inside.
Then run the project:
insert image description here

Login successful interface
Login failed interface
insert image description here

Well, the introduction of the login is here, and the registration will be written in a few days or a few months.

Guess you like

Origin blog.csdn.net/weixin_41590778/article/details/129355068