Skip to content

libsqlExecute

Person edited this page Nov 13, 2023 · 8 revisions

This function executes exactly one (1) SQL query.

Definition

async function libsqlExecute(conf: libsqlConfig, stmt: libsqlSQLStatement): Promise<libsqlResult<libsqlStatementResOkData, libsqlError>>

Arguments

Returns

Type

Promise<libsqlResult<libsqlStatementResOkData, libsqlError>>

Reference: libsqlResult, libsqlStatementResOkData, libsqlError

Examples

import { libsqlExecute } from "libsql-stateless";
//or
const { libsqlExecute } = require("libsql-stateless");

const res = await libsqlExecute(conf, {sql: "SELECT * FROM mad_cats;"});
//or
const res = await libsqlExecute(conf, {
    sql: "SELECT madness, power_level FROM mad_cats WHERE cat_id = ? AND owner_name = ?;",
    args: [
        {
            type: "integer",
            value: "89342"
        },
        {
            type: "text",
            value: "John Smith"
        }
    ]
});
//or
const res = await libsqlExecute(conf, {
    sql: "INSERT INTO mad_cats VALUES (:cat_name, :power_level, :madness);", //In SQLite, the names of arguments include the prefix sign (:, @ or $).
    named_args: [
        {
            name: "cat_name",
            value: {
                type: "text",
                value: "Bgorthe, The Destroyer of Worlds"
            }
        },
        {
            name: "power_level",
            value: {
                type: "integer",
                value: "9105"
            }
        },
        {
            name: "madness",
            value: {
                type: "float",
                value: "32.5"
            }
        }
    ]
});


if (res.isOk) {
    console.log(res.val.rows);
    console.log(res.val.rows[0]);
}
Clone this wiki locally