Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何通过SDK显示手机SQLite数据库?_Android_Database - Fatal编程技术网

Android 如何通过SDK显示手机SQLite数据库?

Android 如何通过SDK显示手机SQLite数据库?,android,database,Android,Database,我目前正在进行一些调试,我想知道是否可以通过SDK显示phones SQLite数据库的内容?我知道这是可能的,通过电话端查询。但我只是好奇你能通过SDK来做吗? 将数据库导出到sdcard文件中,每次您必须复制到计算机上,并通过某些SQLite管理器工具打开数据库时,我都会使用Firefox的插件来实现这一点。这里很简单,我不必一次又一次地重新打开数据库,只要点击刷新按钮,表就会得到更新 当设备处于usb模式时,您可以使用Eclipse的文件管理器从SD卡获取文件。只有当您无法将设备放入Ec

我目前正在进行一些调试,我想知道是否可以通过SDK显示phones SQLite数据库的内容?我知道这是可能的,通过电话端查询。但我只是好奇你能通过SDK来做吗?

  • 将数据库导出到sdcard文件中,每次您必须复制到计算机上,并通过某些SQLite管理器工具打开数据库时,我都会使用Firefox的插件来实现这一点。这里很简单,我不必一次又一次地重新打开数据库,只要点击刷新按钮,表就会得到更新
当设备处于usb模式时,您可以使用Eclipse的文件管理器从SD卡获取文件。只有当您无法将设备放入Eclipse并同时挂载SD卡时,才可以使用此选项。您必须使用Eclipse

下面是将数据库导出到SD卡的代码

/*
     * Task to backup the database to the SDCard
     */
    public static class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
        private Context ctx;

        /**
         *
         */
        public ExportDatabaseFileTask(Context ctx) {
            super();
            this.ctx=ctx;
        }

        // automatically done on worker thread (separate from UI thread)
        protected Boolean doInBackground(final String... args) {

           File dbFile =
                    new File(Environment.getDataDirectory() + "/data/[com.your.pkg]/databases/[pkg]");

           File exportDir = new File(Environment.getExternalStorageDirectory(), "");
           if (!exportDir.exists()) {
              exportDir.mkdirs();
           }
           File file = new File(exportDir, dbFile.getName());

           try {
              file.createNewFile();
              this.copyFile(dbFile, file);
              return true;
           } catch (IOException e) {
              Log.e("birthdroid", e.getMessage(), e);
              return false;
           }
        }

        // can use UI thread here
        protected void onPostExecute(final Boolean success) {
           if (success) {
              Toast.makeText(ctx, "Export successful!", Toast.LENGTH_SHORT).show();
           } else {
              Toast.makeText(ctx, "Export failed", Toast.LENGTH_SHORT).show();
           }
        }

        void copyFile(File src, File dst) throws IOException {
           FileChannel inChannel = new FileInputStream(src).getChannel();
           FileChannel outChannel = new FileOutputStream(dst).getChannel();
           try {
              inChannel.transferTo(0, inChannel.size(), outChannel);
           } finally {
              if (inChannel != null)
                 inChannel.close();
              if (outChannel != null)
                 outChannel.close();
           }
        }

     }
