初步实现登录数据创建、保存和查找的可视化功能

 

 

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using Mono.Data.Sqlite;
using System.IO;
/// <summary>
/// perfect!
/// 初步实现登录数据创建、保存和查找的可视化功能
/// </summary>
public class ZYLogin : MonoBehaviour {

public Text lookupText;
public Text resultText;

bool isShow = false;
public GameObject checkInBg;


public Text nameText;
public Text ageText;
public GameObject loginBg;

string kidName;
string kidAge;


#region 创建表
void CreateMyTable()
{
string appDBPath = Application.persistentDataPath + "/AKid.db";
if (!File.Exists(appDBPath))
{
try
{
string tempStr = "URI=file:" + appDBPath;
DbAccess db = new DbAccess("URI=file:" + appDBPath);
tempStr = "Loading..................";
db.CreateTable("kid", new string[] { "name","age" }, new string[] { "text" ,"text"});
db.CloseSqlConnection();
Debug.Log("----------------------CreateTable is successful!!");
}
catch
{
Debug.Log("--------------------U Had Fail to CreateTable!!");
}
}
else
{
Debug.Log("----------------------------This DB had existsed!!!");
}
}
#endregion

#region 插入数据
void InsetMyData()
{
string appDBPath = Application.persistentDataPath + "/AKid.db";
DbAccess db = new DbAccess("URI=file:" + appDBPath);
db.InsertInto("kid", new string[] { nameText.text, ageText.text });
Debug.Log("nameText.text is" + nameText.text + "ageText.text is" + ageText.text);
db.CloseSqlConnection();
Debug.Log("-------------------------InsetMyData is successful!!");
}
#endregion

#region 查找数据
public void SearchToSql()
{
string appDBPath = Application.persistentDataPath + "/AKid.db";
DbAccess db = new DbAccess("URI=file:" + appDBPath);
SqliteDataReader sqReader = db.SelectWhere("kid", new string[] { "name", "age" }, new string[] { "name" }, new string[] { "=" }, new string[] { lookupText.text });
Debug.Log("------------------- lookupText.text is" + lookupText.text);
while (sqReader.Read())
{
//判读是否找到了相关数据
if (sqReader.GetString(sqReader.GetOrdinal("name")).Equals(lookupText.text))
{
kidName = sqReader.GetString(sqReader.GetOrdinal("name"));
kidAge = sqReader.GetString(sqReader.GetOrdinal("age"));
Debug.Log("找到了数据");
Debug.Log("--------------------------kidName is" + kidName + "kidAge is" + kidAge);
}
}
sqReader.Close();
if (lookupText.text == kidName)
{
resultText.text = kidName + " " + kidAge;
Debug.Log("---------------------------- lookupText.text is" + lookupText.text + "kidName is" + kidName + "kidAge is" + kidAge);
}
else
{

resultText.text = "未找到相关数据";
}

db.CloseSqlConnection();
}
#endregion


public void OnSaveButtonClick()
{
InsetMyData();
loginBg.SetActive(false);
}

public void OnCheckBtnClick()
{
CreateMyTable();
}


//实现注册界面的循环点击出现和隐藏
void Show()
{
isShow = true;
checkInBg.SetActive(true);
}

void Hide()
{
isShow = false;
checkInBg.SetActive(false);
}
public void TranformState()
{
if (isShow == false)
{
Show();
}
else
{
Hide();
}
}

}

posted @ 2016-06-29 09:54  Fei非非  阅读(187)  评论(0编辑  收藏  举报