5.2. Query classes

class simplesqlite.query.Table(value: str)[source]
Parameters

name (str) – Table name.

Returns

String that suitable for table name of a SQLite query.

Examples
>>> from simplesqlite.query import Table
>>> Table("length")
'length'
>>> Table("length(cm)")
'[length(cm)]'
>>> Table("string length")
"'string length'"
to_query()str[source]
class simplesqlite.query.Attr(name: str, operation: str = '')[source]
Parameters
  • name (str) – Attribute name.

  • operation (str) – Used as a SQLite function if the value is not empty.

Returns

String that suitable for attribute name of a SQLite query.

Return type

str

Examples
>>> from simplesqlite.query import Attr
>>> Attr("key")
'key'
>>> Attr("a+b")
'[a+b]'
>>> Attr("key", operation="SUM")
'SUM(key)'
classmethod sanitize(name: str)str[source]
to_query()str[source]
class simplesqlite.query.AttrList(names: Sequence[str], operation: str = '')[source]
Parameters
  • names (list/tuple) – Attribute names.

  • operation (str) – Used as a SQLite function if the value is not empty.

Examples
>>> from simplesqlite.query import AttrList
>>> AttrList(["key", "a+b"]))
['key', '[a+b]']
>>> AttrList(["key", "a+b"], operation="AVG")
['AVG(key)', 'AVG([a+b])']

See also

Attr

append(item: Union[str, simplesqlite.query.Attr])None[source]

Append object to the end of the list.

classmethod sanitize(names: Sequence[str])List[str][source]
to_query()str[source]
class simplesqlite.query.Value(value: Any)[source]
Parameters

value (str) – Value associated with a key.

Returns

String that suitable for a value of a key. Return "NULL" if the value is None.

Return type

str

Examples
>>> from simplesqlite.query import Value
>>> Value(1.2)
'1.2'
>>> Value("value")
"'value'"
>>> Value(None)
'NULL'
to_query()str[source]
class simplesqlite.query.Where(key: str, value: Any, cmp_operator: str = '=')[source]

WHERE query clause.

Parameters
  • key (str) – Attribute name of the key.

  • value – Value of the right hand side associated with the key.

  • cmp_operator (str) – Comparison operator of WHERE query.

Raises

simplesqlite.SqlSyntaxError – If a) cmp_operator is invalid operator. Valid operators are as follows: "=", "==", "!=", "<>", ">", ">=", "<", "<=". b) the value is None and the cmp_operator is not "="/"!=".

Examples
>>> from simplesqlite.query import Where
>>> Where("key", "hoge")
"key = 'hoge'"
>>> Where("value", 1, cmp_operator=">")
'value > 1'
property key: str
to_query()str[source]
property value: str
class simplesqlite.query.Select(select: Union[str, simplesqlite.query.AttrList], table: str, where: Optional[Union[str, simplesqlite.query.Where, simplesqlite.query.And, simplesqlite.query.Or]] = None, extra: Optional[str] = None)[source]

SELECT query clause.

Parameters
  • select – Attribute for SELECT query.

  • table (str) – Table name of executing the query.

  • where (str) – Add a WHERE clause to execute query, if the value is not None.

  • extra (extra) – Add additional clause to execute query, if the value is not None.

Raises
  • ValueErrorselect is empty string.

  • simplesqlite.NameValidationError – If the name is invalid for a SQLite table name.

Examples
>>> from simplesqlite.query import Select, Where
>>> Select(select="value", table="example")
'SELECT value FROM example'
>>> Select(select="value", table="example", where=Where("key", 1))
'SELECT value FROM example WHERE key = 1'
>>> Select(select="value", table="example", where=Where("key", 1), extra="ORDER BY value")
'SELECT value FROM example WHERE key = 1 ORDER BY value'
to_query()str[source]
class simplesqlite.query.And(where_list: List)[source]

AND query clause.

Parameters

where_list (list of str/Where/And/Or) – Query items that concatenating with AND.

to_query()str[source]
class simplesqlite.query.Or(where_list: List)[source]

OR query clause.

Parameters

where_list (list of str/Where/And/Or) – Query items that concatenating with OR.

to_query()str[source]