/*
*将数据库备份到SD卡的任务
*/
公共静态类ExportDatabaseFileTask扩展了AsyncTask{
私有上下文ctx;
/**
*
*/
公共ExportDatabaseFileTask(上下文ctx){
超级();
这个.ctx=ctx;
}
//在工作线程上自动完成(与UI线程分开)
受保护的布尔doInBackground(最终字符串…args){
文件dbFile=
新文件(Environment.getDataDirectory()+“/data/[com.your.pkg]/databases/[pkg]”;
File exportDir=新文件(Environment.getExternalStorageDirectory(),“”);
如果(!exportDir.exists()){
exportDir.mkdirs();
}
File File=新文件(exportDir,dbFile.getName());
试一试{
createNewFile();
这个.copyFile(dbFile,file);
返回true;
}捕获(IOE异常){
Log.e(“birthdroid”,e.getMessage(),e);
返回false;
}
}
//可以在这里使用UI线程
受保护的void onPostExecute(最终布尔值成功){
如果(成功){
Toast.makeText(ctx,“导出成功!”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(ctx,“导出失败”,Toast.LENGTH_SHORT).show();
}
}
void copyFile(文件src、文件dst)引发IOException{
FileChannel inChannel=newfileinputstream(src).getChannel();
FileChannel outChannel=新的FileOutputStream(dst).getChannel();
试一试{
inChannel.transferTo(0,inChannel.size(),outChannel);
}最后{
如果(inChannel!=null)
inChannel.close();
如果(输出通道!=null)
outChannel.close();
}
}
}
  • 在光标上,您始终可以调用:

    数据库utils.dumpCursorToString(cur)

要获取光标的原始字符串表示形式

  • 将数据库导出到sdcard文件中,每次您必须复制到计算机上,并通过某些SQLite管理器工具打开数据库时,我都会使用Firefox的插件来实现这一点。这里很简单,我不必一次又一次地重新打开数据库,只要点击刷新按钮,表就会得到更新
当设备处于usb模式时,您可以使用Eclipse的文件管理器从SD卡获取文件。只有当您无法将设备放入Eclipse并同时挂载SD卡时,才可以使用此选项。您必须使用Eclipse

下面是将数据库导出到SD卡的代码

/*
     * Task to backup the database to the SDCard
     */
    public static class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
        private Context ctx;

        /**
         *
         */
        public ExportDatabaseFileTask(Context ctx) {
            super();
            this.ctx=ctx;
        }

        // automatically done on worker thread (separate from UI thread)
        protected Boolean doInBackground(final String... args) {

           File dbFile =
                    new File(Environment.getDataDirectory() + "/data/[com.your.pkg]/databases/[pkg]");

           File exportDir = new File(Environment.getExternalStorageDirectory(), "");
           if (!exportDir.exists()) {
              exportDir.mkdirs();
           }
           File file = new File(exportDir, dbFile.getName());

           try {
              file.createNewFile();
              this.copyFile(dbFile, file);
              return true;
           } catch (IOException e) {
              Log.e("birthdroid", e.getMessage(), e);
              return false;
           }
        }

        // can use UI thread here
        protected void onPostExecute(final Boolean success) {
           if (success) {
              Toast.makeText(ctx, "Export successful!", Toast.LENGTH_SHORT).show();
           } else {
              Toast.makeText(ctx, "Export failed", Toast.LENGTH_SHORT).show();
           }
        }

        void copyFile(File src, File dst) throws IOException {
           FileChannel inChannel = new FileInputStream(src).getChannel();
           FileChannel outChannel = new FileOutputStream(dst).getChannel();
           try {
              inChannel.transferTo(0, inChannel.size(), outChannel);
           } finally {
              if (inChannel != null)
                 inChannel.close();
              if (outChannel != null)
                 outChannel.close();
           }
        }

     }
/*
*将数据库备份到SD卡的任务
*/
公共静态类ExportDatabaseFileTask扩展了AsyncTask{
私有上下文ctx;
/**
*
*/
公共ExportDatabaseFileTask(上下文ctx){
超级();
这个.ctx=ctx;
}
//在工作线程上自动完成(与UI线程分开)
受保护的布尔doInBackground(最终字符串…args){
文件dbFile=
新文件(Environment.getDataDirectory()+“/data/[com.your.pkg]/databases/[pkg]”;
File exportDir=新文件(Environment.getExternalStorageDirectory(),“”);
如果(!exportDir.exists()){
exportDir.mkdirs();
}
File File=新文件(exportDir,dbFile.getName());
试一试{
createNewFile();
这个.copyFile(dbFile,file);
返回true;
}捕获(IOE异常){
Log.e(“birthdroid”,e.getMessage(),e);
返回false;
}
}
//可以在这里使用UI线程
受保护的void onPostExecute(最终布尔值成功){
如果(成功){
Toast.makeText(ctx,“导出成功!”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(ctx,“导出失败”,Toast.LENGTH_SHORT).show();
}
}
void copyFile(文件src、文件dst)引发IOException{
FileChannel inChannel=newfileinputstream(src).getChannel();
FileChannel outChannel=新的FileOutputStream(dst).getChannel();
试一试{
inChannel.transferTo(0,inChannel.size(),outChannel);
}最后{
如果(inChannel!=null)
inChannel.close();
如果(输出通道!=null)
outChannel.close();
}
}
}
  • 在光标上,您始终可以调用:

    数据库utils.dumpCursorToString(cur)


要获取光标的原始字符串表示形式

您想自己查看数据库的内容还是想查看并使用应用程序中的数据?我想没有人清楚您想要什么。您是想通过eclipse查看内容,还是想查看dbI wan中的一些值,而不是在计算机屏幕(而不是手机)上列出eclipse中的行及其包含的内容