LibSQL-1.0

Creating your database

The first thing you'll need is a database object. Creating one is very simple:

localdatabase=LibSQL:New()

You'll most likely want to save your database for later use, and if so, you can always reopen your database.
The following code shows how to reopen an old database or create a new if none exists:

localdatabase=mySavedVariables.database-- get our random database objectifdatabasethen-- If we already have a saved database objectLibSQL:Open(database)-- just open it and it'll work fine now.elsedatabase=LibSQL:New()-- make a new databaseend

Setting up database tables

Now that you have a working database object, you'll need to create one or more tables to work with.
In any RDBMS, a table is the equivalent of a giant spreadsheet page, whereas the database would be the book that holds the sheets.

Field types

Each field (column) in your table can be set to a specific data type:

STRING : Your garden variety string data, fx. "Hello world!".

INTEGER : Integers (whole numbers) like 1,2,3,4...

NUMBER : Floating point numbers like 1.25 or 7.39477

BOOLEAN : Boolean values (true or false)

BLOB : A binary string. Unlike the regular STRING fields, this type of string is handled with case-sensitivity.

ENUM : A set of predefined values such as [a,b,c,d]. Only values found in the set are accepted.

Furthermore, you can set some field attributes for each field:

AUTO_INCREMENT : Increments the value of the field by 1 each time a new row is inserted into the table.

DEFAULT (...) : Sets the default value of this field to (...)

While it may seem cumbersome having to set a specific data type for each field, it actually serves a lot of useful purposes:

LibSQL automatically conforms your data to the specific type so you won't have to check if a number is really a number or if a string is nil.

some other reasons goes here...

Creating a table

Now then, after we've fiddled with whatever fields and types of fields we'd like, it's time to create a table!
We do this using the database object we've already created and the Execute() function, which parses and runs
any given command we send to it.

localtbl=db:Execute("CREATEmyTable(nameSTRING,levelINTEGER,idINTEGERAUTO_INCREMENT)")-- We now have a table called myTable inside our database!

Adding rows to your table

Before we can search our table for data, we will, of course, need to add some rows to it.
There are multiple ways of adding one or more rows to a table:

Conventional INSERT statement

The conventional way to insert is by executing an INSERT statement:

-- Add a single row using the default field locations:db:Execute("INSERT INTO myTable VALUES('Abe', 85)")-- Add a row using specified fields:db:Execute("INSERT INTO myTable (level, name) VALUES(85, 'Abe')")

Internal insertions:

A faster (and safer) way to insert data is by using the internal Insert functions:

localmytable=db:Get("myTable")-- Fetch our table as a resource objectmytable:Insert({name="Abe",level=85})-- Insert a single row using associative arraysmytable:Insert({"Abe",85})-- Insert a row using just raw values-- Or, we could insert multiple rows at once:mytable:Insert({"Abe",85},{"Ben",80});

Searching for data

To look for rows that match a given criteria, you can issue one of the following commands:

The SELECT statement tells LibSQL to select specific fields from your rows. The asterisk (*) character tells LibSQL to select all fields in each row, but you could also choose to select any specific field, for instance: