Hi, I have been working on a file based, standalone database plugin for darkbasic pro over the past few days. I have made enough progress to write a simple demonstration program which I will attach to this post. Please look at the source code and see if you can give me any ideas or feedback.
result=open database("bank.db")
`If result is 0 then opening the database failed, otherwise 1 is returned
if result=0 then end
`The database is now open. We are going to write a program to keep an account statement.
`We need to create a table in the database for this so we use an SQL query. We shall call
`database 'statement'. First check if the table exists
if table exist("statement")=0
`The table does not yet exist. Lets create it
query$="CREATE TABLE statement (key INTEGER PRIMARY KEY,date TEXT,amount double,description TEXT)"
result=execute query(query$)
if result=-1 then print get error()
if result=0 then print "Table created"
endif
REMSTART
If the query above looks complicated, we can break it down.
First of all is "CREATE TABLE statement" which means the query is going to create a table
called statement. Following this in brackets is the list of fields that will be in
the statement table. Each one has a name and a type. They are as follows:
1. key INTEGER PRIMARY KEY
2. date TEXT
3. amount double
4. description TEXT
INTEGER PRIMARY KEY means that the field is going to be the primary key, and for each
record that is entered into the table, this will go up by 1. This will make more sense later
date is a text field, which will simply hold the date of the transaction.
amount is a double, which is going to hold the sum of money from the transaction
description is a text field which is going to hold details of the transaction
REMEND
`Lets add a transaction to the database. Each time this program runs it will ask you if you
`want to add a new transaction, and then list all the transactions so far
input "Do you want to do a transaction y/n?";tran$
if tran$="y"
input "Please enter an amount. Negative values debit the account: ";amt
input "Please enter a description for this transaction: ";desc$
`Now prepare the query
query$="INSERT INTO statement (date,amount,description) VALUES (CURRENT_DATE," + str$(amt) + ",'" + desc$ + "')"
result=execute query(query$)
if result=-1 then print get error()
if result=0 then print "Record successfully added"
endif
`Now we must display all records in the account
`We use a select query for this
query$="SELECT * FROM statement"
resource=execute query(query$)
if resource=-1 then print get error():wait key:end
`The resource returned from a select statement holds the records returned from the database
`We must now fetch these values and display them
for t=1 to count rows(resource)
key$=SQL RESULT(resource,t,"key")
date$=SQL RESULT(resource,t,"date")
amount$=SQL RESULT(resource,t,"amount")
description$=SQL RESULT(resource,t,"description")
print key$ ; ". "; date$ ; " £" ;amount$ ;" (" ; description$ ;")"
next t
print "Thank you..."
wait key
I have aimed for simplicity in accessing the database. Anybody who uses php and mysql will probably feel familiar here.
WE SHALL BECOME ALL POWERFUL! CRUSH THE LESSER RACES! CONQUER THE GALAXY! UNIMAGINABLE POWER! UNLIMITED RICE PUDDING ! ! ! ETC. ! ! ! ETC.! ! !