Android初始化本地数据库
原文:http://blog.csdn.net/itjavawfc/article/details/50860647
點擊閱讀原文
--------------------------------
最近遇到一個需求,一個同學不會搭服務器,但是Android課程設計需要用到很多數據,這樣就出現了一個問題,一個內容型的APP,怎么初始化數據庫呢??
很容易想到倆種方案,一個是在App中寫一個錄入內容的頁面,用手動的方式錄入數據;另一個是在外部建立一個數據庫,用數據庫工具錄入;第一種方案太慢,太麻煩,手動的錄入簡直就能把人折磨死,所以最后采用了外部建庫的方式,用navicate for sqlite 很容易建立了一個數據庫?
得到了mydb.db數據庫,那么怎么導入到Android App中尼,用下面的方案:?
1.將mydb.db 放到raw文件夾下?
2.編寫將mydb.db 復制到sd卡中的工具類并獲得SQLiteDatabase 的工具類
- 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
- 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
3.在數據中查詢的時候:直接用工具類獲得SQLiteDatabase來查詢
public static List<Season> findAllSeason(Context context) { List<Season> seasons = new ArrayList<Season>(); SQLiteDatabase myDateBase = DBUtil.openDatabase(context); String sql = "select * from Season"; try { Cursor c = myDateBase.rawQuery(sql, null); c.moveToFirst(); while (!c.isAfterLast()) { Season season = new Season(); season.setId(c.getInt(c.getColumnIndex("id"))); season.setName(c.getString(c.getColumnIndex("name"))); seasons.add(season); c.moveToNext(); } if (!c.isClosed()) { c.close(); } if (myDateBase.isOpen()) { myDateBase.close(); } } catch (Exception e) { e.printStackTrace(); } return seasons; }- 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
- 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
就可以直接查詢。
-------------
更多的Java,Angular,Android,大數據,J2EE,Python,數據庫,Linux,Java架構師,:
http://www.cnblogs.com/zengmiaogen/p/7083694.html
總結
以上是生活随笔為你收集整理的Android初始化本地数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java并发编程之线程定时器Schedu
- 下一篇: android sina oauth2.