如何在Android设备上使用.db文件? [英] How to use .db file on an android device?

查看:142
本文介绍了如何在Android设备上使用.db文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在模拟器数据库文件所在的/data/data/com.example.app_name/databases/data.db内。但在实际的设​​备不存在此目录。由于该应用程序崩溃。应用程序在模拟器运行平稳。

In emulator db file is located inside the /data/data/com.example.app_name/databases/data.db. But in real device no such directory exists. Due to which application crashes. Application is running smoothly in emulator.

在实际设备的Andr​​oid / data文件夹存在,但是它不包含我命名的文件夹的应用程序。

In real device android/data folder exists but it does not contain my application named folder.

此外,当我通过敬酒显示的目录应用程序通过this.getDatabasePath()在设备里面显示//data/data/com.example.My_App但只要数据库活动启动应用程序崩溃。而com.example.My_App文件夹不存在于Android的/数据。

Also, when I displayed the directory for application through this.getDatabasePath() inside the device via toast it displays //data/data/com.example.My_App but as soon as database activity starts application crashes. And the com.example.My_App folder does not exists in android/data.

我曾尝试创建内部的Andr​​oid /数据数据文件夹,并提出了在它的所有数据库文件,但仍然没有奏效。也试过上,其中android文件夹,存在,但什么都没有发生手机内存创建。
请帮助我。
答案将AP preciated。

I have tried creating data folder inside android/data and put up all the database files in it but still did not worked. Also tried on creating it at phone memory where android folder exists but nothing happened. Please help me out. Answers will be appreciated.

推荐答案

有关解决这个问题,您可以创建在移动SD卡数据库。它工作的顺利开展,也被用户访问。如果安全是担心再使用RES /原始文件夹,以便用户没有访问它。
对于SD卡上创建数据库,您可以使用下面code(这是样品code)...

For overcoming this problem You can create Your database in SD card of mobile. It work smoothly and also accessed by user. If security is concerned then Use res/raw folder so that User did not access it. for Creating database on SD card you can use following Code(It is Sample Code)...

public static void saveExpanseForm(Context con,NewExpenseForm newexpanse) throws Exception {        
    SQLiteDatabase sampleDB = null;

    try {

        File sdcard;
        if (android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED)){
            sdcard = new File(Environment.getExternalStorageDirectory(), "ROIApp");
            sdcard.mkdirs();
        } else {
            /* save the folder in internal memory of phone */
            sdcard = new File(Environment.getRootDirectory(),"ROIApp");
            sdcard.mkdirs();

        }

        String dbfile = sdcard.getAbsolutePath() + File.separator+ "RoiAppDB" + File.separator + NETNOTENABLE_DB_NAME;
        sampleDB = con.openOrCreateDatabase(dbfile, MODE_PRIVATE,null);

        sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " + Expense_TABLE_NAME +
                " (id INTEGER PRIMARY KEY AUTOINCREMENT, Currentdate varchar, dateFormat varchar, Name varchar, Category varchar,"+
                " Location varchar,Cost varchar,Sex INTEGER, Tags varchar, Notes varchar, In1 varchar, In2 varchar, " +
                "Remindme INTEGER, ImagePath varchar);");

        sampleDB.execSQL("INSERT INTO " + Expense_TABLE_NAME +
                "(Currentdate, dateFormat, Name, Category, Location,Cost, Sex, Tags, Notes, In1, In2, Remindme, ImagePath ) Values ('"
                +newexpanse.getDates()+"','"+newexpanse.getDateFormat()+"','"+newexpanse.getName()+"','"+
                newexpanse.getCategory()+"','"+newexpanse.getLocation()+"','"+newexpanse.getCost()+"','"+newexpanse.getSex()+"','"+newexpanse.getTag()+"','"
                +newexpanse.getNotes()+"','"+newexpanse.getInstr1()+"','"+newexpanse.getInstr2()+"','"
                +newexpanse.getRemindme()+"','"+newexpanse.getImageName()+"');");

        Log.e("ExpanseTable", " ExpanseInsert "+"INSERT INTO " + Expense_TABLE_NAME +
                "(Currentdate, dateFormat, Name, Category, Location,Cost, Sex, Tags, Notes, In1, In2, Remindme,ImagePath ) Values ('"
                +newexpanse.getDates()+"','"+newexpanse.getDateFormat()+"','"+newexpanse.getName()+"','"+
                newexpanse.getCategory()+"','"+newexpanse.getLocation()+"','"+newexpanse.getCost()+"','"+newexpanse.getSex()+"','"+newexpanse.getTag()+"','"
                +newexpanse.getNotes()+"','"+newexpanse.getInstr1()+"','"+newexpanse.getInstr2()+"','"
                +newexpanse.getRemindme()+"','"+newexpanse.getImageName()+"');");

        insertNewExpanseForm(sampleDB);

    } catch (SQLiteException e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    finally {
        if (sampleDB != null)
            sampleDB.close();
    }
}

这篇关于如何在Android设备上使用.db文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