2015-04-19 144 views
1

我想在我的手機找我的數據庫(HTC M7)哪裏可以找到Sqlite數據庫?

其實,當我使用索尼移動,我可以找到它

但現在既然我改變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); 

     } 
} 
+0

爲什麼不是'Gsensor.db'在默認的'/數據/數據/ your.app.name/databses /'路徑? –

+0

@DerGolem此預設路徑,如果我想在此路徑中找到它,我的手機需要root,但我不想root。 –

+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(); 
     } 
    } 
} 
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) { 
       } 
      } 
     } 
    } 
相關問題