Here is a new Class that creates/handles data files. It is an evolution of the db code module but should be much easier to use and learn. It is still in Beta and I would love to have any feedback. I have attached the Class code and have also attached the Class with Demo code.
Requires B4A 2.0+ and Reflection 2.2 Library
db Class Module
Commands
Requires B4A 2.0+ and Reflection 2.2 Library
db Class Module
Commands
- FileName_ As String
Returns the filename in the current work area - FilePath_ As String
Returns the path of the current work area - Record_Count As Int
Returns the number of records for the database in the current work area - Record_Pointer As Int
Returns the record pointer (current record) for the database in the current work area - Set_Pointer (m_Pointer As Int)
Used to set the record pointer of the database in the current work area. The range must be between
0 and the RecordCount -1 - AddRecord
Adds a record to the current DataBase in the current work area, sorts the entry based on the
data entered and sets the DataBase Pointer to the newly added record. - FindRecord (SearchStr As String) As Boolean
Used to find a match to SearchStr. Searches every field in each record
of the database and returns the first record that any Fields data matches the
SearchStr. The more data entered the more likely the desired record will be returned
This search IS NOT case sensitive and returns True if a match was found
Example:
If FindRecord("Billy J. Jones") Then - ResetPointers
Set all pointers for all databases in all work areas back the the last known
value. Use in Activity_Resume to return back to known location after Activity_Pause
or after orientation change. NOTE: These pointers are saved automatically, you do not
need to try and save the pointers in Activity_Pause
db.ResetPointers - GetRecord
Loads the record at the Pointer position into memory then
calls back to the calling activity to the Sub GetFields_X,
where X is the workarea number 0-9. You should have a
GetFields_X sub in each of your Activities that makes a
call to this db class. No Exception is thrown if the
GetFields_X sub is missing. The GetFields_X sub is used
to populate your EditText.Text fields. This helps automate
you code. Example:
Sub GetFields_0 'Put contents of database fields into EditText fields
name.Text = db.GetField("name")
address.Text = db.GetField("address")
city.Text = db.GetField("city")
state.Text = db.GetField("state")
zip.Text = db.GetField("zip")
Phone.Text = db.GetField("phone")
End Sub - GetField (mfieldname As String) As String
Returns the value of the database field named mfieldname
EditTextAddress.Text = GetField("address") - PutField (mfieldname As String, varvalue As String)
Writes any passed value to the database field memory variable named mfieldname.
Does not write to disk. See AddRecord or Update record for writing the record
to the disk.
PutField("address", "8929 West Way Circle")
or
PutField("address", EditTextAddress.Text) - DateNew (mDate As String, HowManyDays As Int) As String
Returns a new date from the date passed plus or minus HowManyDays. Pass mDate as String,
HowManyDays As Int. HowManyDays can be positive or negitive numbers.
DateDue = DateNew("02/04/2012", 90) - DateNOD (CurrentDate As String, OtherDate As String) As Int
Returns the number of days that have passed between two dates.
Pass the dates as a String - Date ( As ) As String
Returns the systems date as a String - ActiveFields As Int
Returns the current number of data fields in the current work area
Returns number as Int. This object is Readonly - GetStructure ( As )
Displays the structure of the database in the active work area - GetStructure2 (Work_Area As Int)
Same as GetStructure but accepts the work area as an Int. This will display the
structure of any database in any workarea even if it is not the active database. - GenerateRecords (Howmany As )
Use this for testing to populate the database with random data
Accepts a number 1 to 10000 as Int. Populates the database in
the current workarea. - UpdateRecord ( As )
Updates the record at pointer position in the current workarea with data
stored in the memory fields. See db.PutFields() to place data into their
memory location. - DeleteRecord
Delete the current record at Pointer position in the current work area.
A confirmation box will be displayed, if user selects Yes, record will
be deleted. The next record following will become the current record. - FindExact (SearchStr As String)
Find an Exact match to the SearchStr passed. Not case sensitive. - FirstRecord
Moves Pointer to the first record in the DataBase and reads the record into memory - LastRecord
Moves Pointer to the last record in the DataBase and reads the record into memory - PreviousRecord
Moves Pointer to the previous record in the DataBase and reads the record into memory
BOF is handled and will show a Toast Message if BOF is reached - NextRecord
Moves Pointer to the next record in the DataBase and reads the record into memory
EOF is handled and will show a Toast Message if EOF is reached - SearchDate (CurrentDate As String, CompareToDate As String, DateRange As Int) As Boolean
Returns True if the CurrentDate and the CompareToDate are within X number of days(DateRange)
of each other. Otherwise False will be returned. - ListRecords (ListTitle As String, dbFields() As String)
Returns a list with your selected fields in an InputList.
If user selects an entry, it reads the record in to memory
and returns the number of the record selected and sets the
record pointer to this record.
db.ListRecords("Contact Listing", Array As String("name", "address", "phone", "zip") - PullList (dbFields() As String, Filter As String) As List
Returns a list with the select fields from the database. Pass the field names as
a string. Returns the listing as a List variable. If no Filter is needed pass
"". If a filter is needed pass Text to match:
db.PullList(Array As String("name", "zip", "phone", "37421"))
Attachments
Last edited: