2016-08-30 168 views
0

我希望能夠重新創建索引到一個數據庫的一部分表。VFP。重新創建索引

該表的索引已損壞,因此表無法打開(當然不是沒有錯誤消息)。我想使用數據庫容器中的信息來重建索引(帶有幾個標記)

也許我可以爲用戶提供一個工具來瀏覽數據庫中的表的列表,然後選擇一個表(或可能全部)被重新編入索引或打包。看過DBGETPROP()和CURSORGETPROP()函數調用,但沒有找到給我所需信息的選項。

一如既往,感謝您的指導。


謝謝添馬艦。是的,我一直在打開.dbc,但後來想要標籤名稱和索引表達式。

我想要做的是構建一個遊標,其中包含數據庫中每個表的標記名稱和索引表達式的詳細信息。

就我所見,我需要從.dbc中獲取表的名稱,然後打開每個表來查找其索引。我認爲這些只在表格本身不在.dbc中。可以看到SYS(14)函數會給我索引表達式,而TAG()會給我標籤名稱;這是要走的路,還是有一些功能已經可以爲我做這些事情?

回答

2

數據庫容器是一個表,所以你可以用USE打開它來讀取那裏的數據。

+0

謝謝。我通過編輯這個問題作出了迴應,以尋求進一步的澄清。 –

0

你不應該依賴dbc來查找和做任何事情。它無論如何都沒有索引信息(除了一些像PK,字段被索引等等,這不是一個值)。有時,專門打開數據庫(並專門訪問其表),然後執行:

validate database recover 

幫助但您不能依賴它。要正確地重新創建索引,您必須全部刪除它們(即刪除CDX文件),然後重新創建。

如果有幫助,下面是我在「修復」中編寫並在需要的情況下使用的實用程序代碼。它爲Dbc和自由表創建了一種數據字典:

* CreateDictionary.prg 
* Author: Cetin Basoz 
* CreateDictionary('c:\mypath\v210\data','','zipcodes,states') 
* Creates DataDictionary files in 'DataDic' directory (Default if not specified) 
* using 'c:\mypath\v210\data' dir as source data dir 
* adds zipcodes and states.dbf as static files (with data as is) 
* tcUserStatic - tables that should be kept on user as is 
Lparameters tcDataDir, tcDicDir, tcStaticList, tcUserStaticList 
tcDataDir = iif(type('tcDataDir')='C' and directory(addbs(m.tcDataDir)), addbs(m.tcDataDir), sys(5)+curdir()) 
tcDicDir = iif(type('tcDicDir')='C' and !empty(m.tcDicDir), addbs(m.tcDicDir), 'DataDic\') 
tcStaticList = iif(Type('tcStaticList')='C',trim(m.tcStaticList),'') 
If !directory(justpath(m.tcDicDir)) 
    Md (justpath(m.tcDicDir)) 
Endif 
Close data all 
lnDatabases = adir(arrDBC,m.tcDataDir+'*.dbc') 
Create table (m.tcDicDir+'DBCreator') (DBCName M nocptrans, FileBin M nocptrans, Filename c(128) nocptrans) 
for ix = 1 to m.lnDatabases 
    open data (m.tcDataDir+arrDBC[m.ix,1]) 
    do home()+'Tools\Gendbc\gendbc' with forceext(m.tcDicDir+arrDBC[m.ix,1],'PRG') 
    compile (forceext(m.tcDicDir+arrDBC[m.ix,1],'PRG')) 
    insert into (m.tcDicDir+'DBCreator') ; 
     values (arrDBC[m.ix,1], ; 
     FileToStr(forceext(m.tcDicDir+arrDBC[m.ix,1],'FXP')), ; 
     forceext(arrDBC[m.ix,1],'FXP')) 
    erase (forceext(m.tcDicDir+arrDBC[m.ix,1],'PRG')) 
    erase (forceext(m.tcDicDir+arrDBC[m.ix,1],'FXP')) 
    if file(forceext(m.tcDicDir+arrDBC[m.ix,1],'KRT')) 
     insert into (m.tcDicDir+'DBCreator') ; 
      values (arrDBC[m.ix,1], ; 
      FileToStr(forceext(m.tcDicDir+arrDBC[m.ix,1],'KRT')),; 
      forceext(arrDBC[m.ix,1],'KRT')) 
     erase (forceext(m.tcDicDir+arrDBC[m.ix,1],'KRT')) 
    endif 
endfor 
Close data all 

Create cursor crsSTRUCTS ; 
    (FIELD_NAME C(128) nocptrans, ; 
    FIELD_TYPE C(1), ; 
    FIELD_LEN N(3, 0), ; 
    FIELD_DEC N(3, 0), ; 
    FIELD_NULL L, ; 
    FIELD_NOCP L, ; 
    _TABLENAME M nocptrans) 
Create cursor crsINDEXES ; 
    (TAG_NAME C(10) nocptrans, ; 
    KEY_EXPR M, ; 
    NDXTYPE C(1), ; 
    IS_DESC L, ; 
    FILTEREXPR M nocptrans, ; 
    _TABLENAME M nocptrans) 
Select 0 
lnTables = adir(arrTables,m.tcDataDir+'*.dbf') 
For ix=1 to m.lnTables 
    Use (m.tcDataDir+arrTables[m.ix,1]) 
    if empty(cursorgetprop('Database')) 
     lnFields=afields(arrStruc) 
     For jx=1 to m.lnFields 
      arrStruc[m.jx,7]=arrTables[m.ix,1] 
     Endfor 
     Insert into crsSTRUCTS from array arrStruc 
     Release arrStruc 
     If tagcount()>0 
      Dimension arrIndexes[tagcount(),6] 
      For jx=1 to tagcount() 
       arrIndexes[m.jx,1] = tag(m.jx) 
       arrIndexes[m.jx,2] = key(m.jx) 
       arrIndexes[m.jx,3] = iif(Primary(m.jx),'P',iif(Candidate(m.jx),'C',iif(unique(m.jx),'U','R'))) 
       arrIndexes[m.jx,4] = descending(m.jx) 
       arrIndexes[m.jx,5] = sys(2021,m.jx) 
       arrIndexes[m.jx,6] = arrTables[m.ix,1] 
      Endfor 
      Insert into crsINDEXES from array arrIndexes 
     Endif 
    endif 
    Use 
Endfor 
Select crsSTRUCTS 
Copy to (m.tcDicDir+'NewStruc') 
Select crsINDEXES 
Copy to (m.tcDicDir+'NewIndexes') 

Create table (m.tcDicDir+'static') (FileName M nocptrans, FileBin M nocptrans) 
If !empty(m.tcStaticList) 
    lnStatic = alines(arrStatic,chrtran(m.tcStaticList,',',chr(13))) 
    For ix = 1 to m.lnStatic 
     lnFiles = adir(arrFiles,m.tcDataDir+trim(arrStatic[m.ix])+'.*') 
     For jx=1 to m.lnFiles 
      If inlist(justext(arrFiles[m.jx,1]),'DBF','CDX','FPT') 
       Insert into (m.tcDicDir+'static') values ; 
        (arrFiles[m.jx,1], FileToStr(m.tcDataDir+arrFiles[m.jx,1])) 
      Endif 
     Endfor 
     Release arrFiles 
    Endfor 
Endif 

CREATE TABLE (m.tcDicDir+'userstatic') (FileName c(50)) 
If !empty(m.tcUserStaticList) 
    lnUserStatic = alines(arrUserStatic,chrtran(m.tcUserStaticList,',',chr(13))) 
    For ix = 1 to m.lnUserStatic 
     lnFiles = adir(arrFiles,m.tcDataDir+trim(arrUserStatic[m.ix])+'.*') 
     For jx=1 to m.lnFiles 
      If inlist(justext(arrFiles[m.jx,1]),'DBF','CDX','FPT') 
       Insert into (m.tcDicDir+'userstatic') values (arrFiles[m.jx,1]) 
      Endif 
     Endfor 
     Release arrFiles 
    Endfor 
Endif 
close data all 
close tables all 
+0

謝謝切廷。我會檢查你的代碼。一個。 –