5.24数据库应用

usingUnityEngine;

usingSystem.Collections;

usingUnityEngine.UI;

usingMono.Data.Sqlite;

publicclassLoginScript:MonoBehaviour{

public InputField  accountText;

public  InputField  passwordText;

Text   showText;

SqliteConnection   connection;

SqliteCommand   command;

SqliteDataReader  reader;

string    dbPath;

voidStart( ){

showText=GameObject.Find("ShowText").GetComponent<Text>( );

dbPath="DataSource="+Application.streamingAssetsPath+"/UserDatabase.sqlite";

connection=new  SqliteConnection(dbPath);

connection.Open( );

command=connection.CreateCommand( );

}


publicvoidButtonPressed( ){

Login( );

}

public void ButtonPressed_1( ){

Reset( );

}

public void ButtonPressed_2( ){

Registered( );

print("sfd");

}

public void Login( ){

string account=accountText.text;

string password=passwordText.text;

if(account.Length>0&&password.Length>0){

//找到与数据库中与account相同的uname

command.CommandText="SELECT*FROM UserTable WHERE uname="+"'"+account+"'";

object  obj=command.ExecuteScalar( );

if(obj!=null){

if(password==obj.ToString()){

print("sdf");

showText.text="登陆成功";

}else{

showText.text="密码错误";

}

}else{

showText.text="用户名不存在";

}

}

}

voidReset( ){

accountText.text="";

passwordText.text="";

showText.text="";

command.CommandText="DELETE FROM UserTable WHERE  rowid>0";

try{

command.ExecuteNonQuery();

}catch(SqliteExceptionex){

print(ex.Message);

}

}

voidRegistered( ){

command.CommandText="INSERTINTOUserTable(pwd,uname)VALUES("+"'"+passwordText.text+"'"+","+"'"+accountText.text+"'"+")";

command.ExecuteNonQuery( );

print("5612");

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容