Working with WebDNA database
numero = 158ID | NAME | NUMBER | COLOR | AGE |
1 | Peter | 534264 | yellow | 38 |
2 | John | 756233 | green | 42 |
--------------------------------------------------ID NAME NUMBER COLOR AGE1 Peter 534264 yellow 382 John 756233 green 42--------------------------------------------------The first line contains the field names. The following lines are the individual records, with data in the fields' corresponding columns.Let's add a new line. Make a new page, "databasetest.dna":
<!--HAS_WEBDNA_TAGS-->[append db=database1.db&autonumber=ID]name=Scott[!][/!]&number=545194[!][/!]&color=violet[!][/!]&age=23[/append]which would mean: append to database database1.db a new row (record), with a unique ID number, where the name is "Scott" and the number is "545194" and the colour is "violet" and the age is "23". Now, each time you hit http://127.0.0.1/databasetest.dna you will have a new line, each time with a different ID. The database now looks like
--------------------------------------------------ID NAME NUMBER COLOR AGE1 Peter 534264 yellow 382 John 756233 green 423 Scott 545194 violet 234 Scott 545194 violet 23--------------------------------------------------Oops! we now have two lines with "Scott" because you hit the file twice:Let's delete the extra line
[delete db=database1.db&eqIDdatarq=4]which would mean "delete from database1.db any entry with an ID equal 4"Though the databases will work without a unique ID field, it is a very good habit to include a unique field (could be called anything -- ID, sku, MemberID). Imagine how we could delete this specific record without the ID?
[delete db=database1.db&eqnamedatarq=Scott]with this line, we would delete both lines 3 and 4.However, there is a way to avoid duplicate records altogether. A very similar context is [replace]:
[replace db=database1.db&eqnamedatarq=Scott&append=t&autonumber=ID][!][/!]&number=545194[!][/!]&color=violet[!][/!]&age=23[/replace]This will append the data if it is not already there, but only update existing data if it IS already there. (Of course, if you have more than one Scott, it will update them all. Here is the perfect example of why you would use a unique ID to pick out specific records.)NOTE: In a real-world situation, you would not hard-code all of your data in an append or replace as in these examples. Instead, you would submit a form from the previous page (see step 3) to a page with WebDNA that would look like this:
[replace db=database1.db&eqnamedatarq=[name]&append=t&autonumber=ID][!][/!]&number=[number][!][/!]&color=[color][!][/!]&age=[age][/replace]>>> Next page... Let's continue with the all-important databases. What is this "database" part of WebDNA? Well, WebDNA handles its own built-in database system. These databases reside in RAM, and are extremely fast, much much faster than disk-based databases. The RAM-based WebDNA databases are loaded from simple tab-delimited text files on disk and written back to disk, so there is no risk to lose content if the server crashes.
ID | NAME | NUMBER | COLOR | AGE |
1 | Peter | 534264 | yellow | 38 |
2 | John | 756233 | green | 42 |
--------------------------------------------------
ID NAME NUMBER COLOR AGE
1 Peter 534264 yellow 38
2 John 756233 green 42
--------------------------------------------------
<!--HAS_WEBDNA_TAGS-->
[append db=database1.db&autonumber=ID]name=Scott[!]
[/!]&number=545194[!]
[/!]&color=violet[!]
[/!]&age=23[/append]
--------------------------------------------------
ID NAME NUMBER COLOR AGE
1 Peter 534264 yellow 38
2 John 756233 green 42
3 Scott 545194 violet 23
4 Scott 545194 violet 23
--------------------------------------------------
[delete db=database1.db&eqIDdatarq=4]
[delete db=database1.db&eqnamedatarq=Scott]
[replace db=database1.db&eqnamedatarq=Scott&append=t&autonumber=ID][!]
[/!]&number=545194[!]
[/!]&color=violet[!]
[/!]&age=23[/replace]
[replace db=database1.db&eqnamedatarq=[name]&append=t&autonumber=ID][!]
[/!]&number=[number][!]
[/!]&color=[color][!]
[/!]&age=[age][/replace]
DOWNLOAD WEBDNA NOW!
A short introduction to WebDNA programming for absolute beginner...
4. Password ProtectingHow to password-protect a page using WebDNA...
1. Learn WebDNA ProgrammingLearning WebDNA programming: the very basics...
Kendo UIThis is a Code Library built with WebDNA...
3. Passing Data from a FormPassing data from a form and retrieving it using WebDNA...
2. Conditional ProgrammingConditional programming with WebDNA...