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();