Android 拷贝raw文件夹下面的sqlite数据库文件到SDCard中 然后读取数据库并绑定

时间:2022-10-28 08:20:22
 添加权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

1
package com.tcrj.customer.basesqllite; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.database.sqlite.SQLiteDatabase; 6 import android.os.Environment; 7 8 import com.tcrj.customer.activity.R; 9 10 import java.io.File; 11 import java.io.FileOutputStream; 12 import java.io.InputStream; 13 import java.io.OutputStream; 14 15 16 /** 17 * SqlLite数据库 18 * Created by leict on 2016/1/25. 19 */ 20 public class SqliteDBHelper { 21 private static Context context = null; 22 private static SQLiteDatabase mSQLiteDatabase = null; 23 24 public SqliteDBHelper(Context context) { 25 this.context = context; 26 } 27 28 public static void getSqLiteData() { 29 try { 30 String DATABASE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/sqlitedb"; 31 String DATABASE_FILENAME = "provincialcitydb.db"; 32 String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME; 33 File dir = new File(DATABASE_PATH); 34 if (!dir.exists()) { 35 dir.mkdir(); 36 if (!(new File(databaseFilename)).exists()) { 37 InputStream is = context.getResources().openRawResource(R.raw.provincialcitydb); 38 OutputStream fos = new FileOutputStream(databaseFilename); 39 byte[] buffer = new byte[1024]; 40 int count = 0; 41 //开始复制ProvincialCityDB.db文件 42 while ((count = is.read(buffer)) > 0) { 43 fos.write(buffer, 0, count); 44 } 45 fos.close(); 46 is.close(); 47 } 48 //mSQLiteDatabase = context.openOrCreateDatabase(databaseFilename, Activity.MODE_PRIVATE, null); 49 } 50 } catch (Exception e) { 51 e.printStackTrace(); 52 } 53 // return mSQLiteDatabase; 54 } 55 56 public static String sqlitePath() { 57 String DATABASE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/sqlitedb"; 58 String DATABASE_FILENAME = "provincialcitydb.db"; 59 String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME; 60 return databaseFilename; 61 } 62 }

Select DataBase

  1  /**
  2      * 省
  3      */
  4     public void setProvinceData() {
  5         List<SpinnerEntity> dataList = new ArrayList<>();
  6         SqliteDBHelper helper = new SqliteDBHelper(context);
  7         SQLiteDatabase sqldata = SQLiteDatabase.openDatabase(helper.sqlitePath(), null, SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY);
  8         Cursor cur = sqldata.rawQuery("select * from Province", null);
  9         if (cur != null) {
 10             if (cur.moveToFirst()) {
 11                 do {
 12                     SpinnerEntity entity = new SpinnerEntity();
 13                     int idValue = cur.getInt(cur.getColumnIndex("id"));
 14                     String codeValue = cur.getString(cur.getColumnIndex("code"));
 15                     String nameValue = cur.getString(cur.getColumnIndex("name"));
 16                     entity.setId(idValue);
 17                     entity.setCode(codeValue);
 18                     entity.setSpinnerName(nameValue);
 19                     dataList.add(entity);
 20                 }
 21                 while (cur.moveToNext());
 22                 cur.close();
 23             }
 24         }
 25         sqldata.close();
 26         pAdapter = new SpinnerAdapter(context);
 27         pAdapter.setData(dataList);
 28         spinnerprovince.setAdapter(pAdapter);
 29         spinnerprovince.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
 30             @Override
 31             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
 32                 SpinnerEntity entity = (SpinnerEntity) pAdapter.getItem(position);
 33                 setCityData(entity.getCode());
 34             }
 35 
 36             @Override
 37             public void onNothingSelected(AdapterView<?> parent) {
 38 
 39             }
 40         });
 41     }
 42 
 43     /**
 44      * 市
 45      */
 46     public void setCityData(String code) {
 47         List<SpinnerEntity> dataList = new ArrayList<>();
 48         SqliteDBHelper helper = new SqliteDBHelper(context);
 49         SQLiteDatabase sqldata = SQLiteDatabase.openDatabase(helper.sqlitePath(), null, SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY);
 50         Cursor cur = sqldata.rawQuery("select * from City", null);
 51         if (cur != null) {
 52             if (cur.moveToFirst()) {
 53                 do {
 54                     SpinnerEntity entity = new SpinnerEntity();
 55                     int idValue = cur.getInt(cur.getColumnIndex("id"));
 56                     String codeValue = cur.getString(cur.getColumnIndex("code"));
 57                     String nameValue = cur.getString(cur.getColumnIndex("name"));
 58                     String parentId = cur.getString(cur.getColumnIndex("provinceId"));
 59                     if (code.equals(parentId)) {
 60                         entity.setId(idValue);
 61                         entity.setCode(codeValue);
 62                         entity.setParentId(parentId);
 63                         entity.setSpinnerName(nameValue);
 64                         dataList.add(entity);
 65                     }
 66                 }
 67                 while (cur.moveToNext());
 68                 cur.close();
 69             }
 70         }
 71         sqldata.close();
 72         cAdapter = new SpinnerAdapter(context);
 73         cAdapter.setData(dataList);
 74         spinnercity.setAdapter(cAdapter);
 75         spinnercity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
 76             @Override
 77             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
 78                 SpinnerEntity entity = (SpinnerEntity) cAdapter.getItem(position);
 79                 setAreaData(entity.getCode());
 80             }
 81 
 82             @Override
 83             public void onNothingSelected(AdapterView<?> parent) {
 84 
 85             }
 86         });
 87     }
 88 
 89     /**
 90      * 区
 91      */
 92     public void setAreaData(String code) {
 93         List<SpinnerEntity> dataList = new ArrayList<>();
 94         SqliteDBHelper helper = new SqliteDBHelper(context);
 95         SQLiteDatabase sqldata = SQLiteDatabase.openDatabase(helper.sqlitePath(), null, SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY);
 96         Cursor cur = sqldata.rawQuery("select * from Area", null);
 97         if (cur != null) {
 98             if (cur.moveToFirst()) {
 99                 do {
100                     SpinnerEntity entity = new SpinnerEntity();
101                     int idValue = cur.getInt(cur.getColumnIndex("id"));
102                     String codeValue = cur.getString(cur.getColumnIndex("code"));
103                     String nameValue = cur.getString(cur.getColumnIndex("name"));
104                     String cityId = cur.getString(cur.getColumnIndex("cityId"));
105                     if (code.equals(cityId)) {
106                         entity.setId(idValue);
107                         entity.setCode(codeValue);
108                         entity.setSpinnerName(nameValue);
109                         entity.setCityId(cityId);
110                         dataList.add(entity);
111                     }
112                 }
113                 while (cur.moveToNext());
114                 cur.close();
115             }
116         }
117         sqldata.close();
118         aAdapter = new SpinnerAdapter(context);
119         aAdapter.setData(dataList);
120         spinnerarea.setAdapter(aAdapter);
121         spinnerarea.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
122             @Override
123             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
124 
125             }
126 
127             @Override
128             public void onNothingSelected(AdapterView<?> parent) {
129 
130             }
131         });
132     }