Xmarin:增、删、改、查
承接上文:http://www.cnblogs.com/bjxingch/articles/5468033.html
通过继承SQLiteOpenHelper实现对SQLite的操作
LocalSqliteOpenHelper 类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Database;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Database.Sqlite;
using Android.Content;
namespace App2
{
class LocalSqliteOpenHelper:SQLiteOpenHelper
{
/// <summary>
/// base(context, "bjxingch", null, 1)
/// Context: 环境;上下文;来龙去脉。
/// "bjxingch":数据库。
/// null:游标。
/// 1:版本号。
/// </summary>
/// <param name="context"></param>
public LocalSqliteOpenHelper(Context context) : base(context, "bjxingch", null, 1)
{
}
public LocalSqliteOpenHelper(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public LocalSqliteOpenHelper(Context context, string name, SQLiteDatabase.ICursorFactory factory, int version) : base(context, name, factory, version)
{
}
public LocalSqliteOpenHelper(Context context, string name, SQLiteDatabase.ICursorFactory factory, int version, IDatabaseErrorHandler errorHandler) : base(context, name, factory, version, errorHandler)
{
}
/// <summary>
/// 创建表
/// </summary>
/// <param name="db"></param>
public override void OnCreate(SQLiteDatabase db)
{
db.ExecSQL("Create table UserInfo(Id INTEGER PRIMARY KEY,UserName TEXT NOT NULL,PassWord TEXT NOT NULL)");
}
/// <summary>
/// 判断表是否存在,存在要删除并新建
/// </summary>
/// <param name="db"></param>
/// <param name="oldVersion"></param>
/// <param name="newVersion"></param>
public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.ExecSQL("DROP TABLE IF EXISTS UserInfo");
OnCreate(db);
}
}
}
查:通过查询用户名和密码,实现登录
登录界面布局:Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="用户名:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_marginBottom="6.5dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/UserName"
android:layout_marginRight="0.0dp" />
<TextView
android:text="密码:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2" />
<EditText
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/UserPassword" />
<Button
android:text="登陆"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnEnter" />
<TextView
android:text="注册"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtRegister"
android:singleLine="true" />
</LinearLayout>
登录界面查询用户名和密码,代码:
using System;
using Android.App;
using Android.Content;
using Android.Database;
using Android.Database.Sqlite;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace App2
{
[Activity(Label = "App2", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.btnEnter);
EditText editUserName = FindViewById<EditText>(Resource.Id.UserName);
EditText editUserPassword = FindViewById<EditText>(Resource.Id.UserPassword);
TextView txtRegisterTextView = FindViewById<TextView>(Resource.Id.txtRegister);
//button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
button.Click += (sender, e) =>
{
#region 简单登录
//if (editUserName.Text == "sa" && editUserPassword.Text == "123")
//{
// Intent intent = new Intent(this, typeof(Index));
// intent.PutExtra("userName", editUserName.Text);
// StartActivity(intent);
//}
//else
//{
// //提示:登陆失败
// Toast.MakeText(this, "登陆失败!", ToastLength.Long).Show();
//}
#endregion
#region 读取数据库登录
//1、获取用户填写的用户名密码
string userName = editUserName.Text;
string passWord = editUserPassword.Text;
//完成查询操作
LocalSqliteOpenHelper localSqliteOpenHelper=new LocalSqliteOpenHelper(this);
SQLiteDatabase db = localSqliteOpenHelper.WritableDatabase;
string sql = "select UserName from UserInfo where UserName=? and PassWord=?";
ICursor cursor = db.RawQuery(sql,new []{userName,passWord});
//验证登录
if (cursor.MoveToFirst())
{
//登录成功,打开主界面,显示登录成功的当前用户名,获取本行第一列的值
string dbUserName = cursor.GetString(0);
//打开主界面,传递用户名
Intent intent = new Intent(this, typeof(Index));
intent.PutExtra("userName", editUserName.Text);
intent.PutExtra("passWord", passWord);
StartActivity(intent);
}
else
{
//登录失败
Toast.MakeText(this,"登录失败!",ToastLength.Short).Show();
}
cursor.Close();
db.Close();
editUserName.Text = "";
editUserPassword.Text = "";
#endregion
};
txtRegisterTextView.Click += (sender, e) =>
{
Intent intent = new Intent(this, typeof (Register));
intent.PutExtra("userName", editUserName.Text);
StartActivity(intent);
};
}
}
}
增:通过注册用户来实现增加用户。
新增用户,界面布局:Register.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout1">
<TextView
android:text="用户名:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtRegUserName" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editRegUserName" />
<TextView
android:text="密码:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtRegPassword" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editRegPassword" />
<TextView
android:text="确认密码:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtRegConfigPassword" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editRegConfigPassword" />
<Button
android:text="注册"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnRegister" />
</LinearLayout>
新增用户代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Database.Sqlite;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace App2
{
[Activity(Label = "Register")]
public class Register : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Register);
// Create your application here
EditText editRegUserName = FindViewById<EditText>(Resource.Id.editRegUserName);
EditText editRegPassword = FindViewById<EditText>(Resource.Id.editRegPassword);
EditText editRegConfigPassword = FindViewById<EditText>(Resource.Id.editRegConfigPassword);
Button btnRegister = FindViewById<Button>(Resource.Id.btnRegister);
btnRegister.Click += (sender, e) =>
{
//获取文本值
var userName = editRegUserName.Text;
var passWord = editRegPassword.Text;
var passWordCon = editRegConfigPassword.Text;
if (passWord == passWordCon)
{
//注册功能 insert into
LocalSqliteOpenHelper localSqliteOpenHelper = new LocalSqliteOpenHelper(this);
SQLiteDatabase db = localSqliteOpenHelper.WritableDatabase;
db.ExecSQL("insert intO UserInfo values(null,?,?)", new Java.Lang.Object[] {userName, passWord});
db.Close();
Toast.MakeText(this, "注册成功!", ToastLength.Short).Show();
Intent intent=new Intent(this,typeof(MainActivity));
StartActivity(intent);
}
else
{
//错误提示
Toast.MakeText(this, "确认密码与密码不一致,请重填", ToastLength.Short).Show();
}
};
}
}
}
删:在当前数据库中删除当前用户

