SQLiteDatabase db = getContext().openOrCreateDatabase("Mdb.db", MODE_PRIVATE, null);
//数据库对象 getContext是因为我是继承了其他类,一般都用this,参数("数据库名称","数据库打开方式",一般为null")
db.execSQL("create table if not exists TableNmae__ (String_1 varchar(20), String2 varchar(20))");
//执行语句,可用于增删改,删表之类的操作,但是它没有返回值,
db.execSQL("insert into TableNmae__ values(?,?)", new Object[]{"SS", "AA"});
//只是更改了数据库的操作语句,后面跟的是附带参数,?是占位符,也可以执行增删改,这里不做详细说明
Cursor cursor = db.rawQuery("select * from TableNmae__", null);
//查询数据,数据临时储存在Cursor游标中
//以上便是数据库的基本操作了,4行代码,没骗你吧。
//下面是游标的操作,
//下面是转换成可用的ArrayList<Map<String,Object>>;
ArrayList<Map<String, Object>> Arr = new ArrayList<Map<String, Object>>();
//申明对象
while (cursor.moveToNext()) {//逐条读取,是不是感觉很像SQLServer的断开式查询呢?
Map<String, Object> Maps = new HashMap<String, Object>();
//Map对象,放到while外面会很不一样的,可以进行尝试,C#中放到外面和里面没有区别。
Maps.put("String1", cursor.getString(cursor.getColumnIndex("String_1")));
//保存数据, getString获取数据参数为下标,getColumnIndex为获取下标参数为数据库列名
Maps.put("String2", cursor.getString(cursor.getColumnIndex("String2")));
//同上
Arr.add(Maps);
//添加数据至ArrayList
//cursor.getString(cursor.getColumnIndex("String_1"))可以直接获取当前行的String_1列
}
//拿到数据想干嘛就干嘛