使用Sqlite,一般都是单独创建一个工具类,继承SQLiteOpenHelper。除了重写其onUpgrade、onCreate方法外,再自定义增、删、改、查方法用于对数据库的操作。在使用时,定义工具类的实例即可。
Sqlite一般都和ContentProvider结合使用,ContentProvider中的 onCreate、query、insert、delete、update方法包括方法参数和SQLiteOpenHelper的如出一辙。
本例仅为了展示如何操作sqlite,包括sql语句形式、JavaAPI形式,具体看代码。
以下分了篇章,但代码是连续的。
首先是初始化SQLiteOpenHelper
同时,实例化控件,添加点击监听。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
public class MainActivity extends Activity implements OnClickListener { SQLiteOpenHelper oh; // 数据库操作助手 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 上下文,db库文件名,游标工厂,版本(须大于0) oh = new SQLiteOpenHelper(this, "day13.db", null, 1) { @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if (newVersion > oldVersion) db.execSQL("drop table tb_1;"); // 翻新表 } @Override public void onCreate(SQLiteDatabase db) { // 如果表数据将来用于SimpleCursorAdapter,注意其只识别带下划线的 _id 主键 db.execSQL("CREATE TABLE tb_1 (_id INTEGER PRIMARY KEY, name varchar);"); } }; // 上面4个按钮通过执行sql语句形式操作 findViewById(R.id.btn_delete).setOnClickListener(this); findViewById(R.id.btn_insert).setOnClickListener(this); findViewById(R.id.btn_query).setOnClickListener(this); findViewById(R.id.btn_update).setOnClickListener(this); // 下面4个按钮通过javaapi形式操作 findViewById(R.id.btn_delete_java).setOnClickListener(this); findViewById(R.id.btn_insert_java).setOnClickListener(this); findViewById(R.id.btn_query_java).setOnClickListener(this); findViewById(R.id.btn_update_java).setOnClickListener(this); } |
通过SQL语句操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
@Override public void onClick(View v) { SQLiteDatabase db; switch (v.getId()) { case R.id.btn_delete: db = oh.getWritableDatabase(); db.execSQL("delete from tb_1 where _id = 1"); db.close(); break; case R.id.btn_insert: db = oh.getWritableDatabase(); db.execSQL("insert into tb_1 (name) values ('eminem')"); db.close(); break; case R.id.btn_query: db = oh.getReadableDatabase(); // Cursor cursor = db.rawQuery("select * from tb_1 where _di=?", new String[]{"5"}); Cursor cursor = db.rawQuery("select * from tb_1", null); // 查询结果集 // 遍历每一条结果(如同一个array) while (cursor.moveToNext()) { int _idIndex = cursor.getColumnIndex("_id"); // 得到结果集中此列的索引 int nameIndex = cursor.getColumnIndex("name"); // int id = cursor.getInt(_idIndex); // 得到一条结果中此列的值 String idvalue = cursor.getString(_idIndex); String namevalue = cursor.getString(nameIndex); Log.d("", idvalue + "-" + namevalue); } cursor.close(); db.close(); break; case R.id.btn_update: db = oh.getWritableDatabase(); db.execSQL("update tb_1 set name = 'Alizee' where _id= 1"); db.close(); break; |
通过JavaAPI形式操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
case R.id.btn_delete_java: db = oh.getWritableDatabase(); int num = db.delete("tb_1", "_id = ?", new String[]{"1"}); Log.d("", "影响了行数:" + num); break; case R.id.btn_insert_java: db = oh.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put("name", "小明"); // 列名,值 cv.put("name", "小红"); // 这是个Map,即相同的key,value仅保存最后一条。尽管两次put,但只 name=小红 有效 //cv.put("names", "小小"); // 如果没有指定列,插入失败,返回-1 long nums = db.insert("tb_1", null, cv); // 返回最新插入一条的ID Log.d("", "最后插入行的ID:" + nums); break; case R.id.btn_query_java: db = oh.getReadableDatabase(); // 表,列,where条件,条件值,groupby,having,orderby Cursor cursor2 = db.query("tb_1", new String[]{"_id", "name"}, "1 = 1", null, null, null, null); while(cursor2.moveToNext()){ int idIndex = cursor2.getColumnIndex("_id"); int nameIndex = cursor2.getColumnIndex("name"); String idValue = cursor2.getString(idIndex); String nameValue = cursor2.getString(nameIndex); Log.d("", "id:" + idValue + ", name:" + nameValue); } break; case R.id.btn_update_java: db = oh.getWritableDatabase(); ContentValues cv2 = new ContentValues(); cv2.put("name", "Alizee"); int count = db.update("tb_1", cv2, "_id = ?", new String[]{"2"}); Log.d("", "影响了:" + count); break; } } } |
参考文章 https://www.52pojie.cn/thread-1291455-1-1.html
Sqlite加密
SQLCipher库的使用:https://www.52pojie.cn/thread-1291452-1-1.html
-end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/1607.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设