First sample that I made is obviously based on XML, one of the best way to work with Flash Platform, but not only, also others Adobe softwares are working with this language.
In this little tutorial we’ll start to work with SQLite module and we’ll take a look on how to get data from this database and to create XML file that is ready to be parsed on our Flash or Flex application.
The first step is install Python SQLite module on your computer, to do this you must have GCC that you can find installing XCode for Mac OS X or you can download from sourceforge a GCC project for Windows too.
GCC is a C compiler that allow Python to install modules like MySQL, Bluetooth or SQLite one in your computer.
Then you can download pysqlite and you can install it directly with Terminal.
In Terminal when you arrive in the right folder you only write 2 lines of code to install all files:
1. python setup.py build (and press Enter key)
2. python setup.py install (and press Enter key)
In readme file you can find how to detect if all procedures are ok.
Now, we are ready to start working with SQLite in Python!
With another amazing tool called SQLite database browser, it is an open-source software that allow to manage SQLite databases, we create a new SQLite database with a table called myT and some records with 3 fields: name, surname and age, all fields will be TEXT type.
From your favourite IDE, you can open a new Python file and put these few lines of code:
#import modules to manage XML and SQLite
from xml.dom.minidom import Document
from pysqlite2 import dbapi2 as sqlite
#create database connection
conn = sqlite.connect(“firstDB.db“)
cur = conn.cursor()
#retrieve all database data
cur.execute(“SELECT * FROM myT”)
#create a new XML object
xmlDoc = Document()
#create XML document structure
allNodes = xmlDoc.createElement(“people”)
#popolate XML with database data
count = 0
for person in cur:
item = xmlDoc.createElement(“person”)
count += 1
#show in console the final XML
This is the result in Eclipse console panel:
Finally we can create a Flex, Flash Lite or Flash project that read XML made by Python and show it in our Flash Platform project.