Python [examples]

Run Python code


// Character field
create table if not exists scripts (script char(200))
if not used("scripts")
	use scripts
select scripts
append blank
replace script with 'for i in range(9):' + CHR(13)+ '# process commands' + CHR(13)
// Text constant
execpython('for i in range(9):' + CHR(13) + '# process commands' + CHR(13))
// Memory variable
m_script = 'for i in range(9):' + CHR(13) + '# process commands' + CHR(13)


To open a Lianja database from Python

import Lianja
db = Lianja.openDatabase("southwind")


After we have opened a recordset we can navigate through the data using any of the recordset data navigation methods of the Recordset class.

Access a table in the database using the openRecordSet() method of the Database class using SQL or noSQL. For example we can access the customers table using SQL like this:

rs = db.openRecordSet("select * from customers")

Or alternatively just open the customers table with noSQL like this:

rs = db.openRecordSet("customers")

Recordset data navigation

For example to position on the first record in a recordset use the moveFirst() method.


Extract data from the Recordset

When you are positioned on a particular record in a recordset you can extract data using the fields() method.

for i in range(rs.fields.count):
   name = rs.fields(i).name
   value = rs.fields(i).value

The fields() method can also take the field name as an argument as well as the column ordinal position.

value = rs.fields("amount").value

Filtering selected data

When you open a recordset with an SQL select statement, the data selected is only that which matches the where condition of the select statement. If you open a table with noSQL e.g.

rs = db.openRecordSet("customers")

noSQL keyed data access

When opening a table in noSQL mode, you can lookup records by keys.

rs = db.openRecordSet("customers")
rs.index = "id""12345")
if rs.nomatch():
   # key was not found.

Adding new records

You can add new blank records to a recordset using the addNew() method.


Note that after executing addNew() the record is not written until the update() method is called. This allows you to update the fields of the blank record prior to it being committed into the table.

Updating records

You can update records in a recordset using the update() method.

rs = db.openRecordSet("customers")
rs.index = "id""12345")
if !rs.nomatch():
   rs.fields("amount").value = rs.fields("amount").value + 1000

Deleting records

You can delete records in a recordset using the delete() method.

rs = db.openRecordSet("customers")
rs.index = "id""12345")
if !rs.noMatch():

Closing a Recordset

You close a recordset using the close() method.




I have a python library called

I want to load that library in a prg called via lianjaruntim.exe
I have an install on c:\lianja and I am testing this locally.
The app is called mbcore.
So the files are sitting for now in “c:\lianja\apps\mbcore\”

What setting would I need to set in order to use loadlibrary() correctly.

I have tried –runtimedir and –tenancy.


Put in the library and reference it using the ‘lib:/’ prefix.


I have copied the file to the library.

In my proc, I am now calling

isloaded = loadlibrary(“lib:/”)

I am calling it like this in the command line.

c:\lianja\bin\lianjaruntime.exe c:\lianja\apps\mbcore\convert2cropgifremote.dbo –runtime –runtimedir c:\lianja\apps\mbcore\ –args “‘BYS2EP20_1_000001.jpg’,’BYS2EP20_1_000301.jpg’,5 0,’R:\bys2ep20\bys2ep20_ROLL1′,1,’bys2ep20′”

The library is not getting loaded.

If I hard code the path to the library, it does load, but then doesnt call the python function.


in the deployed cloudserver path library


This is the code of the gadget who crash the IDE with any kind of code:

define class Seleccion as Commandbutton

procedure click(id)
//a=messagebox(“Hola Mundo”)
select employees


In Lianja, you would use define and endDefine for classes, not endclass.


I have the module in that deployment folder (.py in the Lib folder and .pyd in the DLLs folder) yet the app still breaks on the “import xyz” line in my app when not run through the app builder.
check that any modules required by your xyz module are also installed on the deployment target machine.

where to put python DLLs (.pyd) that are called in python modules we want to import? I’ve tried putting them in both …/Lianja/scriptinglanguages/python/DLLs and in …/Lianja/scriptinglanguages/python/Lib. In the “Using Python Modules” thread on here it mentions registry key editing and I’ve followed those instructions as well. What is happening now is:

  • I have a python DLL “mydll.pyd” in the 2 folders mentioned above
  • I have a script “” in …/Lianja/scriptinglanguages/python/Lib which has at the top:


import mydll

When I runCode:

import myscript

in the Lianja Python console, it pops out:

**** Lianja Python error ****
Traceback (most recent call last):
  File "Console" at line 1, in <module>
  File "C:\lianja\scriptinglanguages\python\lib\" at line 7, in <module>
    import mydll
ImportError: DLL load failed: The specified module could not be found.

If the python DLL has been built against the version of Python supported in Lianja (which is 2.7 on windows) it works by just placing the DLL in the DLL directory.
You can verify this from the Python console.

import sqlite3

where sqlite3.dll exists in C:\lianja\scriptinglanguages\python\DLLs
If you exit Lianja and rename the sqllite3.dll to xxx_sqlite3.dll and then run Lianja again and do the same sequence of steps the DLL will not be found.
The DLL must be a 32-bit DLL not a 64-bit version, and it must be compatible with Python 2.7.

how python imports work when deploying desktop apps? I assume I have to put the imported .py and .pyd files somewhere on the client system in a similar place to where I have them on the dev system. I’ve tried putting them in the same place (lianja/scriptinglanguages/python) and in cloudserver/scriptingilanguages/python.
The deployment path for Python modules is:


The development path for Python modules is:


The drive letter where the exe is run from is prefixed onto it and replaces C:
After deploying look in file explorer and make sure the files are indeed in the place you expect them to be.

it told me I had the wrong version of python installed. So I uninstalled my Python 3.5 and installed Python 2.7, the same version Lianja uses.
it seems to be an issue with the way node.js structures its directories. I had a look through the phonegap directory tree on the computer we have with phonegap working and it doesn’t seem to have “node_modules\bufferutil” or “node_modules\utf-8-validate”, it goes straight from “ws” to “build”. Without those folders the entire path is just barely under the Windows character limit for a folder name, with them it’s just barely over.
Do you know how to get around this or why it’s putting those two folders in?
Lianja is not using those folders. It seems you installed visual studio with PhoneGap tools and it has basically messed up any ability to install PhoneGap and node.js for use with Lianja.
It’s the path that visual studio has setup for node.js. It’s too long as stated in the error messages
It’s highly likely that visual studio has custom versions of PhoneGap and node.js installed that prevent it being used by other products.
Yep that was it, I uninstalled Visual Studio and reinstalled Lianja and now everything works

I have problem about Lianja App Builder.
Sometimes to often my App Builder stop working immediately after changing my python file a switching from Apps to Pages.
I’m using Windows 7 32 bit and Lianja full download with all services, but Trial.
I’m going to add some code below to check if my code isn’t bad.
Before my Lianja stopped working, I had the following code:

# Lianja custom Python section "page1_section1"
import Lianja
# Step 1: Define the classes we need
# Note that all Lianja UI Framework classes can be subclassed in Python
class mySection(Lianja.Section):
    def add(self):
        Lianja.showMessage("add() was called")
    def delete(self):
        Lianja.showMessage("delete() was called")
    def first(self):
        Lianja.showMessage("first() was called")
    def previous(self):
        Lianja.showMessage("previous() was called")
    def next(self):
        Lianja.showMessage("next() was called")
    def last(self):
        Lianja.showMessage("last() was called")
    def refresh(self):
    def search(self):
    def watch(self):
        Lianja.showMessage("watch() was called")
    def edit(self):
        Lianja.showMessage("edit() was called")
    def save(self):
        Lianja.showMessage("save() was called")
    def cancel(self):
        Lianja.showMessage("cancel() was called")
# Note how the click() event handler for the ListBox is defined in Python
class myListbox(Lianja.Listbox):
    def click(self):
        ui_grid.clear( )
        # Note how the AddItems method of Grid, ListBox and ComboBox can take 
        # a SQL SELECT statement as an argument
        if ui_listbox.text == "All":
            ui_grid.additems('select * from southwind!example where last_name != " "')
            ui_grid.additems('select * from southwind!example where upper(left(last_name,1)) \
            = "' + ui_listbox.text + '"')
        ui_grid.refresh( )

This works when I changed from Apps to Pages.
But when I added following lines of code, my Lianja stop working, and after restart I cannot access my App, It stop working again and again..

# Step 2: Create the Section object
# Note that the CreateObject() method needs two args: objectname, classname
oSection = Lianja.createObject("oSection", "mySection")
oSection.backcolor = "lightgreen"
oSection.caption = "This is a custom Python section

Stop working means Windows pop-up dialog stop working, and just one way is to close the app.
The reason I asked you to cut and paste the code is that Python syntax is all based on whitespace indentation.
1. oSection.caption = “This is a custom Python section

This is missing a closing quote.

2. This is not a valid custom section as you have removed this code that was generated for you and is required.

oSection = Lianja.createObject("oSection", "mySection")
returnvalue = oSection

you will see some Python sections and gadgets in the pages menu. Select the python section, then click the little keyboard icon in the section header to edit/view the code.

The developers guide on the Lianja website has documentation on how to use Python to build custom sections and custom gadgets.…velopers_Guide

I have problem about debugging in Python. When I click Open file in debugger, it says: “You cannot run the debugger on this type of file.”
It is possible to debug in python?
No the debugger is for Lianja/VFP code only.

I have a file in my application called
I would like to call it from a Custom VFP section.
I have tried execPython(“”), but that is not calling it.
once I loaded the file using loadlibrary(), all of my python functions are working using execPython(“functionName()”).

in Lianja 3.1 the execPython( expr ), execPHP( expr ) and execJavaScript( expr ) Lianja/VFP functions return the result of the executed code. This makes it simple to integrate Lianja/VFP with Python/PHP/JavaScript. If expr is a filename it it loaded and executed.

And also just to remind you.

1. In Python, PHP and JavaScript you can call Lianja.evaluate( expr ) to call back into Lianja/VFP.
2. The Lianja/VFP loadLibrary( filename ) function can load Python (.py), PHP (.php) and JavaScript (.js) libraries too.

This works fine in my application.

for i in range(1,20):
        <one tab Here>Lianja.console(str(i))

But it looks like the it is not picking up both lines when I try to test it in the app inspector or console.
You can only issue single-line commands in the Console/App Inspector Command Windows.

I have a migration process to Lianja occurring in stages.
But the data will be live I Lianja from Day 1.
I need the rest of the applications to be able to read the data from python until they are all moved over.
I am testing out how best to access the Lianja databases from outside Python code.
To test, I have configured the Lianja ODBC connection “Lianja_Soutwind” and successfully tested the connection.
However, I am unable to connect using pyodbc.
Is there a different, recommended way I should be connecting to Lianja?

We have an internal team at the Fund that uses Python a lot. I am trying to show them the light of Lianja
So for now, I do need to test both the code in Lianja and from the current external install.
You would appear to be using your own Python installation which is probably x64 rather than the python we provide that is also embedded in Lianja.
As per the error message you have a x64 and x86 mismatch of drivers.

You dont need QT to build full database interactive openCV applications.
By usings Lianja python and native database support, you can extend opencv easier than you ever thought possible.
Lianja openCV threshold part 1 –

Taking the C++ threshold example, building it in python, then extending it as a web application using Lianja.
Lianja openCV threshold part 2 –

Using the robust Lianja framework with python/OpenCV, we build an application that converts any image to a pencil sketch.
openCVLIanja –

Just did a quick and dirty demo of Python with newest Data Aware IDE/Platform on the Market called Lianja.
I have been developing apps for over 20 years and just started with Python.
I have used the Lianja product for a couple years now building desktop, web and mobile apps.
It’s fairly new, but makes developing data aware applications simple.
Python LIanja Intro –


Did you copy the the pyglet folder – i.e. the folder under pyglet-1.1.4 – into C:\lianja\scriptinglanguages\python\Lib\site-packages\?

From Python, you can use:
Lianja.execute(‘Lianja.showdocument(“page:name_of_ page”)’)
to select a particular Page. Lianja.activePage returns an object reference for the currently active Page. If you get the chance, can you submit a ticket with an App that displays the ‘white flash’ – I haven’t seen that.

I have some VFP global variables declared in App Init() event. What would be the best way to access those in a python event delegate?
For example I have an App, myapp, with the myapp_init.prg (App init()):

public m_var = "Hello World"

Then a Python Custom Section:

import Lianja

print "<html>"
print "<head>"
print "</head>"
print "<body>"
m_pub = Lianja.evaluate("m_var")
print m_pub
# Or, if all I want to do is print the value
# print Lianja.evaluate("m_var")
print "</body>"
print "</html>"

With the modules installed in the Lianja Python path under C:\lianja\scriptinglanguages\python\Lib\ (default installation) you should be able to import as in your existing Python code. Check out…Python-modules for information on Registry tweaks that may be required.
Remember that you can also use the Python tab in the Console Workspace to test out imports (and see any error messages if the modules aren’t found) before running your complete script.

It looks like you don’t yet have pywin32 installed and wmi sits on top of it. Once you have pywin32 installed, copy the dlls from C:\Lianja\scriptinglanguages\python\Lib\site-packages\pywin32_system32 into your Windows System folder and you should be up and running.

for num in range(5):
Lianja.writeLog() takes a string.