I just wrote a simple Database API I needed for a project.
It can store any sort of Data and can be used in a simple way. Every function is documented and I compare the Syntax with the MySQL Syntax, so you know how to use them.
But the best thing is, that you can use basic MySQL / SQL queries to operate the API. (link to the documentation / limitations site)
Planned features:
- Improve the MySQL interpreter to support more complex queries
Pastebin: http://pastebin.com/dBHuAvka
CCMyAdmin is a tool similar to PHPmyAdmin. It allows you to easily manage your databases and edit data.
[attachment=1835:ccdb.png]
Download: http://newcat.bplaced.net/CCDB.zip
How to use:
Click the "Open Database" button, then select the "Databases" folder in your minecraft/saves/computer/{computerID}. In the following dialog select a database or create a new one.
After that you can select the table with the tabs on the top, select the ID/row with the listbox in the left and then edit the data with the three buttons above the table view.
Functions of the API:
Spoiler
This is just a list of currently implemented functions. A detailed documentation can be found here: http://computercraftdb.wikispaces.com/- dbExists
- tableExists
- createDatabase
- deleteDatabase
- createTable
- renameTable
- deleteTable
- getTotalEntries
- set
- get
- delete
- getIdsByValue
- getNextFreeID
- splitString
- query
- sqlSELECT
- sqlUPDATE
- sqlINSERT
- sqlDELETE
- sqlCREATE
- sqlRENAME
- sqlDROP
Beginner's Guide / Example Program
Spoiler
--Load the API if not done yet
os.unloadAPI("db")
os.loadAPI("db")
--First of all we need a database in which we want to write, so we check if there is already
--a database called "warehouse" and if not, we create a new one
if (not db.dbExists("warehouse")) then
db.createDatabase("warehouse")
end
--Then we create a table called "items". We also check if it is already existing,
--because we will get an error else
--Currently you have to specify the database you want to modify in almost all
--methods, this will be removed in a later version and replaced through
--db.selectDatabase(), like in PHP/MySQL
if (not db.tableExists("warehouse", "items")) then
db.createTable("warehouse", "items")
end
--Next we want to feed our table with some data.
--The syntax of this very long set commmand is quite simple:
--1st parameter: The database you want to modify (in our case "warehouse")
--2nd parameter: The table you want to modify (in our case "items")
--3rd parameter: The row ID, which will in our case be the Minecraft-ID of the item
--4th parameter: The key (column) which should be modified, in our case for every item the count column and the name column
--5th parameter: The (new) value of the key, in our case for every item the count and the name
--Our first item will be stone
db.set("warehouse", "items", 1, "name", "Stone")
db.set("warehouse", "items", 1, "count", "64")
--The second item will be sand
db.set("warehouse", "items", 12, "name", "Sand")
db.set("warehouse", "items", 12, "count", "12")
--And our last item is going to be diamonds *-*
db.set("warehouse", "items", 264, "name", "Diamond")
db.set("warehouse", "items", 264, "count", "1")
--Also we want to create an index so we know which item ID's we
--currently have stored.
--Since there is no item in minecraft with the ID "0" (except air,
--but in my opionion this is no item), we store the index there
--As index I use an serialized lua-table
index = {1, 12, 264}
db.set("warehouse", "items", 0, "index", textutils.serialize(index))
--All of our data is now written into the database.
--Now it is time to read it and present it to the user
--Let's start. First of all we need to read our index file
--Reading is done by using db.get()
--The syntax is the same as db.set(), except the last parameter
--So for the index file we use this:
index = textutils.unserialize(db.get("warehouse", "items", 0, "index"))
--Now we can read all the other items by using a for-loop, since we have the
--index table.
for i = 1, #index do
print(db.get("warehouse", "items", index[i], "name") .. ": " .. db.get("warehouse", "items", index[i], "count"))
end
--The program will output:
--Stone: 64
--Sand: 12
--Diamond: 1
--Using the index makes the program very flexible and expandable,
--but it is also possible to hardcode the ID's.
Changelog
Spoiler
v0.9+ Initial Release
v1.0
+ Added all MySQL related functions:
- query()
- sqlSELECT()
- sqlUPDATE()
- sqlINSERT()
- sqlDELETE()
- sqlCREATE()
- sqlRENAME()
- sqlDROP()
- splitString()
v1.01
* Bugfix