java—如何将mysql数据库中的数据以特定格式导出到.flr或.pl文件中

rt4zxlrg  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(345)

我正在尝试从mysql数据库获取数据到一个prolog可以读取的格式的文件中,这个格式类似于json格式,但是我不知道如何进行换行,而且当我上传到mysql数据库时,它会自动保存到文件中。
这段代码是从数据库中提取到一个文件中,但只是在一条直线上,而不是所需的特定格式

JSONArray jObj= new JSONArray();
        jObj.add("id->"+id  );      
        jObj.add("faculty->"+faculty );            
        jObj.add("department->"+department );       
        jObj.add("capacity-> "+capacity  );               
        jObj.add("type-> "+type );               
        obj.add(id +":classroom");              
        obj.add(jObj);

         try (FileWriter file = new FileWriter(textName)) {                       
           while(obj.iterator().hasNext())            
           file.write(obj.toJSONString());

我希望输出是

cmpe025: Classroom          
   [
       location-> cmpe_building,
       capacity-> 70 ,
       inDepartment-> cmpe_department,
       roomNumber-> cmpe025
   ].

cmpe026: Classroom 
   [
       location-> cmpe_building,
       capacity-> 70 ,
       inDepartment-> cmpe_department,
       roomNumber-> cmpe026
   ].

但是我得到的实际输出没有换行

{ "Classroom" ["location->"cmpe_building"capacity-> 70,"inDepartment->"cmpe_department, "roomNumber->" cmpe025], ["location->" cmpe_building,"capacity->"70 ,"inDepartment->" cmpe_department, "roomNumber->" cmpe026].
kjthegm6

kjthegm61#

这是一个作为答案发布的评论,因为这是一个很大的评论,并没有回答问题,它提出了一个更好的解决方案,而不是要求。
如果您希望prolog能够使用您输出的数据,那么将它放在prolog易于使用的方法中;创造事实,例如。
文件名: classroom.pl ```
classroom(cmpe025,cmpe_building,70,cmpe_department).
classroom(cmpe026,cmpe_building,70,cmpe_department).

请注意,对于prolog事实,您不包括值名称,prolog使用position来标识值。事实本身确实需要一个名字。 `classroom` 能够唯一地识别它们,而不是说 `person` 一组事实。
然后在prolog中,您可以使用consult/1加载文件,例如。

consult("C:/classroom.pl").

并在 predicate 中使用它。您必须像在源代码中一样在单独的文件中创建 predicate 。
文件名: `predicates.pl` ```
test :-
  classroom(Course,Building,Room,Department),
  write("Course: "),write(Course),nl,
  write("Building: "),write(Building),nl,
  write("Room: "),write(Room),nl,
  write("Department: "),write(Department).

线路

classroom(Course,Building,Room,Department)

从课堂事实中读出价值观。无需解析数据、重新格式化等。
使用swi-prolog

Welcome to SWI-Prolog (threaded, 64 bits, version 7.3.35)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit http://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

1 ?- consult("C:/predicates.pl").
true.

2 ?- consult("C:/classroom.pl").
true.

3 ?- test.
Course: cmpe025
Building: cmpe_building
Room: 70
Department: cmpe_department
true ;
Course: cmpe026
Building: cmpe_building
Room: 70
Department: cmpe_department
true.

虽然我不建议您也可以在文件中的一行创建事实,但请确保在一个事实的结尾之间有一个空格 . 下一个事实的开始 classroom ,
例如。 ). classroom( ```
classroom(cmpe025,cmpe_building,70,cmpe_department). classroom(cmpe026,cmpe_building,70,cmpe_department).

相关问题