(每次学习一点xamarin就做个学习笔记和视频来加深记忆巩固知识)
如有不正确的地方,请帮我指正。
SQLite简介
SQLite是一个关系型的数据库,支持标准SQL语法,它是内置在安卓系统中的。
创建数据库和表
安卓提供了一个抽像类SQLiteOpenHelper来建库和表,所以得新建一个类去继承它并重写OnCreate()和OnUpgrade()方法,还得提供抽像类构造方法所需的参数。创建的数据库文件默认放在/data/data/你的packagename/databases/这个目录下。
下面是一段创建库和表的代码:
public class MyDBHelper : SQLiteOpenHelper
{
public const string DBNAME = "MyDB";
private Context _context;
public const string CREATE_BOOK_SQL = @"create table Book (
BookId integer primary key autoincrement,
BookName text,
BookPrice real)";
public MyDBHelper(Context context, SQLiteDatabase.ICursorFactory factory, int version) : base(context, DBNAME, factory, version)
{
_context = context;
}
public override void OnCreate(SQLiteDatabase db)
{
db.ExecSQL(CREATE_BOOK_SQL);
Toast.MakeText(_context, "创建成功。", ToastLength.Short).Show();
}
public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
}
上面这段代码,首先新建了一个MyDBHelper的类继承自抽象类SQLiteOpenHelper,把抽象类构造方法所需的参数给传递了过去,重写了OnCreate()和OnUpgrade()方法。
下面是一段在MainActivity中调用的代码:
public class MainActivity : AppCompatActivity, IOnClickListener
{
private MyDBHelper _myDBHelper;
private SQLiteDatabase _sqLiteDB;
public void OnClick(View v)
{
switch (v.Id)
{
case Resource.Id.button1: //创建库和表
_sqLiteDB = _myDBHelper.WritableDatabase;
break;
}
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
_myDBHelper = new MyDBHelper(this, null, 1);//version改为2就可以使MyDBHelper中的OnUpgrade方法执行
Button btn1 = this.FindViewById<Button>(Resource.Id.button1);
btn1.SetOnClickListener(this);
}
}
首先在MainActivity中的OnCreate方法内实例化了一个MyDBHelper对象,接着在点击事件中通过调用WritebleDatabase属性来创建库和表。还有一个ReadableDatabase也可以创建库和表,这个在数据库不能写入时将以只读方式打开数据库,而WritebleDatabase会出现异常。调用完WritebleDatabase属性后会得到一个SQLiteDatabase类型的对象,就可使用这个对象进行增删改查了。
下面是一段增删改查的代:
private void Add()//添加数据
{
//ContentValues values = new ContentValues();
//values.Put("BookName", "C#入门");
//values.Put("BookPrice", 68.5);
//sqLiteDB.Insert("Book", null, values); //添加第1条数据
//values.Clear();
//values.Put("BookName", "SQL入门");
//values.Put("BookPrice", 37.9);
//sqLiteDB.Insert("Book", null, values); //添加第2条数据
//values.Clear();
_sqLiteDB.ExecSQL("insert into Book(BookName,BookPrice) values(?,?)", new Java.Lang.Object[] { "MySQL入门", "88" });
_sqLiteDB.ExecSQL("insert into Book(BookName,BookPrice) values(?,?)", new Java.Lang.Object[] { "五笔打字学习", "77" });
}
private void Update()//修改数据
{
//ContentValues values = new ContentValues();
//values.Put("BookPrice", 100);
//sqLiteDB.Update("Book", values, "BookName=?", new string[] { "MySQL入门" });
_sqLiteDB.ExecSQL("update Book set BookName=?, BookPrice=? where BookName=?", new Java.Lang.Object[] { "MySQL入门2", "88.88", "MySQL入门" });
}
public void Delete()//删除数据
{
//ContentValues values = new ContentValues();
//sqLiteDB.Delete("Book", "BookPrice>=?", new string[] { "100" });
_sqLiteDB.ExecSQL("delete from Book where BookName=?", new Java.Lang.Object[] { "MySQL入门2" });
}
public void Query()//查询数据
{
//ICursor cursor = sqLiteDB.Query("Book", null, null, null, null, null, null);
ICursor cursor = _sqLiteDB.RawQuery("select * from Book where BookName like ?", new string[] { "%入门%" });
if (cursor.MoveToFirst())
{
do
{
int bookId = cursor.GetInt(cursor.GetColumnIndex("BookId"));
string bookName = cursor.GetString(cursor.GetColumnIndex("BookName"));
double bookPrice = cursor.GetDouble(cursor.GetColumnIndex("BookPrice"));
Log.Debug("MainActivity", "BookId=" + bookId);
Log.Debug("MainActivity", "BookName=" + bookName);
Log.Debug("MainActivity", "BookPrice=" + bookPrice);
} while (cursor.MoveToNext());
}
}
上面的代码中被注释掉的代码是第一种方式,使用ContentValues来组装数据,而没被注释的代码是第二种方式,直接使用SQL语句。
完整代码和视频在我上传的CSDN资源中http://download.csdn.net/download/junshangshui/9926956