日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Android初始化本地数据库

發(fā)布時(shí)間:2025/3/11 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android初始化本地数据库 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原文: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)

public class DBUtil { private static SQLiteDatabase database; public static final String DATABASE_FILENAME = "myb.db"; public static final String PACKAGE_NAME = "com.mobilexu.test"; public static final String DATABASE_PATH = "/data" + Environment.getDataDirectory().getAbsolutePath() + "/" + PACKAGE_NAME; public static SQLiteDatabase openDatabase(Context context) { try { String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME; File dir = new File(DATABASE_PATH); if (!dir.exists()) { dir.mkdir(); } if (!(new File(databaseFilename)).exists()) { InputStream is = context.getResources().openRawResource(R.raw.mydb); FileOutputStream fos = new FileOutputStream(databaseFilename); byte[] buffer = new byte[8192]; int count = 0; while ((count = is.read(buffer)) > 0) { fos.write(buffer, 0, count); } fos.close(); is.close(); } database = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null); return database; } catch (Exception e) { e.printStackTrace(); } return null; } }
  • 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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。