5.3. SqlQuery class

class simplesqlite.sqlquery.SqlQuery[source]

Support class for making SQLite query.

classmethod make_insert(table, insert_tuple, is_insert_many=False)[source]

Make INSERT query.

Parameters:
  • table (str) – Table name of executing the query.
  • insert_tuple (list/tuple) – Insertion data.
  • is_insert_many (bool) – Make query that insert multiple data at once, if the value is True.
Returns:

Query of SQLite.

Return type:

str

Raises:
  • ValueError – If insert_tuple is empty list/tuple.
  • ValueError – If the name is invalid for a SQLite table name.
classmethod make_select(select, table, where=None, extra=None)[source]

Make SELECT query.

Parameters:
  • select (str) – 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.
Returns:

Query of SQLite.

Return type:

str

Raises:
  • ValueErrorselect is empty string.
  • ValueError – If the name is invalid for a SQLite table name.
Examples:
>>> from simplesqlite.sqlquery import SqlQuery
>>> SqlQuery.make_select(select="value", table="example")
'SELECT value FROM example'
>>> SqlQuery.make_select(select="value", table="example", where=SqlQuery.make_where("key", 1))
'SELECT value FROM example WHERE key = 1'
>>> SqlQuery.make_select(select="value", table="example", where=SqlQuery.make_where("key", 1), extra="ORDER BY value")
'SELECT value FROM example WHERE key = 1 ORDER BY value'
classmethod make_update(table, set_query, where=None)[source]

Make UPDATE query.

Parameters:
  • table (str) – Table name of executing the query.
  • set_query (str) – SET part of the UPDATE query.
  • where (str) – Add a WHERE clause to execute query, if the value is not None.
Returns:

Query of SQLite.

Return type:

str

Raises:
  • ValueError – If set_query is empty string.
  • ValueError – If the name is invalid for a SQLite table name.
classmethod make_where(key, value, operation='=')[source]

Make part of WHERE query.

Parameters:
  • key (str) – Attribute name of the key.
  • value (str) – Value of the right hand side associated with the key.
  • operation (str) – Operator of WHERE query.
Returns:

Part of WHERE query of SQLite.

Return type:

str

Raises:

ValueError – If operation is invalid operator. Valid operators are as follows: "=", "==", "!=", "<>", ">", ">=", "<", "<="

Examples:
>>> from simplesqlite.sqlquery import SqlQuery
>>> SqlQuery.make_where("key", "hoge")
"key = 'hoge'"
>>> SqlQuery.make_where("value", 1, operation=">")
'value > 1'
classmethod make_where_in(key, value_list)[source]

Make part of WHERE IN query.

Parameters:
  • key (str) – Attribute name of the key.
  • value_list (str) – List of values that the right hand side associated with the key.
Returns:

Part of WHERE query of SQLite.

Return type:

str

Examples:
>>> from simplesqlite.sqlquery import SqlQuery
>>> SqlQuery.make_where_in("key", ["hoge", "foo", "bar"])
"key IN ('hoge', 'foo', 'bar')"
classmethod make_where_not_in(key, value_list)[source]

Make part of WHERE NOT IN query.

Parameters:
  • key (str) – Attribute name of the key.
  • value_list (str) – List of values that the right hand side associated with the key.
Returns:

Part of WHERE query of SQLite.

Return type:

str

Examples:
>>> from simplesqlite.sqlquery import SqlQuery
>>> SqlQuery.make_where_not_in("key", ["hoge", "foo", "bar"])
"key NOT IN ('hoge', 'foo', 'bar')"
classmethod sanitize(query_item)[source]

Sanitize SQLite query with an empty char.

Parameters:

query_item (str) – String to be sanitized.

Returns:

String that exclude invalid chars. Invalid operators are as follows: "%", "/", "(", ")", "[", "]", "<", ">", ".", ":", ";", "'", "!", "", "#", "-", "+", "=", "\n", "\r"

Return type:

str

Examples:
>>> from simplesqlite.sqlquery import SqlQuery
>>> SqlQuery.sanitize("k<e:y")
'key'
classmethod to_attr_str(name, operation_query='')[source]
Parameters:
  • name (str) – Attribute name.
  • operation_query (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.sqlquery import SqlQuery
>>> SqlQuery.to_attr_str("key")
'key'
>>> SqlQuery.to_attr_str("a+b")
'[a+b]'
>>> SqlQuery.to_attr_str("key", operation_query="SUM")
'SUM(key)'
classmethod to_attr_str_list(name_list, operation_query='')[source]
Parameters:
  • name_list (list/tuple) – List of attribute names.
  • operation_query (str) – Used as a SQLite function if the value is not empty.
Returns:

List of strings that suitable for attribute names of a SQLite query.

Return type:

list/itertools.imap

Examples:
>>> from simplesqlite.sqlquery import SqlQuery
>>> list(SqlQuery.to_attr_str_list(["key", "a+b"]))
['key', '[a+b]']
>>> SqlQuery.to_attr_str_list(["key", "a+b"], operation_query="AVG")
['AVG(key)', 'AVG([a+b])']

See also

to_attr_str()

classmethod to_table_str(name)[source]
Parameters:

name (str) – Table name.

Returns:

String that suitable for table name of a SQLite query.

Return type:

str

Examples:
>>> from simplesqlite.sqlquery import SqlQuery
>>> SqlQuery.to_table_str("length")
'length'
>>> SqlQuery.to_table_str("length(cm)")
'[length(cm)]'
>>> SqlQuery.to_table_str("string length")
"'string length'"
classmethod to_value_str(value)[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.sqlquery import SqlQuery
>>> SqlQuery.to_value_str(1.2)
'1.2'
>>> SqlQuery.to_value_str("value")
"'value'"
>>> SqlQuery.to_value_str(None)
'NULL'
classmethod to_value_str_list(value_list)[source]
Parameters:

value_list (list) – List of values associated with a key.

Returns:

List of values that executed to_value_str method for each item.

Return type:

itertools.imap

Examples:
>>> from simplesqlite.sqlquery import SqlQuery
>>> list(SqlQuery.to_value_str_list([1, "a", None]))
['1', "'a'", 'NULL']

See also

to_value_str()