Wednesday, February 07, 2024

SQLite queries

SQLite engine is written in C, and used from many other prog. languages, 
directly or via ORMs. The syntax of queries may vary, depending on language and lib used.

JS async: sqlite3 - npm

sql - Does SQLite3 have prepared statements in Node.js? - Stack Overflow

// Directly in the function arguments.
db.run("UPDATE tbl SET name = ? WHERE id = ?", "bar", 2);

// As an array.
db.run("UPDATE tbl SET name = ? WHERE id = ?", [ "bar", 2 ]);

// As an object with named parameters.
db.run("UPDATE tbl SET name = $name WHERE id = $id", {
  $id: 2,
  $name: "bar"
});

JS sync (faster): better-sqlite3 - npm


C# (.NET) Parameters - Microsoft.Data.Sqlite | Microsoft Learn

Parameters can be prefixed with either :@, or $.

command.CommandText = @" INSERT INTO user (name) VALUES ($name) "; command.Parameters.AddWithValue("$name", name);


Go: Go sqlite3 - working with sqlite3 in Golang

    stm, err := db.Prepare("SELECT * FROM cars WHERE id = ?")
    var id int
    var name string
    var price int
    cid := 3
    err = stm.QueryRow(cid).Scan(&id, &name, &price)

Python: Python 3 sqlite parameterized SQL-query - Stack Overflow

sql = "select exists(SELECT * from USERS where PASSWORD = ? AND USERNAME = ?)"
args = (var1,var2)
cursor = database_connection.execute(sql, args)


No comments: