2016-11-20 67 views
1

這是我的序言數據庫代碼。將查詢結果寫入序言中的文件

:- 
    dynamic myTable/2. 

init :- 
    removeAll, 
    asserta(myTable('avalue', 'another value')), 
    asserta(myTable('avalue1', 'another value 1')), 
    asserta(myTable('avalue2', 'another value 2')), 
    asserta(myTable('avalue3', 'another value 3')), 
    asserta(myTable('avalue4', 'another value 4')). 

read(Col1, Col2) :- 
    myTable(Col1, Col2). 

saveQueries(FileName) :- 
    tell(FileName). 

stopSavingQueries :- 
    told. 

我想開始將prolog輸出保存到文件中。對動態數據庫進行一些查詢,將其保存到文件中,然後停止保存查詢。它看起來像這樣

?- init. 
true. 

?- saveQueries('queries.txt'). 
true. 

?- read(Col1, Col2). 
... 
?- stopSavingQueries. 
true. 

當我運行此代碼文件queries.txt創建。當我運行read(Col1, Col2).時,我在控制檯中看到輸出,並且文件queries.txt保持爲空。

回答

1

谷歌搜索一段時間後,我找到了這個解決方案。

saveQueries(FileName) :- 
    protocol(FileName). 

stopQueriesSaving :- 
    noprotocol. 

然後當我執行我有一個文件queries.txt它包含所有的查詢和結果的命令,我可以做到這一點

?- saveQueries('queries.txt'). 
true. 

/* execute some queries here */ 

?- stopQueriesSaving. 
true.