4

这似乎是一个如此简单的问题,但我似乎无法在任何地方找到解决方案。我和我的同事正在开发一个使用 Foxpro xml 转储工具的应用程序。它工作得很好,但我们希望根据一些大小限制将表拆分为多个文件。

这似乎应该是简单的部分:您如何在 Foxpro 中找到光标的大小?

4

3 回答 3

2

RECSIZE() 将以字节为单位返回单个行的长度- RECCOUNT() 将为您提供大小。已经讨论的所有要素都是准确的。

对于备注字段,如果您需要知道它们有多大,您可能需要在表结构中为“MemoLength”添加一个新的整数列。然后

用 len( alltrim( YourMemoField )) 替换所有 memoLength

然后,您可以通过将此列大小与您要提取的其余 RECSIZE() * 行一起考虑来帮助确定您的细分组。

此外,您可能希望根据可以用作链接的表的主键列运行查询并执行类似...

选择 YourPrimaryKey, len( alltrim( YourMemoField )) as MemoLength 从 YourTable 到光标 SomeHoldingCursor 读写

.. 或者,选择

进入表 MemSizeTable

在 MemSizeTable 上建立一个索引,您可以使用它来获取更多信息。这样,它不会扭曲您的原始记录大小,也不会破坏您的原始表结构,但通过关系,您仍然可以提取您需要的元素。

于 2010-01-23T03:19:58.760 回答
1

如果您的意思是文件大小,您可以通过以游标为别名调用 DBF() 函数来查找与游标相关的文件,检查返回值扩展名是否为 .dbf,然后使用文件函数读取文件大小。虽然游标可能在内存中(如果我没记错的话,报告的“文件名”将具有 .tmp 扩展名)因此另一种方法是使用 RECCOUNT() (获取行数)结合 AFIELDS() (获取每行的大小)以近似文件大小。(内存游标有时可以通过在生成查询中包含 NOFILTER 子句来强制到磁盘)

于 2010-01-22T15:02:07.877 回答
1

