Getting Started

Get up and running with SIMPLE_SQL in minutes.

1. Add to Your Project

Add SIMPLE_SQL to your ECF file:

<library name="simple_sql" location="$SIMPLE_SQL/simple_sql.ecf"/>
Environment Variable

Set SIMPLE_SQL to point to the library location, or use an absolute path.

2. Open a Database

Create a database connection:

local
    db: SIMPLE_SQL_DATABASE
do
    -- File-based database
    create db.make ("myapp.db")

    -- Or in-memory database
    create db.make_memory

    -- Always close when done
    db.close
end

View make View make_memory

3. Create Tables

Execute DDL statements:

db.execute ("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE, created_at TEXT DEFAULT CURRENT_TIMESTAMP)")

View execute

4. Insert Data

Use the fluent insert builder:

-- Simple insert
db.insert_into ("users")
    .value ("name", "Alice")
    .value ("email", "alice@example.com")
    .execute

-- Get the inserted ID
print ("Inserted user ID: " + db.last_insert_rowid.out)

View insert_into View SIMPLE_SQL_INSERT_BUILDER

5. Query Data

Use the fluent select builder:

local
    result: SIMPLE_SQL_RESULT
do
    result := db.select_from ("users")
        .columns ("id, name, email")
        .where ("name LIKE ?", "A%")
        .order_by ("name")
        .execute

    across result as row loop
        print (row.integer_value ("id").out + ": " + row.string_value ("name") + "%N")
    end
end

View select_from View SIMPLE_SQL_SELECT_BUILDER

6. Update Data

db.update ("users")
    .set ("email", "alice.new@example.com")
    .where ("id = ?", 1)
    .execute

View update View SIMPLE_SQL_UPDATE_BUILDER

7. Delete Data

db.delete_from ("users")
    .where ("id = ?", 1)
    .execute

View delete_from View SIMPLE_SQL_DELETE_BUILDER

8. Use Transactions

Wrap multiple operations in a transaction:

db.begin_transaction
db.insert_into ("users").value ("name", "Bob").execute
db.insert_into ("users").value ("name", "Carol").execute
db.commit

-- Or rollback on error
db.begin_transaction
db.insert_into ("users").value ("name", "Dave").execute
db.rollback  -- Undo the insert

View begin_transaction View commit View rollback

Complete Example

class SIMPLE_SQL_DEMO

create
    make

feature {NONE} -- Initialization

    make
        local
            db: SIMPLE_SQL_DATABASE
            result: SIMPLE_SQL_RESULT
        do
            -- Create database
            create db.make_memory

            -- Create table
            db.execute ("CREATE TABLE tasks (id INTEGER PRIMARY KEY, title TEXT, done INTEGER DEFAULT 0)")

            -- Insert tasks
            db.begin_transaction
            db.insert_into ("tasks").value ("title", "Learn SIMPLE_SQL").execute
            db.insert_into ("tasks").value ("title", "Build something awesome").execute
            db.insert_into ("tasks").value ("title", "Ship it!").execute
            db.commit

            -- Query incomplete tasks
            result := db.select_from ("tasks")
                .columns ("id, title")
                .where ("done = 0")
                .execute

            print ("Pending tasks:%N")
            across result as row loop
                print ("  - " + row.string_value ("title") + "%N")
            end

            -- Mark first task done
            db.update ("tasks").set ("done", 1).where ("id = ?", 1).execute

            db.close
        end

end

Next Steps