Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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 将文件室数据库导出为json文件_Android_Sqlite_Android Sqlite_Android Room - Fatal编程技术网

Android 将文件室数据库导出为json文件

Android 将文件室数据库导出为json文件,android,sqlite,android-sqlite,android-room,Android,Sqlite,Android Sqlite,Android Room,是否可以将包含所有表、条目的所有文件室数据库导出到json文件?我看到了一些使用SQLite的方法,但是房间数据库呢?您可以根据需要将房间实体对象传递给JSON生成器。若您希望检索一组实体并将它们写入JSON,您可以这样做 换句话说,您可以使用JSON生成器将Dog和Cat对象中的数据写入JSON。狗和猫对象来自何处-房间、改装、领域等-并不重要 实现导出的范围(“包含所有表、条目的所有文件室数据库”)取决于您。您可以根据需要将文件室实体对象传递给JSON生成器。若您希望检索一组实体并将它们写入

是否可以将包含所有表、条目的所有文件室数据库导出到json文件?我看到了一些使用SQLite的方法,但是房间数据库呢?

您可以根据需要将房间实体对象传递给JSON生成器。若您希望检索一组实体并将它们写入JSON,您可以这样做

换句话说,您可以使用JSON生成器将
Dog
Cat
对象中的数据写入JSON。
对象来自何处-房间、改装、领域等-并不重要


实现导出的范围(“包含所有表、条目的所有文件室数据库”)取决于您。

您可以根据需要将文件室实体对象传递给JSON生成器。若您希望检索一组实体并将它们写入JSON,您可以这样做

换句话说,您可以使用JSON生成器将
Dog
Cat
对象中的数据写入JSON。
对象来自何处-房间、改装、领域等-并不重要


实现导出的范围(“包含所有表、条目的所有文件室数据库”)取决于您。

如果您了解如何将文件室数据库中的表导出到JSON,这将非常有用。让我们看看怎么做

步骤1:为房间数据库创建模型

步骤2:为房间数据库创建数据访问对象(DAO)

步骤4:从Room数据库导出学生表作为JSON

//在活动中创建引用变量
私人列表标准列表;
私立学生数据库stdDatabase;
私有void exportJSON(){
Completable.fromAction(新操作(){
@凌驾
public void run()引发异常{
//stdDatabase.stdDAO().getStudents();
}
}).observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.订阅(新的CompletableObserver(){
@凌驾
认购的公共无效(一次性d){
}
@凌驾
未完成的公共空间(){
stdList=stdDatabase.stdDAO().getStudents();
Gson Gson=新的Gson();
Type Type=new-TypeToken(){}.getType();
字符串stdJson=gson.toJson(stdList,type);
//调用函数将文件写入外部目录
writeToStorage(getApplicationContext(),“StudnetJson”,stdJson)
}
@凌驾
公共无效申报人(可丢弃的e){
//显示错误消息
}
});
}
//在活动中创建一个方法
public void writeToStorage(上下文mContext、字符串文件名、字符串jsonContent){
File File=新文件(mContext.getFilesDir(),“exportStudentJson”);
如果(!file.exists()){
mkdir()文件;
}
试一试{
File mFile=新文件(文件,文件名);
FileWriter writer=新的FileWriter(mFile);
writer.append(jsonContent);
writer.flush();
writer.close();
}捕获(例外e){
e、 printStackTrace();
}
}
即使你不明白,请让我写评论


注意:将来我将为此问题创建一个GitHub项目。谢谢

如果您了解如何将表从Room数据库导出到JSON,这将非常有用。让我们看看怎么做

步骤1:为房间数据库创建模型

步骤2:为房间数据库创建数据访问对象(DAO)

步骤4:从Room数据库导出学生表作为JSON