这是一个基于示例游标和虚假记录的完整功能......关键功能是 DumpXML() 例程,需要转储文件的别名,每个文件的大小要限制在(在“k " size),以及您希望将 XML 转储为的文件名前缀。它将自动生成一个排序 ex:MyXMLOutput1.xml、MyXMLOutput2.xml、MyXMLOutput3.xml 等,无论它需要多少实例。花了我大约15分钟。

CREATE CURSOR SomeTest ;
    (   SomeField1      c(10),;
        AnotherField    i,;
        SomeNumber      N(8,2),;
        MemoFld         m,;
        SomeDateTime    t;
    )

INSERT INTO SomeTest VALUES ( "testchar10", 9403, 12345.78, "some memo value string", DATETIME() )


DumpXML( ALIAS(), 300, "MyXML" )


FUNCTION dumpXML
LPARAMETERS cAliasName, nSizeLimit, cNameOfXMLOutput
    IF NOT USED( cAliasName )
        RETURN ""
    ENDIF 

    */ Assume size limit in "k"
    nSizeLimit  = nSizeLimit * 1024

    SELECT ( cAliasName )

    */ Get a copy of the structure without disrupting original
    USE IN SELECT( "MySample" )  && pre-close in case left open from prior cycle
    SELECT * ;
        FROM ( cAliasName ) ;
        WHERE RECNO() = 1;
        INTO CURSOR MySample READWRITE

    SELECT MySample
    */ Populate each field with maximum capacities... typically
    */ critical for your char based fields
    AFIELDS( aActualStru )
    cMemoFields = ""
    lHasMemoFields = .f.
    FOR I = 1 TO FCOUNT()
        cFieldName = FIELD(I)
        DO CASE 
        CASE aActualStru[i,2] = "C"
            replace &cFieldName WITH REPLICATE( "X", aActualStru[i,3] )

        CASE aActualStru[i,2] = "L"
            replace &cFieldName WITH .T.

        CASE aActualStru[i,2] = "D"
            replace &cFieldName WITH DATE()

        CASE aActualStru[i,2] = "T"
            replace &cFieldName WITH DATETIME()

        CASE aActualStru[i,2] = "M"
            */ Default memo as a single character to ensure
            */ closing field name </endoffield> included in XML
            replace &cFieldName WITH "X"

            */ if a MEMO field, add this element to a string 
            */ to be macro'd to detect its size... Each record
            */ can contain MORE than one memo field...
            */ Ex: + LEN( ALLTRIM( MemoFld ))
            lHasMemoFields = .T.
            cMemoFields = cMemoFields + " + len( ALLTRIM( " + cFieldName + " ))"

        CASE aActualStru[i,2] = "I"
            */ Integer, force to just 10 1's
            replace &cFieldName WITH 1111111111


        CASE aActualStru[i,2] = "N"
            */ Actual numeric and not an integer, double or float
            */ Allow for full length plus decimal positions
            NumValue = VAL( REPLICATE( "9", aActualStru[i,3] - aActualStru[i,4] - 1 );
                            + "." + REPLICATE( "9", aActualStru[i,4] ))
            replace &cFieldName WITH NumValue

        ENDCASE     
    ENDFOR 

    */ Strip leading " + " from the string in case multiple fields
    IF lHasMemoFields
        cMemoFields = SUBSTR( cMemoFields, 3 )
    ENDIF 

    cXML = ""
    LOCAL oXML as XMLAdapter

    oXML = CREATEOBJECT( "XMLAdapter" )
    oXML.AddTableSchema( "MySample" )
    oXML.ToXML( "cXML", "", .f. )

    */ Now, determine the size of the per record at its full length -- less memo        
    nSizeOfPerRecord = LEN( STREXTRACT( cXML, "<MySample>", "</MySample>", 1, 4 ))

    */ and the rest of the header per XML dump
    nSizeOfSchema = LEN( cXML ) - nSizeOfPerRecord


    */ Now, back to the production alias to be split
    SELECT( cAliasName )
    nNewSize = 0
    nXMLCycle = 0

    */ if we just started, or finished writing another block
    */ and need to generate a new group of XML dump reset size
    nNewSize = nSizeOfSchema

    */ Always blank out the temp cursor for each batch...
    SELECT MySample
    ZAP 

    SELECT ( cAliasName )
    SCAN 
        IF lHasMemoFields
            nAllMemoSizes = &cMemoFields
        ELSE 
            nAllMemoSizes = 0
        ENDIF 

        IF nNewSize + nSizeOfPerRecord + nAllMemoSizes > nSizeLimit
            */ The upcoming record will have exceeded capacity, finish XML
            */ with all records up to this point
            nXMLCycle = nXMLCycle + 1
            cNewFile = FULLPATH( cNameOfXMLOutput + ALLTRIM( STR( nXMLCycle )) + ".XML" )

            oXML = CREATEOBJECT( "XMLAdapter" )
            oXML.AddTableSchema( "MySample" )
            */ Generate the XML cycle of these qualified records...
            oXML.ToXML( cNewFile, "", .t. )

            */ restart for next pass of data
            nNewSize = nSizeOfSchema

            */ Always blank out the temp cursor for each batch...
            SELECT MySample
            ZAP 
        ENDIF 

        */ Add record to total size...
        nNewSize = nNewSize + nSizeOfPerRecord + nAllMemoSizes

        */ we have a record to be included in segment dump...
        */ scatter from the original table and gather into the temp
        SCATTER MEMO NAME oFromOriginal

        SELECT MySample
        APPEND BLANK
        GATHER MEMO NAME oFromOriginal

        */ back to original table driving the XML Dump process
        SELECT ( cAliasName )

    ENDSCAN 


    */ if the "MyTable" has records not yet flushed from limit, write that too
    IF RECCOUNT( "MySample" ) > 0
        */ The upcoming record will have exceeded capacity, finish XML
        */ with all records up to this point
        nXMLCycle = nXMLCycle + 1
        cNewFile = FULLPATH( cNameOfXMLOutput + ALLTRIM( STR( nXMLCycle )) + ".XML" )

        oXML = CREATEOBJECT( "XMLAdapter" )
        oXML.AddTableSchema( "MySample" )
        */ Generate the XML cycle of these qualified records...
        oXML.ToXML( cNewFile, "", .t. )
    ENDIF 

    */ Done with the "MySample" for cursor to XML analysis...
    USE IN SELECT( "MySample" )

ENDFUNC 
于 2010-01-28T15:51:32.127 回答