Learning Wiki - new experience
I have a mysql database which I wanted to dynamically display fields in a table in a Wiki page - (simple database named naicsdb consisting of 1 table named entity with 2 fields: codename and expansion).
I ended up spending a lot of time trying to find information that would be understandable and simple enough to accomplish the task; especially for someone new to this (new to python, mysql, wiki etc.). I discovered macro and ended up writing the following macro through an iterative (guesswork) process and managed to make it work successfully.
A call to a macro called LocalMySQL1 with 2 arguments (database name and table name) was name I came up with -- I decided to put the word Local in front to differentiate it from other macros.
Call to macro on my test wiki page
[[LocalMySQL1(naicsdb,entity)]]
OK, I had a name - now the following is relatively newbie code and can certainly be improved upon/expanded etc. It works and if this exercise helps someone else along the way, thats cool.
If you are now using a higher version of MySql 5.0+ and MySQLdb 1.0.0 you may want to check out following information: http://dev.mysql.com/doc/refman/5.0/en/old-client.html in order to resolve authentication protocol issues...
LocalMySQL1.py Code
1 import os
2 import MySQLdb
3
4 def execute(macro, text):
5 if text:
6 args = text.split(',')
7 else:
8 args = []
9 if len(args) < 2:
10 return '<font color="red">Not enough arguments for MACRO</font>'
11 try:
12 # make connection
13 db = MySQLdb.connect(host="localhost", user="", passwd="", db=str(args[0]))
14 # create a cursor
15 cursor = db.cursor()
16 # create SQL statement
17 query_1 = "SELECT * FROM " + str(args[1])
18 # execute SQL statement
19 cursor.execute(query_1)
20 # get the results as a tuple
21 results = cursor.fetchall()
22 # iterate through results
23 text = "<table>"
24 colortest = 0
25 for record in results:
26 if colortest == 0:
27 text += "<tr><td bgcolor=lightgreen>" + str(record[0]) + "</td><td bgcolor=lightgreen>" + str(record[1]) + "</td></tr>"
28 colortest = 1
29 continue
30 if colortest == 1:
31 text += "<tr><td bgcolor=lightblue>" + str(record[0]) + "</td><td bgcolor=lightblue>" + str(record[1]) + "</td></tr>"
32 colortest = 0
33 return text + "</table>"
34 except:
35 return '<font color="red">Check code for ERROR(s)!</font>'
Hints:
- almost never you need to import string - strings are objects with member functions.
- try avoiding using too much string + string, makes lots of garbage, esp. in loops. Use %s and lists.
- use the formatter
read PEP 8
Recoding in process!
Onward and Upward!
LocalMySQL1.py was placed in directory
C:\Python23\Lib\site-packages\MoinMoin\macro
Shouldn't there be a directory called localmacros? Just a thought.
- There is. See data/plugin/macro/.
Changed Location
C:\Moin\mywiki\data\plugin\macro
System information
Believe it or not an IBM Aptiva (Windows 98) with only 64 megs of ram running Apache/2.0.48 (Win32) mod_perl/1.99_12 Perl/v5.8.3 mod_python/3.1.3 Python/2.3.4 (package MySQLdb) MySQL/4.0.20d and MoinMoin/1.3.4 (How about that MoinMoin in bold isn't a link!)
"Now that I have all of this up and running - I better start learning!".... wjk
Or looking at MacroMarket - there is already an MySQL macro
A "sqlite3" macro would be nice to have as well! -- there may be one in progress... wjk.
Thank you
Thank you for your replies, hints and guidance... they are greatly appreciated... will take a further look at Pep 8, plugin and python code... wjk