//在活动中创建引用变量
私人列表标准列表;
私立学生数据库stdDatabase;
私有void exportJSON(){
Completable.fromAction(新操作(){
@凌驾
public void run()引发异常{
//stdDatabase.stdDAO().getStudents();
}
}).observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.订阅(新的CompletableObserver(){
@凌驾
认购的公共无效(一次性d){
}
@凌驾
未完成的公共空间(){
stdList=stdDatabase.stdDAO().getStudents();
Gson Gson=新的Gson();
Type Type=new-TypeToken(){}.getType();
字符串stdJson=gson.toJson(stdList,type);
//调用函数将文件写入外部目录
writeToStorage(getApplicationContext(),“StudnetJson”,stdJson)
}
@凌驾
公共无效申报人(可丢弃的e){
//显示错误消息
}
});
}
//在活动中创建一个方法
public void writeToStorage(上下文mContext、字符串文件名、字符串jsonContent){
File File=新文件(mContext.getFilesDir(),“exportStudentJson”);
如果(!file.exists()){
mkdir()文件;
}
试一试{
File mFile=新文件(文件,文件名);
FileWriter writer=新的FileWriter(mFile);
writer.append(jsonContent);
writer.flush();
writer.close();
}捕获(例外e){
e、 printStackTrace();
}
}
即使你不明白,请让我写评论


注意:将来我将为此问题创建一个GitHub项目。谢谢

那么多对多数据库加外键和连接表怎么样?@kosas:在
Dog
Cat
POJOs级别,在JSON级别,没有外键和连接表之类的东西。如何选择在JSON中对这些内容进行建模取决于您,Room在这种建模方法中并没有涉及太多内容。感谢您的帮助!使用外键和joint t的多对多数据库怎么样
    @Entity(tableName = "students")
    public class Student{
        @PrimaryKey(autoGenerate = true)
        private int id;
        @ColumnInfo(name = "std_name")
        private String name;
        public Student(int id, String name){
            this.id = id;
            this.name = name;
        }
        @Ignore
        public Student(String name){
            this.name = name;
        }
        public int getId() {
            return id;
        }
        public String getName() {
            return name;
        }
     }
@Dao
public interface StudentDAO {
    @Query("select * from students")
    List<Student> getStudents();

    @Insert
    public void insert(Student student);

    // Other CRUD methods
    // ..........
}
@Database(entities = { Student.class }, version = 1, exportSchema = false)
public abstract class StudentDatabase extends RoomDatabase {
    private static final String DB_NAME ="StudentDb";
    private static StudentDatabase stdInstance;
    public abstract StudentDAO stdDAO();    

    public synchronized static StudentDatabase getInstance(final Context context) {
        if (stdInstance == null) {
            stdInstance = Room.databaseBuilder(context, StudentDatabase.class, DB_NAME)
                                        .allowMainThreadQueries().build();
        }
        return stdInstance;
    }
}
// Create reference variables in your Activity
private List<Student> stdList;
private StudentDatabase stdDatabase;

private void exportJSON(){
    Completable.fromAction(new Action() {
        @Override
        public void run() throws Exception {
           // stdDatabase.stdDAO().getStudents();
        }
    }).observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(new CompletableObserver() {
                @Override
                public void onSubscribe(Disposable d) {
                }

                @Override
                public void onComplete() {
                    stdList = stdDatabase.stdDAO().getStudents();
                    Gson gson = new Gson();
                    Type type = new TypeToken<List<Student>>(){}.getType();
                    String stdJson = gson.toJson(stdList, type);

                    // Call function to write file to external directory
                    writeToStorage(getApplicationContext(), "StudnetJson", stdJson)
                }

                @Override
                public void onError(Throwable e) {
                    // Show error message
                }
            });
    }

//Create a Method in your Activity
public void writeToStorage(Context mContext, String fileName, String jsonContent){      
    File file = new File(mContext.getFilesDir(),"exportStudentJson");
    if(!file.exists()){
        file.mkdir();
    }
    try{
        File mFile = new File(file, fileName);
        FileWriter writer = new FileWriter(mFile);
        writer.append(jsonContent);
        writer.flush();
        writer.close();

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

    }
}