discord test stars

About

SQLite wrapper in the Mys programming language.

Project: https://github.com/mys-lang/package-sqlite

An example

A basic example that creates a table and then inserts and selects rows.

from sqlite import Database

func main():
    database = Database("my.db")
    database.execute("CREATE TABLE tab(foo, bar, baz)")
    database.execute("INSERT INTO tab VALUES(1, 'one', null)")
    database.execute("INSERT INTO tab VALUES(2,  2.2, 'two')")
    database.execute("INSERT INTO tab VALUES(3,  'three', null)")
    statement = database.prepare("SELECT * FROM tab WHERE foo >= ? ORDER BY foo")
    statement.bind_int(1, 2)

    while statement.fetch():
        print("Row:")
        print("  foo:", statement.column_value_string(0))
        print("  bar:", statement.column_value_string(1))
        print("  baz:", statement.column_value_string(2))

Build and run:

 mys run
 Reading package configuration (0 seconds)
 Building (1.51 seconds)
Row:
  foo: 2
  bar: 2.200000
  baz: "two"
Row:
  foo: 3
  bar: "three"
  baz: null

API

enum Type(u8):
    Value type.

    Integer
    Float
    String
    Null
    Bytes
class SqlError(Error):
    message: string
class Database:

    func __init__(self, path: Path):
        Create or open a database.

    func execute(self, sql: string):
        Execute given statement.

    func prepare(self, sql: string) -> Statement:
        Prepare a statement. Safer than execute(), and faster if used more
        than once.
class Statement:
    database: Database

    func __init__(self, sql: string, database: Database):

    func bind_int(self, column: u32, value: i64):
        Bind given integer to given column.

    func bind_float(self, column: u32, value: f64):
        Bind given float to given column.

    func bind_string(self, column: u32, value: string):
        Bind given string to given column.

    func bind_bytes(self, column: u32, value: bytes):
        Bind given bytes to given column.

    func bind_null(self, column: u32):
        Bind null to given column.

    func execute(self):
        Execute the statement. Bind any values to columns before executing
        it. Calls reset() once complete.

    func fetch(self) -> bool:
        Fetch the next row from the database. Returns True if a row was
        fetched, or calls reset() and returns False when there are no
        more rows available. If fetched, get column values with
        column_*() methods.

    func reset(self):
        Reset the statement so it can be used again.

    func column_type(self, column: u32) -> Type:
        Get the type of given column.

    func column_int(self, column: u32) -> i64:
        Get the value of given column as an integer.

    func column_float(self, column: u32) -> f64:
        Get the value of given column as a float.

    func column_string(self, column: u32) -> string?:
        Get the value of given column as a string.

    func column_bytes(self, column: u32) -> bytes:
        Get the value of given column as bytes.

    func column_value_string(self, column: u32) -> string:
        Get given columns value as a string.