Hello all! I've been working on a small DLL plugin for DBP for a couple of days called DBData. This plugin allows DarkBASIC Pro programmers the ability to create and modify database files for their projects.
These database files are a custom format (nothing special), which stores multiple tables with data in one file.
An example of creating a database:
create database "test.dbd"
`---------------------------------
` Create a master inventory table.
`---------------------------------
create table "MasterInventory"
add string field "Name", 50
add integer field "Quantity"
add integer field "Damage"
add integer field "RequiredLevel"
finish creating table
finish creating database
Code to access the database is also simple. The DBData system is not quite done (thus the WIP posting), and features include:
- Creating databases
- Multiple tables per file
- INTEGER, FLOAT, and STRING field types
- Moving back and forward through records (bi-directional)
- Deleting records
- Adding records
Planned features:
- Search and filter table capabilities
- Table indexes for faster record searches
You can download the DLL from
http://www.mighty-atom.com/d-zone/downloads/DBData.zip. Simply copy it to your CompilerPlugins-user directory.
Screenie:
Here is the code that produced the program you see above...
#constant ALIGN_LEFT = 1
#constant ALIGN_RIGHT = 2
#constant ALIGN_CENTER = 3
`---------------------------------------------
` If the database file doesn't exist, make it.
`---------------------------------------------
if file exist("test.dbd") = 0
`----------------------------------------
` The database does not exist. Create it.
`----------------------------------------
create database "test.dbd"
`---------------------------------
` Create a master inventory table.
`---------------------------------
create table "MasterInventory"
add string field "Name", 50
add integer field "Quantity"
add integer field "Damage"
add integer field "RequiredLevel"
finish creating table
finish creating database
endif
`-------------------
` Open the database.
`-------------------
open database "test.dbd", 1
finished = 0
while not finished
cls
print "A simple test of the DBData System (by TheOneRing)"
print
print "1. View inventory items"
print "2. Add an inventory item"
print "3. Exit"
clear entry buffer
Key = scancode()
select Key
case 2
gosub __view_inventory
endcase
case 3
gosub __add_inventory
endcase
case 4
finished = 1
endcase
endselect
endwhile
close database 1
end
__view_inventory:
clear entry buffer
print
print FixedLengthString("Name", ALIGN_CENTER, 50);
print FixedLengthString("Qty", ALIGN_RIGHT, 8);
print FixedLengthString("Damage", ALIGN_RIGHT, 10);
print FixedLengthString("Lvl", ALIGN_RIGHT, 8)
for index = 1 to 76
print "-";
next index
print
`---------------------------------------
` If there are no records tell the user.
`---------------------------------------
if get record count(1, "MasterInventory") <= 0
print "There are no records in the MasterInventory table."
else
move to first record 1, "MasterInventory"
repeat
CurrentRecord = current record number(1, "MasterInventory")
Name$ = get string field(1, "MasterInventory", "Name")
Quantity = get integer field(1, "MasterInventory", "Quantity")
Damage = get integer field(1, "MasterInventory", "Damage")
RequiredLevel = get integer field(1, "MasterInventory", "RequiredLevel")
print FixedLengthString(Name$, ALIGN_LEFT, 50);
print FixedLengthString(str$(Quantity), ALIGN_RIGHT, 8);
print FixedLengthString(str$(Damage), ALIGN_RIGHT, 10);
print FixedLengthString(str$(RequiredLevel), ALIGN_RIGHT, 8)
` print str$(CurrentRecord) + " - Name: " + Name$ + " Qty: " + str$(Quantity) + " Damage: " + str$(Damage) + " Req. Lvl: " + str$(RequiredLevel)
until move to next record(1, "MasterInventory") = 0
print
endif
print "Press any key to return to the menu."
suspend for key
return
__add_inventory:
clear entry buffer
print
`--------------------------------------------------
` Prompt the user for each piece of data. Apply the
` database changes when done.
`--------------------------------------------------
input "Enter the name of this item: ", Name$
input "Enter the number of items: ", Quantity
input "Enter the damage rating for this item(integer only): ", Damage
input "Enter the required level for this item(integer only): ", RequiredLevel
`---------------------------------
` Add this record to the database.
`---------------------------------
add new record 1, "MasterInventory"
set string field "Name", Name$
set integer field "Quantity", Quantity
set integer field "Damage", Damage
set integer field "RequiredLevel", RequiredLevel
finish adding record 1
apply database changes 1
return
__remove_inventory:
clear entry buffer
return
function FixedLengthString(Value$, Alignment, Length)
result$ = ""
strlen = len(Value$)
if Alignment = ALIGN_LEFT
result$ = Value$
for index = 1 to (Length - strlen)
result$ = result$ + " "
next index
endif
if Alignment = ALIGN_RIGHT
for index = 1 to (Length - strlen)
result$ = result$ + " "
next index
result$ = result$ + Value$
endif
if Alignment = ALIGN_CENTER
spacelen = Length - strlen
half = spacelen / 2
for index = 1 to half
result$ = result$ + " "
next index
result$ = result$ + Value$
for index = 1 to half
result$ = result$ + " "
next index
endif
endfunction result$
Tell me what you guys think!