Demonstrate the Database plugin
Here are some examples showing how to access an external Databasesystem fromn within TWiki. Two database systems are used:
SQLite and
Mysql. SQLite is the simpler of the two. In both cases the database
will need to be configured within the TWiki before use, so please contact the admins at
physicstwikiadmin@lists.rhul.ac.uk if you wish to use this plugin.
For SQLite it should be possible for users to externally modify a database file and upload it to the TWiki for use there by
attaching it to a topic.
See
DatabasePlugin for plugin details.
Example 1
In this example the request will make a simple query to an
SQLite database. The database contains a table listing the attendees at Widecombe Fair according to a folk song.
Request:
%DATABASE_SQL{description="test_data" sql="SELECT * FROM Widecombe_Fair" header="|*Forename* | *Surname* |" format="| $forename | $surname |"}%
Returned Data:
Forename |
Surname |
Bill |
Brewer |
Jan |
Stewer |
Peter |
Gurney |
Peter |
Davy |
Daniel |
Whiddon |
Harry |
Hawke |
Tom |
Cobley |
Example 2
As above but only list attendees whose forname is Peter.
Request:
%DATABASE_SQL{description="test_data" sql="SELECT * FROM Widecombe_Fair WHERE forename='Peter'" header="|*Forename* | *Surname* |" format="| $forename | $surname |"}%
Returned Data:
Example 3
Insert some dynamical information obtained from
TWikiVariables into an SQLite database table. This demo also uses the
GeoLookupPlugin to lookup the remote IP's geographical location. Everytime the topic is viewed the current information is inserted into the SQLite database table TWiki_Demo. The last 20 rows of the table are then dumped as a TWiki table in descending order. The following SQL command creates/initialises the table,
CREATE TABLE TWiki_Demo (int primary key, username varchar, wikiusername varchar, time varchar, ip varchar, location varchar)
Request:
%DATABASE_SQL{description="test_data" sql="INSERT INTO 'TWiki_demo' VALUES(NULL, \"%USERNAME%\", \"%WIKIUSERNAME%\", \"%SERVERTIME%\", \"%REMOTE_ADDR%\", \"%GEOLOOKUP{%REMOTE_ADDR%}%\" )"}%
inserts the dynamical information into the SQLite database table.
Request:
%DATABASE_SQL{description="test_data" sql="SELECT * FROM TWiki_demo order by 1 DESC limit 20" header="| *Username* | *Wikiusername* | *Time* | *Remote IP* | *Remote Location* |" format="| $username | $wikiusername | $time | $ip | $location |"}%
displays the updated contents of SQLite database table.
Returned Data:
Username |
Wikiusername |
Time |
Remote IP |
Remote Location |
guest |
TWikiGuest |
07 Oct 2024 - 18:34 |
44.220.184.63 |
San Diego, CA, USA |
guest |
TWikiGuest |
07 Oct 2024 - 07:06 |
40.77.167.35 |
Boydton, VA, USA |
guest |
TWikiGuest |
07 Oct 2024 - 07:06 |
40.77.167.35 |
Boydton, VA, USA |
guest |
TWikiGuest |
06 Oct 2024 - 11:07 |
40.77.167.23 |
Boydton, VA, USA |
guest |
TWikiGuest |
05 Oct 2024 - 21:49 |
17.241.75.198 |
Cupertino, CA, USA |
guest |
TWikiGuest |
05 Oct 2024 - 17:45 |
40.77.167.144 |
Boydton, VA, USA |
guest |
TWikiGuest |
05 Oct 2024 - 13:25 |
207.46.13.127 |
Redmond, WA, USA |
guest |
TWikiGuest |
04 Oct 2024 - 23:37 |
17.241.227.119 |
Cupertino, CA, USA |
guest |
TWikiGuest |
04 Oct 2024 - 16:56 |
157.55.39.56 |
Redmond, WA, USA |
guest |
TWikiGuest |
04 Oct 2024 - 16:23 |
207.46.13.102 |
Redmond, WA, USA |
guest |
TWikiGuest |
04 Oct 2024 - 16:03 |
92.233.89.67 |
Edinburgh, U8, United Kingdom |
guest |
TWikiGuest |
03 Oct 2024 - 23:10 |
157.55.39.10 |
Redmond, WA, USA |
guest |
TWikiGuest |
03 Oct 2024 - 14:32 |
207.46.13.87 |
Redmond, WA, USA |
guest |
TWikiGuest |
02 Oct 2024 - 18:05 |
52.167.144.166 |
Boydton, VA, USA |
guest |
TWikiGuest |
02 Oct 2024 - 13:41 |
66.249.77.128 |
Mountain View, CA, USA |
guest |
TWikiGuest |
02 Oct 2024 - 13:38 |
66.249.76.128 |
Mountain View, CA, USA |
guest |
TWikiGuest |
02 Oct 2024 - 13:38 |
66.249.76.128 |
Mountain View, CA, USA |
guest |
TWikiGuest |
02 Oct 2024 - 02:57 |
40.77.167.152 |
Boydton, VA, USA |
guest |
TWikiGuest |
01 Oct 2024 - 22:42 |
66.249.76.136 |
Mountain View, CA, USA |
guest |
TWikiGuest |
01 Oct 2024 - 22:07 |
66.249.79.107 |
Mountain View, CA, USA |
Example 4
Read a variety of different types of information from a user using a TWIki form and store it in SQLite database table. The last 20 rows of the table are then dumped as a TWiki table in descending order. The following SQL command creates/initialises the table,
CREATE TABLE Mychoices (int primary key, info varchar, choice varchar, mycheck varchar, options varchar, multichoice varchar);
See
https://twiki.ph.rhul.ac.uk/twiki/bin/view/ShowCase/PluginTestDatabase?raw=on to view the Form's TWiki markup language code and
%DATABASE_SQL{}%
calls below. To use, fill in the Info text field, select values for the other options and click
Commit to commit the data to the database. In particular, note the escaping needed in the
%DATABASE_SQL{}%
call's arguments for the
"
and
%
characters. The
"
are double-escaped. Such escaping is needed inside the
%CALC%
call which is needed to delay execution of the
%DATABASE_SQL{}%
call to prevent it being executed when the
%IF{}%
clause's then statement is not executed, when a user views the page but does not click the
Commit button.
Returned Data:
Info |
Choice |
Mycheck |
Options |
Multichoice |
TWiki-6.0.2 devtwiki test |
Excellent |
on |
Option#1 |
optiona |
Special entry |
Bad |
on |
Option#3 |
optionc |
|
Excellent |
|
Option#1 |
optionc |
|
Rubbish |
on |
Option#3 |
optionc |
|
Good |
on |
Option#2 |
optionb |
|
Indifferent |
|
Option#2 |
optionc |
BoogertTest |
Excellent |
on |
Option#2 |
optiona |
6th entry |
Bad |
on |
Option#3 |
optiona |
5th entry |
Indifferent |
on |
Option#2 |
optiona |
forth entry |
Rubbish |
|
Option#1 |
optionc |
Example 5
In this example the request will make a simple query as in
Example 1 above but this time to a
Mysql database. Mysql is a much larger, more sophisticated database system than Sqlite and works on a client server basis rather than as a standalone utility. Consequently, more back-end sysadmin work is required to configure it. Please contact the admins at
physicstwikiadmin@lists.rhul.ac.uk for more information if you would like to use Mysql.
Request:
%DATABASE_SQL{description="twikidemo" sql="SELECT * FROM Widecombe_Fair" header="| *Forename* | *Surname* |" format="| $forename | $surname |"}%
Returned Data:
Forename |
Surname |
Bill |
Brewer |
Jan |
Stewer |
Peter |
Gurney |
Peter |
Davy |
Daniel |
Whiddon |
Harry |
Hawke |
Tom |
Cobley |
Example 6
This more sophisticated example demonstrates using SQL to query the Classic Models
Mysql sample database available for download at
http://www.mysqltutorial.org/mysql-sample-database.aspx.
It represents the company records of a classic model car company.
The procedure uses the TWiki Form below to select the table and query. There are 8 data-tables within the Classic Models database as follows;
To use,
- Select a table in the Table pull-down menu
- Enter an SQL instruction in the SQL query text field. Be sure to specify the same table in the query and Table pull-down menu.
- Click Submit
Some example SQL queries.
Cut and paste these queries into the TWiki form below.
- Show all customer data records
select * FROM classicmodels.customers
- Show data on UK only customers
select * FROM classicmodels.customers WHERE country='UK'
- Show all employee data records
select * FROM classicmodels.employees
- Show employees who report directly to the company president
select * FROM classicmodels.employees WHERE reportsTo=1002
- Show all office data records
select * FROM classicmodels.offices
- Show all order details records
select * FROM classicmodels.orderdetails
- Show order details records where more than 20 items were ordered.
select * FROM classicmodels.orderdetails WHERE quantityOrdered > 20
- Show order details records where between 50 and 56 items were ordered.
select * FROM classicmodels.orderdetails WHERE quantityOrdered > 50 AND quantityOrdered < 56
- Show order details records where between 50 and 56 items were ordered and costed more than $100.
select * FROM classicmodels.orderdetails WHERE quantityOrdered > 50 AND quantityOrdered < 56 AND priceEach > 100.00
select * FROM classicmodels.orders
- Show orders records for items shipped in May 2005
select * FROM classicmodels.orders WHERE shippedDate > '2005-05-00' AND shippedDate < '2005-06-00'
- Show payments records involving payments of more than $100,000
select * FROM classicmodels.payments WHERE amount > 100000;
- Show payments records involving payments of more than $100,000 that were paid on 5th May 2005
select * FROM classicmodels.payments WHERE amount > 100000 AND paymentDate='2005-03-05'
select * FROM classicmodels.productlines
- Show the first 3 product lines
select * FROM classicmodels.productlines LIMIT 3
- Show products where fewer than 1000 items are currently in stock
select * FROM classicmodels.products WHERE quantityInStock < 1000
- Show products where fewer than 1000 or more than 9000 items are currently in stock
select * FROM classicmodels.products WHERE quantityInStock < 1000 OR quantityInStock > 9000
- Show models with a 1:32 scale where fewer than 1000 or more than 9000 items are currently in stock
select * FROM classicmodels.products WHERE ( quantityInStock < 1000 OR quantityInStock > 9000 ) AND productScale='1:32'
Notes.
- SQL language is very rich and these examples only provide a very small example of what is possible by way of selecting and displaying data. This topic uses a separate
%DATABASE_SQL%
call for each of the 8 tables and displays all fields present in the table. To display fields selectively, individual %DATABASE_SQL%
calls must be provided.
See
https://twiki.ph.rhul.ac.uk/twiki/bin/view/ShowCase/PluginTestDatabase?raw=on to view the Form's TWiki markup language code and the
%DATABASE_SQL{}%
calls
--
TomCraneAdmin - 13 August 2012