MapInfo Pro Developers User Group

 View Only
  • 1.  Using Python to run MapBasic and retrieve table values

    Posted 03-14-2022 11:24
    Hi All,

    I am wanting to retrieve table values from a query and assign the values as a variable within Python. So far I have used this Python code to run MapBasic to access these table values, however, after doing so cannot get them from MapBasic back into Python afterwards. Is there a way around this? Or can it done directly from Python?

    # Open Table File
    Open_tabInput = pro.Catalog.OpenTable(tabInput, "Points")
    # Select row of Tab File
    do("Select * From Points where rowid = 1 into selection")
    # Open Bowser
    do("Browse * From Selection")
    # Set variable
    do("Dim value as String")
    # Retrieve value
    do("value = Query1.ColName")

    Is it possible to get this variable "value" set in MapBasic into Python?
    Or is it possible to access this table value directly using Python instead of calling MapBasic?

    Any help is much appreciated.
    Thanks,
    Luke
    ​​​​​

    ------------------------------
    Luke Lynx
    Knowledge Community Shared Account
    ------------------------------


  • 2.  RE: Using Python to run MapBasic and retrieve table values
    Best Answer

    Posted 03-14-2022 16:53
    Hi Luke,

    The eval function is used to retrieve a value returned by a MapInfo command:

    sValue = eval("Query1.ColName")

    In addition, you do not need to open a browser or declare a MapBasic variable.  So your code would become:

    # Open Table File
    Open_tabInput = pro.Catalog.OpenTable(tabInput, "Points")
    # Set the current selection record within the tab file
    do("Fetch First From Points")
    # Retrieve value
    sValue = eval("Points.ColName")

    If you want to loop through the records to retrieve a value from each record, use the following within your loop to set the next record as current:
    do("Fetch Next From Points")

    Hopefully this helps.

    ------------------------------
    James Nolet
    Dooley Mitchell & Morrison Pty Ltd
    Mentone, VIC, Australia
    ------------------------------



  • 3.  RE: Using Python to run MapBasic and retrieve table values

    Posted 03-15-2022 06:57
    Hi James,

    Thanks for the response, this has been very helpful, much appreciated.

    Thanks,
    Luke

    ------------------------------
    Luke Lynx
    Knowledge Community Shared Account
    ------------------------------



  • 4.  RE: Using Python to run MapBasic and retrieve table values

    Employee
    Posted 03-15-2022 02:50
    Hi Luke

    It might also be worth reading @Anshul Goel's reply to this question: Read and Access TAB File Attributes in Python.​

    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------



  • 5.  RE: Using Python to run MapBasic and retrieve table values

    Posted 03-15-2022 06:59
    Edited by Luke Lynx 03-15-2022 06:59
    Hi Peter,

    Thanks for forwarding this post it has been very helpful.

    Thanks,
    Luke

    ------------------------------
    Luke Lynx
    Knowledge Community Shared Account
    ------------------------------