androidstudio中外部数据库的导入

   由于工具的不同,在androidstudio中导入外部数据库也是有所不同,下面教大家怎么在as中导入db,注意往下看



        1.切换project视图   src->main->新建assets


       至于为什么,往下看:







             2.创建DBMain继承自

                         SQLiteOpenHelper

public class DBMain extends SQLiteOpenHelper {
    private static String DB_PATH= "data/data/com.safephone.lwp.importdatabase/databases/";
    private static String DB_NAME = "ii";
    private SQLiteDatabase dbObj;
    private final Context context;

    public DBMain(Context context) {
        super(context,  DB_NAME , null, 3);
        this. context  = context;
    }

    public void createDB() throws IOException {

        this.getReadableDatabase();


        try {
            copyDB();


        } catch (IOException e) {

            throw new Error("Error copying database");
        }
    }

    private boolean checkDB(){

        SQLiteDatabase checkDB = null;

        try{
            String path = DB_PATH + DB_NAME;

            checkDB = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);


            if (checkDB!=null)
            {
                Cursor c= checkDB.rawQuery("SELECT * FROM bank", null);
                Log.i("Cursor.......",c.getString(0));
                c.moveToFirst();
                String contents[]=new String[80];
                int flag=0;

                while(! c.isAfterLast())
                {
                    String temp="";
                    String s2=c.getString(0);
                    String s3=c.getString(1);
                    String s4=c.getString(2);
                    temp=temp+"\n Id:"+s2+"\tType:"+s3+"\tBal:"+s4;
                    contents[flag]=temp;
                    flag=flag+1;

                    Log.i("DB values.........",temp);
                    c.moveToNext();

                }
            }
            else
            {
                return false;
            }

        }catch(SQLiteException e){
            e.printStackTrace();
        }

        if(checkDB != null){

            checkDB.close();

        }
        return checkDB != null ? true : false;
    }

    public void copyDB() throws IOException{
        try {


            InputStream ip =  context.getAssets().open(DB_NAME+".db");
            Log.i("Input Stream....",ip+"");
            String op=  DB_PATH  +  DB_NAME ;
            OutputStream output = new FileOutputStream( op);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = ip.read(buffer))>0){
                output.write(buffer, 0, length);
                Log.i("Content.... ",length+"");
            }
            output.flush();
            output.close();
            ip.close();
        }
        catch (IOException e) {
            Log.v("error", e.toString());
        }
    }

    public void openDB() throws SQLException {

        String myPath = DB_PATH + DB_NAME;
        dbObj = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
        Log.i("open DB......",dbObj.toString());
    }

    @Override
    public synchronized void close() {

        if(dbObj != null)
            dbObj.close();

        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
3.Mainactivity的oncreate方法中
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    DBMain db;
    db = new DBMain(this);

    try {

        db.createDB();
    } catch (IOException ioe) {

        throw new Error("Database not created....");
    }

    try {
        db.openDB();

    }catch(SQLException sqle){

        throw sqle;
    }

    SQLiteDatabase db1;
    db1=openOrCreateDatabase("ii",SQLiteDatabase.CREATE_IF_NECESSARY,null);
    Cursor c= db1.rawQuery("SELECT * FROM bank",null);

    c.moveToFirst();

    String temp="";
    while(! c.isAfterLast())
    {
        String s2=c.getString(0);
        String s3=c.getString(1);
        String s4=c.getString(2);
        temp=temp+"\n Id:"+s2+"\tType:"+s3+"\tBal:"+s4;

        c.moveToNext();
    }

}


ok,大功告成,觉得可以请点赞
 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值