一份国内省份城市天气地点查询的数据库(WeatherCities.db)在android中的使用

一份国内省份城市天气地点查询的数据库(WeatherCities.db)在android中的使用,第1张

在天气或者位置相关应用开发时,常常会选择一个地点查询该地点的天气或者位置信息,当然可通过API提供的json来解析城市列表。当无API提供城市列表的情况,可通过下面的db数据库文件查询城市列表。
数据库文件放在应用的assets目录下,使用前需要将其拷贝到内部存储对应的应用目录下,然后通过SQLite查询。详细代码如下:

private static final String DB_NAME = "WeatherCitys.db";
    private static final String TABLE_NAME = "weathercities";


    private void readDBData() {
        SQLiteDatabase sqLiteDatabase = getFileDB();
        Cursor cursor = null;
        try {
            cursor = sqLiteDatabase.query(TABLE_NAME, null, null, null, null, null, null);
            Log.d(TAG, "cursor" + cursor.getCount());
            while (cursor.moveToNext()) {
                String provinceName = cursor.getString(cursor.getColumnIndex("province_name"));
                String provinceId = cursor.getString(cursor.getColumnIndex("province_id"));
                String cityName = cursor.getString(cursor.getColumnIndex("city_name"));
                String cityId = cursor.getString(cursor.getColumnIndex("city_id"));
                String areaName = cursor.getString(cursor.getColumnIndex("area_name"));
                String areaPinyin = cursor.getString(cursor.getColumnIndex("area_pinyin"));
                String areaPinyinShort = cursor.getString(cursor.getColumnIndex("area_pinyinshort"));
                String areaId = cursor.getString(cursor.getColumnIndex("area_id"));
                String weatherId = cursor.getString(cursor.getColumnIndex("weather_id"));
                Log.d(TAG, "provinceName:" + provinceName + " ,provinceId:" + provinceId + " ,cityName:" + cityName + " ,cityId:" + cityId
                        + " ,areaName:" + areaName + " ,areaPinyin:" + areaPinyin + " ,areaPinyinShort:" + areaPinyinShort + " ,areaId:" + areaId
                        + " ,weatherId:" + weatherId);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }


    public SQLiteDatabase getFileDB() {
        String dbPath =  getFilesDir() + "/" + DB_NAME;
        boolean fileExist = new File(dbPath).exists();
        if (!fileExist) {
            try {
                FileOutputStream out = new FileOutputStream(dbPath);
                InputStream in = getAssets().open(DB_NAME);
                byte[] buffer = new byte[1024];
                int readBytes = 0;
                while ((readBytes = in.read(buffer)) != -1)
                    out.write(buffer, 0, readBytes);
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return SQLiteDatabase.openOrCreateDatabase(dbPath, null);
    }

AndroidStudio可通过Database Navigator插件查看db文件,详见此博客:AndroidStudio查看数据库文件
看到的weathercites的数据表内容如下:

数据库文件下载地址:天气国内城市数据库WeatherCities.db

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/992691.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存