(每次學習一點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