Android初始化本地数据库
原文:http://blog.csdn.net/itjavawfc/article/details/50860647
點(diǎn)擊閱讀原文
--------------------------------
最近遇到一個(gè)需求,一個(gè)同學(xué)不會(huì)搭服務(wù)器,但是Android課程設(shè)計(jì)需要用到很多數(shù)據(jù),這樣就出現(xiàn)了一個(gè)問(wèn)題,一個(gè)內(nèi)容型的APP,怎么初始化數(shù)據(jù)庫(kù)呢??
很容易想到倆種方案,一個(gè)是在App中寫(xiě)一個(gè)錄入內(nèi)容的頁(yè)面,用手動(dòng)的方式錄入數(shù)據(jù);另一個(gè)是在外部建立一個(gè)數(shù)據(jù)庫(kù),用數(shù)據(jù)庫(kù)工具錄入;第一種方案太慢,太麻煩,手動(dòng)的錄入簡(jiǎn)直就能把人折磨死,所以最后采用了外部建庫(kù)的方式,用navicate for sqlite 很容易建立了一個(gè)數(shù)據(jù)庫(kù)?
得到了mydb.db數(shù)據(jù)庫(kù),那么怎么導(dǎo)入到Android App中尼,用下面的方案:?
1.將mydb.db 放到raw文件夾下?
2.編寫(xiě)將mydb.db 復(fù)制到sd卡中的工具類(lèi)并獲得SQLiteDatabase 的工具類(lèi)
- 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.在數(shù)據(jù)中查詢(xún)的時(shí)候:直接用工具類(lèi)獲得SQLiteDatabase來(lái)查詢(xún)
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
就可以直接查詢(xún)。
-------------
更多的Java,Angular,Android,大數(shù)據(jù),J2EE,Python,數(shù)據(jù)庫(kù),Linux,Java架構(gòu)師,:
http://www.cnblogs.com/zengmiaogen/p/7083694.html
總結(jié)
以上是生活随笔為你收集整理的Android初始化本地数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java并发编程之线程定时器Schedu
- 下一篇: Android之back键拦截处理