android¿ª·¢Ö®Ê¹ÓÃSQLiteÊý¾Ý¿â(dbÎļþ)

來源:互聯網
上載者:User

android¿ª·¢ÖʹÓÃSQLiteÊý¾Ý¿â(dbÎļþ)

ÔÚ¿ª·¢ÖУ¬ÓÐʱÐèҪʹÓÃdbÎļþÊý¾Ý¿â£¬ËùÒÔ¾ÍÐèÒª½«Æäµ¼ÈëÏîÄ¿£¬ÔÙ½«ÆäʹÓóÌÐòдÈëµ½Ó¦ÓõÄdbÎļþÏÂʹÓá£

´úÂëºÜ¼òµ¥£¬¿ÉÒÔÄÃÀ´Ö±½ÓʹÓá£

ҪʹÓÃÐèÒªÁ½¸ö²½Ö裺

1.´´½¨rawÎļþ£¬µ¼ÈëdbÎļþ£¬ÈçÏ£º


2.´úÂë<†·Ÿ"http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PHByZSBjbGFzcz0="brush:java;">public class DBOpenHelper {private final int BUFFER_SIZE = 400000;public static final String DB_NAME = "idiom.db"; // ±£´æµÄÊý¾Ý¿âÎļþÃûpublic static final String PACKAGE_NAME = "cn.edu.bzu.happy";// Ó¦ÓõİüÃûpublic static final String DB_PATH = "/data"+ Environment.getDataDirectory().getAbsolutePath() +"/"+ PACKAGE_NAME+ "/databases"; // ÔÚÊÖ»úÀï´æ·ÅÊý¾Ý¿âµÄλÖÃ//sdcardµÄ·¾¶(ÔÚandroid 4.4Öв»ºÃʹ£¬Îļþ³É¹¦´´½¨ÊÇÔÚÊÖ»úµÄ)//public static final String DB_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()+"/idiom";private Context context;public DBOpenHelper(Context context) {this.context = context;}public SQLiteDatabase openDatabase() {try {File myDataPath = new File(DB_PATH);if (!myDataPath.exists()){myDataPath.mkdirs();// Èç¹ûûÓÐÕâ¸öĿ¼,Ôò´´½¨}String dbfile=myDataPath+"/"+DB_NAME;if (!(new File(dbfile).exists())) {// ÅжÏÊý¾Ý¿âÎļþÊÇ·ñ´æÔÚ£¬Èô²»´æÔÚÔòÖ´Ðе¼È룬·ñÔòÖ±½Ó´ò¿ªÊý¾Ý¿âInputStream is = context.getResources().openRawResource(R.raw.idiom); // Óûµ¼ÈëµÄÊý¾Ý¿âFileOutputStream fos = new FileOutputStream(dbfile);byte[] buffer = new byte[BUFFER_SIZE];int count = 0;while ((count = is.read(buffer)) > 0) {fos.write(buffer, 0, count);}fos.close();is.close();}SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,null);return db;} catch (FileNotFoundException e) {Log.e("Database", "File not found");e.printStackTrace();} catch (IOException e) {Log.e("Database", "IO exception");e.printStackTrace();}return null;}}
ʹÓãº

public class TestDao {private DBOpenHelper dbHelper;public TestDao (Context context) {dbHelper = new DBOpenHelper(context);}public List getAllTests() {List animals = new ArrayList();SQLiteDatabase sqLiteDatabase = dbHelper.openDatabase();Cursor cursor = sqLiteDatabase.rawQuery("select * from test", null);While(cursor.moveToNext()){//»ñÈ¡ÄãµÄÊý¾Ý}}}

TestDao  test = new TestDao();test.getAllTests();




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.