Android Studio导入外部数据库(使用Sqlite expert pro创建外部数据库)

在各个博客网站查询了很多方法,虽然能够导入,但是表是空的,个人判断是外部数据库类型(如后缀为db,sql)的原因,改为sqlite类型的数据库就可以正常导入所有外部数据库的信息了,以下是导入的几个步骤:

方法一是使用虚拟机的目录的upload方法直接导入,具体步骤去查其他博主的吧,这里主要讲使用代码来写入外部的数据库:

在这里插入图片描述

方法二,代码导入:
1.创建外部数据库和数据表:

在这里插入图片描述
并把该数据库放到项目中的assets或者raw这两个目录中(自行创建的目录)
![在这里插入图片描述](https://img-blog.csdnimg.cn/944d682a2fec453fa1986aaa282abc36.png在这里插入图片描述在这里插入图片描述
在这里插入图片描述

2.创建IO流的封装类来写入数据库:

public class NationOpenDB {
    private final int BUFFER_SIZE = 400000;
    public static final String DB_BANE = "nations.sqlite";//保存的数据库文件名称
    public static final String PACKAGE_NAME = "com.example.test2";//应用的包名
    public static final String DB_PATH = "/data" + Environment.getDataDirectory().getAbsolutePath() + "/"
            + PACKAGE_NAME + "/databases";//在手机里存放数据库的位置
    private Context context;//组件的上下文

    public NationOpenDB(Context context) {
        this.context = context;
    }
    public SQLiteDatabase openDatabase(){
        try {
            File myDataPath = new File(DB_PATH);
            if (!myDataPath.exists()) {
                myDataPath.mkdirs();
            }
            String dbFile = myDataPath + "/" + DB_BANE;
            if (!(new File(dbFile).exists())) {
                InputStream is = context.getAssets().open(DB_BANE);
                //InputStream is = context.getResources().openRawResource(R.raw.nations);
                FileOutputStream fos = new FileOutputStream(dbFile);
                byte[] buffer = new byte[BUFFER_SIZE];
                int count = 0;
                while ((count = is.read(buffer)) !=-1) {
                    Log.e("test2", "写入"+String.valueOf(count));
                    fos.write(buffer, 0, count);
                }
                fos.flush();
                fos.close();
                is.close();
            }

            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
            return db;
        } catch (FileNotFoundException e) {
            Log.e("test2", "File not found!");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("test2", "IO exception");
            e.printStackTrace();
        }
        Log.e("test2", "没有??!");
        return null;
    }
}

注意这些成员变量:DB_NAME(使用sqlite文件类型的数据库),PACKAGE_NAME(应用包名)

3.创建存储各条数据的实体类和实现测试

public class Nations {
    public Integer id;
    public String nation;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNation() {
        return nation;
    }

    public void setNation(String nation) {
        this.nation = nation;
    }
}
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        NationOpenDB nation = new NationOpenDB(this);
        List<Nations> nationList = new ArrayList<Nations>();
        SQLiteDatabase sqLiteDatabase = nation.openDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery("select *from china_nations", null);
        //Cursor cursor = sqLiteDatabase.query("china_nations", new String[]{"id", "nation"}, null, null, null, null, null);
        Log.e("test2", "cursor:" + String.valueOf(cursor.getCount()));
        while (cursor.moveToNext()) {
            Nations nations = new Nations();
            Integer id = cursor.getInt(cursor.getColumnIndex("id"));
            String nationName = cursor.getString(cursor.getColumnIndex("nation"));
            nations.setId(id);
            nations.setNation(nationName);
            nationList.add(nations);
        }
        for (Nations nations1 : nationList) {
            Log.e("test2",nations1.id + " " + nations1.nation);
        }
    }

4.Log结果
在这里插入图片描述

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值