删:界面布局 Index.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner1" />
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
<Button
android:text="删除"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnDelete" />
<Button
android:text="修改"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnModPassword" />
</LinearLayout>
删:代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Database.Sqlite;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace App2
{
[Activity(Label = "Index")]
public class Index : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Index);
// Create your application here
string _password = Intent.GetStringExtra("passWord");
TextView textView = FindViewById<TextView>(Resource.Id.textView1);
//获取消息对象
Spinner _spinner = FindViewById<Spinner>(Resource.Id.spinner1);
//声明配置器
ArrayAdapter arrayAdapter=new ArrayAdapter(this,Android.Resource.Layout.SimpleSpinnerDropDownItem);
arrayAdapter.Add("北京");
arrayAdapter.Add("上海");
arrayAdapter.Add("广州");
arrayAdapter.Add("深圳");
//设置适配器
_spinner.Adapter = arrayAdapter;
_spinner.ItemSelected += (sender, e) =>
{
textView.Text = arrayAdapter.GetItem(e.Position).ToString();
textView.Text = "当前用户是:" + Intent.GetStringExtra("userName");
};
Button btnDelete = FindViewById<Button>(Resource.Id.btnDelete);
Button btnModPassword = FindViewById<Button>(Resource.Id.btnModPassword);
btnModPassword.Click += (sender, e) =>
{
Intent intent=new Intent(this,typeof(ModPassword));
intent.PutExtra("UserName", textView.Text);
intent.PutExtra("password", _password);
StartActivity(intent);
};
btnDelete.Click += (sender,e) =>
{
//1、获取当前用户
string userName = Intent.GetStringExtra("userName");
//2、删除功能
LocalSqliteOpenHelper localSqliteOpenHelper=new LocalSqliteOpenHelper(this);
SQLiteDatabase db = localSqliteOpenHelper.WritableDatabase;
db.ExecSQL("delete from UserInfo where UserName=?",new Java.Lang.Object[] {userName});
db.Close();
Toast.MakeText(this,"用户:"+userName+"被删除",ToastLength.Short).Show();
};
}
}
}
改:修改用户密码。
改:布局,ModPassword.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="当前用户:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtCurUserName" />
<TextView
android:text="当前密码:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtCurPassword" />
<TextView
android:text="新密码:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView3" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editNewPassword" />
<TextView
android:text="确认密码:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView4" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editNewConfigPassword" />
<Button
android:text="确认修改"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnConUpdate" />
</LinearLayout>
改:代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Database.Sqlite;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.Lang;
namespace App2
{
[Activity(Label = "ModPassword")]
public class ModPassword : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.ModPassword);
TextView _txtCurUserName = FindViewById<TextView>(Resource.Id.txtCurUserName);
TextView _txtCurPassword = FindViewById<TextView>(Resource.Id.txtCurPassword);
string strUserName = Intent.GetStringExtra("UserName");
string strPassWord = Intent.GetStringExtra("password");
_txtCurUserName.Text = "当前用户:"+ strUserName;
_txtCurPassword.Text = "当前密码:"+ strPassWord;
EditText _editNewPassword = FindViewById<EditText>(Resource.Id.editNewPassword);
EditText _editNewConfigPassword = FindViewById<EditText>(Resource.Id.editNewConfigPassword);
Button _btnConUpdate = FindViewById<Button>(Resource.Id.btnConUpdate);
_btnConUpdate.Click +=(sender,e)=>
{
if (_editNewPassword.Text== _editNewConfigPassword.Text)
{
LocalSqliteOpenHelper localSqliteOpenHelper=new LocalSqliteOpenHelper(this);
SQLiteDatabase db = localSqliteOpenHelper.WritableDatabase;
string sql = "UPDATE UserInfo SET PassWord =? WHERE UserName =? ";
db.ExecSQL(sql,new Java.Lang.Object[] { strPassWord, strUserName });
db.Close();
Toast.MakeText(this, "密码修改成功!", ToastLength.Short).Show();
}
else
{
Toast.MakeText(this,"两次输入的密码不匹配~!",ToastLength.Short).Show();
}
};
}
}
}
风中代表自由、寻觅代表不断前进~!

浙公网安备 33010602011771号