哪里可以找到Sqlite数据库?

时间:2015-04-19 15:50:42

标签: android sqlite android-sqlite

我想在我的手机(HTC M7)中查找我的数据库

实际上,当我使用sony mobile时,我可以找到它

但是现在因为我改变HTC M7而无法找到它

这是我的数据路径设置

哪里可以在HTC M7中找到它?

import static android.provider.BaseColumns._ID;
import java.io.File;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;

public class DBHelper extends SQLiteOpenHelper {
    private static Context context;   
       public static final String TABLE_NAME = "Gsensor";  //sqlite table name
       public static final String X = "X";
       public static final String Y = "Y";
       public static final String Z = "Z";
       private final static String DATABASE_NAME = "Gsensor.db";  //database name
       private final static int DATABASE_VERSION = 1;  //db ver
       private File path = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + 
                            File.separator); //db path
       String filepath = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                            File.separator + "Gsensor.db";
       private File f = new File(filepath);

       //DB table
       String CreateTable = "CREATE TABLE " + TABLE_NAME + " (" +_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                            X + " CHAR, " + Y + " CHAR, " + Z + " CHAR);";
       //del table
       String DelTable = "DROP TABLE IF EXISTS " + TABLE_NAME;
       public DBHelper(Context context) {
           super(context, DATABASE_NAME, null, DATABASE_VERSION);
           this.context = context;//openOrCreateDatabase is this method
                                  //so need to init context and return to
      }

       @Override
       //create table
       public void onCreate(SQLiteDatabase db) {
        SQLiteDatabase dbwrite= context.openOrCreateDatabase("f", context.MODE_WORLD_WRITEABLE, null);
        db.execSQL(CreateTable);
      }
       @Override
       //del table
       public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         db.execSQL(DelTable);
         onCreate(db);

      }
}

2 个答案:

答案 0 :(得分:0)

如果您没有root权限,则需要将数据库复制到公用文件夹。您可以像常规文件一样复制数据库,您可能会发现这些函数很有用。像这样使用

copyFileOrDirectory(getDatabasePath(),
Environment.getExternalStorageDirectory().getPath()
 + File.separator + "Gsensor" + File.separator + "Gsensor.db");

public static String getDatabasePath() {
    return (Environment.getDataDirectory() + File.separator + "Gsensor.db");
}

public static void copyFileOrDirectory(String srcDir, String dstDir) {

    try {
        File src = new File(srcDir);
        File dst = new File(dstDir, src.getName());

        if (src.isDirectory()) {

            String files[] = src.list();
            int filesLength = files.length;
            for (int i = 0; i < filesLength; i++) {
                String src1 = (new File(src, files[i]).getPath());
                String dst1 = dst.getPath();
                copyFileOrDirectory(src1, dst1);

            }
        } else {
            copyFile(src, dst);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.getParentFile().exists())
        destFile.getParentFile().mkdirs();

    if (!destFile.exists()) {
        destFile.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;

    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

答案 1 :(得分:0)

你可以试试这个:

public static void copyDatabase(Context c) {
        String databasePath = c.getDatabasePath("Gsensor.db").getPath();
        File f = new File(databasePath);
        OutputStream myOutput = null;
        InputStream myInput = null;
        Log.d("testing", " testing db path " + databasePath);
        Log.d("testing", " testing db exist " + f.exists());

        if (f.exists()) {
            try {

                File directory = new File("/mnt/sdcard/DB_DEBUG");
                if (!directory.exists())
                    directory.mkdir();

                myOutput = new FileOutputStream(directory.getAbsolutePath()
                        + "/" + "hoho.sqlite");
                myInput = new FileInputStream(databasePath);

                byte[] buffer = new byte[1024];
                int length;
                while ((length = myInput.read(buffer)) > 0) {
                    myOutput.write(buffer, 0, length);
                }

                myOutput.flush();
            } catch (Exception e) {
            } finally {
                try {
                    if (myOutput != null) {
                        myOutput.close();
                        myOutput = null;
                    }
                    if (myInput != null) {
                        myInput.close();
                        myInput = null;
                    }
                } catch (Exception e) {
                }
            }
        }
    }