android 匯入外部資料庫

來源:互聯網
上載者:User

常用的資料庫操作中,大部分都是在應用內部建立資料庫,有時候可能需要訪問已經建立完成的資料庫,這時就需要匯入外部的資料庫。

建立資料庫實現方法:

1. extends  SQLiteOpenHelper

2. SQLiteDatabase.openOrCreateDatabase

 

匯入外部資料庫實現

1.網上廣為流傳的將 db放在 res目錄下面,然後引用ID擷取輸入資料流,將資料庫寫入SD卡或手機記憶體。這樣匯入的資料庫大小可能出現和原來大小不一致的問題,因為res目錄下所有資源打包為APK時系統會進行壓縮。

2. 將db放在assets目錄下面,通過AssetManager擷取輸出資料流,將db寫入。由於assets目錄下面有些格式的檔案大小不能超過2M,所以將資料庫改為 *.mp3。在寫入SD卡或手機記憶體時,在將尾碼名改為db.由於assets目錄下面的檔案不會被壓縮,所以不會出現匯入的資料庫大小不一致情況。

 

有人嘗試將資料庫寫入data/data/應用/目錄下面,這樣在模擬器下測試木有問題,可以查看到建立成功的資料庫。但在手機上,這個目錄不能進行任何操作,提示為檔案唯讀,也就是木有許可權修改或建立檔案。

木有測試過root後的手機是否可以寫入。

 

實現:

public class DBManager
{
private final int BUFFER_SIZE = 1024;
private SQLiteDatabase database;
private Context context;


public DBManager(Context context)
{
this.context = context;
}
 
public SQLiteDatabase openDatabase()
{
        File sdFile = Environment.getExternalStorageDirectory();
        File gpsPath = new File(sdFile.getPath()+"/pms/gps.db");
       
        if (!gpsPath.exists())
        {
            try
{
            //建立目錄www.2cto.com
            File pmsPaht = new File(sdFile.getPath()+"/pms");
            Log.i("pmsPaht", "pmsPaht: "+pmsPaht.getPath());
            pmsPaht.mkdirs();
           
            AssetManager am = this.context.getAssets();
InputStream is= am.open("gps.mp3");

FileOutputStream fos = new FileOutputStream(gpsPath);
 
byte[] buffer = new byte[BUFFER_SIZE];
           int count = 0;
           while ((count = is.read(buffer)) > 0)
           {
                   fos.write(buffer, 0, count);
           }
fos.flush();

fos.close();
is.close();
am.close();
}
catch (IOException e)

e.printStackTrace();
}
        }

        database = SQLiteDatabase.openOrCreateDatabase(gpsPath, null);

        
        return database;
    }

 
public void close()
{
if (database != null)
{
this.database.close();
}
}
    

}

測試 :

public class TestWriteDBActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button create = (Button) findViewById(R.id.create);
        create.setOnClickListener(new View.OnClickListener()
        {
        @Override
        public void onClick(View v)
        {
        createDB();
        
        }
        });
        Button query = (Button) findViewById(R.id.query);
        query.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
query();
}
});
       
    }
   
    public void createDB()
    {
    DBManager dbm = new DBManager(this);
    dbm.openDatabase();
    dbm.close();
    }
   
    public void query()
    {
    String sql = "select * from gps limit 5";
   
    DBManager dbm = new DBManager(this);
    SQLiteDatabase db = dbm.openDatabase();
    Cursor cur =  db.rawQuery(sql, null);
   
    while (cur.moveToNext())
    {
    float latitude = cur.getFloat(1);
    Log.i("latitude", "維度:"+latitude);
    }
    cur.close();
    db.close();
    }
   
   
   
}


摘自 qq1761310972

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.