记使用Android SQLite遇到的小问题
来源:互联网 发布:2017七天网络成绩查询 编辑:程序博客网 时间:2024/05/01 10:55
引用请标明出处:http://blog.csdn.net/callon_h/article/details/51428341
之前一直是遇到activity需要保存数据就直接使用SQLite就好,但是有一天突然发现,我需要在其中一个Activity里访问另一个Activity建立的SQLiteDatabase,从而引出了该文。
查了很多资料,大多数都是说,使用单例模式建立继承SQLiteOpenHelper的类,然后使用静态方法(一般取名getInstance)来获得。自己亲自尝试了,发现总是不对,于是找了一种更简便更懒的方法(直接用文件位置引用数据库):
SettingActivity.java
db = openOrCreateDatabase("callon_set_pump", Context.MODE_PRIVATE, null); if(tabbleIsExist("table1")){ DatabaseHelper dbHelper = new DatabaseHelper(SettingActivity.this, "callon_set_pump"); SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.query("table1", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); //add your code here } cursor = db.query("table1", new String[]{"id","name"}, "id=?", new String[]{"2"}, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); //add your code here } cursor = db.query("table1", new String[]{"id","name"}, "id=?", new String[]{"3"}, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); //add your code here } cursor = db.query("table1", new String[]{"id","name"}, "id=?", new String[]{"4"}, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); //add your code here } cursor = db.query("table1", new String[]{"id","name"}, "id=?", new String[]{"5"}, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); //add your code here } } else { db.execSQL("CREATE TABLE table1(id int,name varchar(20))"); ContentValues values = new ContentValues(); values.put("id", 1); values.put("name", "5000"); db.insert("table1", null, values); values = new ContentValues(); values.put("id", 2); values.put("name", "1000"); db.insert("table1", null, values); values = new ContentValues(); values.put("id", 3); values.put("name", "3"); db.insert("table1", null, values); values = new ContentValues(); values.put("id", 4); values.put("name", "3"); db.insert("table1", null, values); values = new ContentValues(); values.put("id", 5); values.put("name", "NONE"); db.insert("table1", null, values); //add your code here }
上述类建立了一个数据库callon_set_pump,在另一个类中使用的话,
PageActivity.java
db_set = SQLiteDatabase.openOrCreateDatabase("data/data/com.autopump.callon.nenu/databases/callon_set_pump",null); Cursor cursor = db_set.query("table1", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); System.out.println("name is :"+ name); }
关于tabbleIsExist方法,它是用来检测是否存在该数据库的,如果存在重复创建会出现异常的哦~
private boolean tabbleIsExist(String tableName){ boolean result = false; if(tableName == null){ return false; } SQLiteDatabase db = null; DatabaseHelper dbHelper = new DatabaseHelper(SettingActivity.this, "callon_set_pump"); Cursor cursor = null; try { db = dbHelper.getReadableDatabase(); String sql = "select count(*) as c from Sqlite_master where type ='table' and name ='"+tableName.trim()+"' "; cursor = db.rawQuery(sql, null); if(cursor.moveToNext()){ int count = cursor.getInt(0); if(count>0){ result = true; } } } catch (Exception e) { // TODO: handle exception } return result; }
总之能抓到耗子的就是好猫吧,虽然方法不像什么单例模式那样洋气,希望对大家有帮助。
2 0
- 记使用Android SQLite遇到的小问题
- 使用sqlite遇到的问题
- unity 使用 sqlite 数据库遇到的问题 unity android 使用sqlite
- android应用sqlite遇到的问题
- Android遇到的小问题
- android theme主题使用遇到的一个小问题
- Android SQLite数据库使用的小总结
- 每日一记-Git使用中遇到的小问题
- Android开发遇到的小问题
- android开发遇到的小问题备忘
- android菜鸟开发遇到的小问题
- 遇到的一些Android小问题
- android开发遇到的一个小问题
- 初识Android遇到的小问题
- Android开发中遇到的小问题
- Android Studio遇到的一些小问题
- Android(二)遇到的小问题
- android 遇到的一个小问题
- CentOS 7 安装好后,无法使用小键盘的解决办法
- ACM:搜索算法专题(1)——24点
- Android 连续点击实现一些隐蔽功能
- int main( int argc ,char** argv) windows下如何传递参数
- [剑指offer]正则表达式匹配
- 记使用Android SQLite遇到的小问题
- Android开发自定义控件实现一个圆形进度条【带数值和动画】
- pitch yaw roll
- 整数解
- 前端---CSS中display-block & display-inline
- 如何动态显示图片
- iOS - 利用pch文件在程序发布时统一去除打印代码
- Spring IoC
- MSDN关于I/O完成端口