lua-dbase: a dBase-format (dbf) driver for lua

(c) 2009-19 Alacner zhang <alacner@gmail.com>
* This content is released under the MIT License.

Table of Contents

Introduction

These functions allow you to access records stored in dBase-format (dbf) databases. You can download lua-dbase from gitand report bug for me and i'll fix it as quickly as i can.

dBase files are simple sequential files of fixed length records. Records are appended to the end of the file and delete records are kept until you call dbf:pack().

The types of dBase fields available are:

Available types of fields
Field dBase Type Format Additional information
M Memo n/a This type is not supported, such field will be ignored
D Date YYYYMMDD The field length is limited to 8
N Number A number You must declare a length and a precision (the number of digits after the decimal point)
C String A string You must declare a length. When retrieving data, the string will be right-padded with spaces to fit the declared length.
L Boolean T or Y for TRUE, F or N for FALSE Stored and returned as an integer (1 or 0)
F Float A float number  

after download,you can install like this.
tar xzvf lua-dbase.*
cd lua-dbase.*
make
make install

Functions

require "dbase"

Public objects

create the dbase link handle.

dbase.version()

dbase.version()
return the lua-dbase version info. just like:
luadbase (1.0.0) - Dbase-format(dbf) driver
(c) 2009-19 Alacner zhang <alacner@gmail.com>
This content is released under the MIT License.

dbase.open(filename, mode)

opens a dBase database with the given access mode.
local db, err = pgsql.open('/tmp/tmp.dbf', 2)
mode: An integer which correspond to those for the open() system call (Typically 0 means read-only, 1 means write-only, and 2 means read and write).

dbase.create(filename, fields)

creates a dBase database with the given definition.
local def = {
{"date", "d"},
{"name", "C", 50},
{"age", "N", 3, 0},
{"email", "C", 128},
{"ismember", "L"},
}
local dbh,a,b,c = dbase.create('/tmp/base/test.dbf', def)
dbh:add_record{os.time(), "alacner", 18, 'alacner@gmail.com', 1}
dbh:add_record{os.time(), "name", 28, 'w@g.j', 0}
fields: An array of arrays, each array describing the format of one field of the database. Each field consists of a name, a character indicating the field type, and optionally, a length, and a precision.

Link objects

the methods to contol the dbase link handle

dbh:add_record(record)

Adds a record to a database
record(table) : An indexed array of data. The number of items must be equal to the number of fields in the database, otherwise dbh:add_record() will fail.

dbh:replace_record(record, record_number)

Replaces the given record in the database with the given data.
record(table) : An indexed array of data. The number of items must be equal to the number of fields in the database, otherwise dbh:add_record() will fail.
record_number(int) : An integer which spans from 1 to the number of records in the database (as returned by dbh:numrecords()).

dbh:get_record(record_number)

Gets a record from a database as an indexed array.

dbh:get_record_with_names(record_number)

Gets a record from a dBase database as an associative array.

dbh:delete_record(record_number)

Marks the given record to be deleted from the database.
Note: To actually remove the record from the database, you must also call dbh:pack().

dbh:pack()

Packs the specified database by permanently deleting all records marked for deletion using dbh:delete_record().

dbh:numfields()

Gets the number of fields (columns) in the specified database.

db:numrecords()

Gets the number of records (rows) in the specified database.

db:close()

Closes the given database link identifier.