Library Reference
Database
A convenient frontend for a sqlite3 database, with a few bells and whistles to save code.
If you have a Database called 'db', you can generally access any table in it via 'db.TABLE_NAME'. If the table name conflicts with an existing property or method, you can instead use 'db[TABLE_NAME]' as a fallback.
Source code in hissdb/db.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
__delitem__(item)
Remove a Table from the database
Source code in hissdb/db.py
99 100 101 102 |
|
__enter__()
Context manager to handle connections to the database. If self._autocommit is True, then any changes are automatically committed at the close of the the 'with' statement.
Source code in hissdb/db.py
134 135 136 137 138 139 140 141 |
|
__init__(path, autoconnect=True, autocommit=True, verbose=False)
Database constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
the file path for a new or existing database, or ':memory:' for an in-memory database with no corresponding file |
required |
autoconnect |
bool
|
whether to autoconnect to the database whenever a query requires it. If this is False, you must manually run the connect() method or keep all database operations inside a 'with' statement. |
True
|
autocommit |
bool
|
whether to write all changes to the database at the close of a 'with' statement. Note that even if this is True, you must manually run the commit() method if you make changes outside of a context manager. |
True
|
verbose |
bool
|
whether to print each SQL statement to the console as it is executed |
False
|
Source code in hissdb/db.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
__setattr__(attr, value)
If value is a Table object, do CREATE TABLE. Otherwise, do normal setattr behavior.
Source code in hissdb/db.py
69 70 71 72 73 74 75 76 77 78 |
|
commit()
Save recent changes to the database
Source code in hissdb/db.py
223 224 225 |
|
connect(reuse_existing=True)
Connect to the database. Returns a sqlite3 connection object, but you should not need to use it.
Source code in hissdb/db.py
211 212 213 214 215 216 217 218 219 220 |
|
create_table(name, columns={}, foreign_keys={}, primary_key=(), if_not_exist=False, **kwargs)
Add a table to the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
the name of the new table |
required |
columns |
dict[str, str]
|
a dict where each key is the name of a column, and each value contains the column constraints, e.g. {'id': 'INTEGER PRIMARY KEY', 'title': 'TEXT NOT NULL'}. |
{}
|
foreign_keys |
dict[str, str]
|
a dict where each key is the name of a column in this table, and each value represents a column in another table. Values can be specified as Column objects or as strings like 'users(id)' or 'users.id'. |
{}
|
primary_key |
tuple[str]
|
optional tuple with the names of multiple columns that should be combined to form the primary key. |
()
|
Source code in hissdb/db.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
|
disconnect(commit='AUTO')
Close the connection to the database. If commit is False, roll back any changes. If commit is True, commit them.
If commit is 'AUTO', only commit if self._autocommit is True, but don't rollback either way.
Source code in hissdb/db.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
|
drop_table(name)
Delete the given table and its contents. Same as 'del self[name]'
Source code in hissdb/db.py
181 182 183 184 185 186 |
|
execute(statement, placeholders={}, many=False)
Feed the given statement and placeholders to the execute() or executemany() method of this database's SQLite3 connection. If self._verbose is True, also print the executed statement.
Source code in hissdb/db.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
rollback()
Undo all changes since the last commit
Source code in hissdb/db.py
228 229 230 |
|
Table
Source code in hissdb/table.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
|
_schema()
property
cached
If this table is in a database, get the schema from there. Otherwise, generate a schema from the list of columns, etc.
Source code in hissdb/table.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
count(where=None, **kwargs)
Get the number of rows in the table, optionally restricted to those that meet the given criteria.
Source code in hissdb/table.py
141 142 143 144 145 146 147 148 149 150 |
|
delete(where=None, **kwargs)
Make and execute a Delete statement from this table, and return the number of rows deleted.
Source code in hissdb/table.py
214 215 216 217 218 219 220 221 222 223 |
|
fetchall(cols='*', where=None, **kwargs)
Make and execute a Select statement from this table, and return a list of all results.
Source code in hissdb/table.py
161 162 163 164 165 166 |
|
fetchone(cols='*', where=None, **kwargs)
Make and execute a Select statement from this table, and return the first result.
Source code in hissdb/table.py
153 154 155 156 157 158 |
|
insert(row={}, **kwargs)
Make and execute an Insert statement into this table, and return the index of the of the new row.
Source code in hissdb/table.py
169 170 171 172 173 174 |
|
insertmany(cols, rows, or_=None, **kwargs)
Make and execute an InsertMany statement, and return the number of rows added.
Source code in hissdb/table.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
|
select(cols='*', where=None, **kwargs)
Make and return a Select statement starting from this table.
Source code in hissdb/table.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
|
update(updates={}, where=None, **kwargs)
Make and execute an Update statement from this table, and return the number of rows modified.
Source code in hissdb/table.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
|
Column
Bases: Expression
A Column is a reference to a column in a SQLite database. Because it is also an Expression, many of its logical operators are overridden
so that you can build SQL Statements via Python logic like this
john_does = db.people.select(where= db.people.first_name == 'John' & db.people.last_name == 'Doe' )
For more information on this, see the Expression documentation.
Attributes:
Name | Type | Description |
---|---|---|
cid |
column index number |
|
name |
name of the column |
|
type |
string containing the SQL datatype of this column |
|
notnull |
int representing whether the column disallows null vals |
|
dflt_value |
the column's default value |
|
pk |
int representing whether the column is a primary key |
Source code in hissdb/column.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
__init__(constraints=None, table=None, name=None)
Column object constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
constraints |
str
|
a SQL expression defining this column, like 'TEXT NOT NULL' or 'INTEGER PRIMARY KEY' |
None
|
table |
the table containing this column. If not provided, it will be set when the column is assigned to a table with Table.setattr or Table.setitem. |
None
|
|
name |
str
|
the name of this column. If not provided, it will be set when this column is assigned to a table. |
None
|
Source code in hissdb/column.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
fetchall(where=None, **kwargs)
Convenience method to execute a Select statement targeting only this column, and return a list of the resulting values (rather than a list of one-item tuples).
Source code in hissdb/column.py
78 79 80 81 82 83 84 85 |
|
fetchone(where=None, **kwargs)
Convenience method to execute a Select statement targeting only this column, and return the single resulting value (rather than a tuple with one item in it).
Source code in hissdb/column.py
69 70 71 72 73 74 75 76 |
|
select(where=None, **kwargs)
Convenience method to make a Select statement targeting only this column.
Source code in hissdb/column.py
58 59 60 61 62 63 64 65 66 67 |
|
update(new_value, where=None, **kwargs)
Convenience method to execute an Update statement setting the value of this column, and return the number of rows modified.
Source code in hissdb/column.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
Expressions
Expressions are the building blocks of Statements. They provide two key features: First, they parameterize input values to avoid SQL injection; and second, they provide a set of methods and overloaded logical operators (e.g. '==', '&', or '+') that allow users to build compound Expressions with Python syntax.
Attributes:
Name | Type | Description |
---|---|---|
tokens |
the results of parameterizing each of the provided args. Any item that is not an Expression, Table, or Column object will be converted to a placeholder unless it is in the _literals whitelist. |
|
placeholders |
a dictionary of parameters that would need to be provided to sqlite3.execute() if this expression were a Statement of its own |
|
necessary_tables |
a list of Tables this expression references |
Source code in hissdb/expression.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
|
__abs__()
SQLite ABS() function
Source code in hissdb/expression.py
266 267 268 |
|
__add__(other)
Add two numbers or concatenate two strings
Source code in hissdb/expression.py
238 239 240 241 242 243 244 245 246 247 248 |
|
__init__(*args, func=None, prefix=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
the list of words or other values, in order, in the expression. Each one will be converted to a placeholder unless it is an Expression, Column, or Table object or is otherwise in the _literals whitelist. When the expression is converted to a string, the args (or their placeholders) will be joined with spaces. |
required | |
func |
str
|
the SQL function, if any, that the expression implements. If a func is provided, then the rendered expression will be enclosed in parentheses, preceded by the func string, and each arg will be separated by commas instead of spaces, to make them into function arguments rather than just words. |
None
|
prefix |
str
|
only relevant if func is provided. The prefix will be rendered just after the opening parentheses, before the first arg, with no separating comma. This is meant to allow for syntax like the "DISTINCT" argument for aggregate functions. |
None
|
Source code in hissdb/expression.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
__invert__()
Return an expression that is True if and only if this one is not True. This works by replacing operators with their inverses (e.g. replacing '>' with '<=').
When the expression to be inverted contains two sub-expressions joined with AND or OR, the sub-expressions are both inverted, and the AND is replaced with OR, or vice versa.
Source code in hissdb/expression.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
|
__mod__(other)
LIKE operator for strings, modulo operator otherwise
Source code in hissdb/expression.py
259 260 261 262 263 264 |
|
__str__()
Text of the expression that will be inserted into a SQL statement, with strings and integers replaced with placeholders unless they are listed in Expression._literals
Source code in hissdb/expression.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
_db()
property
cached
Find the Database that this expression relates to
Source code in hissdb/expression.py
125 126 127 128 129 130 |
|
avg(distinct=False)
SQLite AVG() function
Source code in hissdb/expression.py
280 281 282 283 284 |
|
ceil()
SQLite CEIL() function
Source code in hissdb/expression.py
290 291 292 |
|
count(distinct=False)
SQLite COUNT() function
Source code in hissdb/expression.py
316 317 318 319 |
|
desc()
property
Shortcut for use in ORDER BY clauses
Source code in hissdb/expression.py
380 381 382 383 |
|
exists()
SQLite EXISTS() function
Source code in hissdb/expression.py
321 322 323 |
|
exp()
SQLite EXP() function
Source code in hissdb/expression.py
306 307 308 |
|
floor()
SQLite FLOOR() function
Source code in hissdb/expression.py
294 295 296 |
|
ln()
SQLite LN() function
Source code in hissdb/expression.py
298 299 300 |
|
max(distinct=False)
SQLite MAX() function
Source code in hissdb/expression.py
270 271 272 273 274 |
|
min(distinct=False)
SQLite MIN() function
Source code in hissdb/expression.py
276 277 278 |
|
pow(exponent)
SQLite POWER() function
Source code in hissdb/expression.py
310 311 312 |
|
render()
Text of the expression with placeholders filled in
Source code in hissdb/expression.py
132 133 134 135 136 137 138 139 140 141 |
|
round()
SQLite ROUND() function
Source code in hissdb/expression.py
286 287 288 |
|
sqrt()
SQLite SQRT() function
Source code in hissdb/expression.py
302 303 304 |
|
BaseStatement
Bases: Expression
base class that all Statements inherit from
Source code in hissdb/statements.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
__init__(table, where=None, join={}, order_by=None, limit=None, offset=None, autojoin=True, **kwargs)
BaseStatement class constructor. There should be no need for a user to use this directly rather than a subclass, but all subclasses accept the following arguments in their constructors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table |
the Table object this statement relates to |
required | |
where |
Expression
|
an Expression object representing a WHERE clause in SQL. |
None
|
join |
dict
|
a dictionary where each key is a Column object to join on the condition that the corresponding Expression is met. |
{}
|
order_by |
tuple[Expression]
|
a Column, an Expression object representing a virtual column, or a tuple containing multiple of either of those things. To sort descending, you can use the 'desc' property of any Column or Expression, e.g. "order_by=db.users.first_name.desc" |
None
|
limit |
int
|
an int or an Expression representing an int, setting the maximum number of rows to select or modify |
None
|
offset |
int
|
an int or an Expression representing an int, which sets which row to on. |
None
|
autojoin |
bool
|
whether HissDB should use the database's foreign key relationships to automatically join any tables that the statement requires. Defaults to True. |
True
|
Source code in hissdb/statements.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
Insert
Bases: BaseStatement
SQL statement to insert a single row into a table
Source code in hissdb/statements.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
__init__(table, row={}, or_=None, **kwargs)
Insert statement constructor. Any unknown keyword arguments will be added to the row dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table |
Table object to insert the row into |
required | |
row |
dict
|
dict of values representing the row to insert |
{}
|
or_ |
str
|
what to do when the insert statement fails due to a table constraint. Options are 'ABORT', 'FAIL', 'IGNORE', 'REPLACE', and 'ROLLBACK'. |
None
|
Source code in hissdb/statements.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
Select
Bases: BaseStatement
SQL statement to return some or all rows meeting given criteria
Source code in hissdb/statements.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
__init__(table, cols='*', where=None, group_by=None, having=None, **kwargs)
Select statement constructor.
Any unknown keyword arguments are interpreted as WHERE conditions constraining the value of a column in the given table. For instance, 'first_name="Jerry"' is equivalent to 'where=[table].first_name == "Jerry"'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table |
the Table object from which to select values |
required | |
cols |
list[Column]
|
list of Column objects, or Expressions representing virtual columns, that the statement should select. For ease of use, columns can also be referenced via names rather than Column objects. |
'*'
|
where |
Expression
|
Expression constraining which rows to select |
None
|
group_by |
list[Column]
|
equivalent to SQL 'GROUP BY' clause |
None
|
having |
Expression
|
equivalent to SQL 'HAVING' clause |
None
|
Source code in hissdb/statements.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
Update
Delete
Bases: BaseStatement
SQL statement to delete some or all rows meeting given criteria
Source code in hissdb/statements.py
266 267 268 269 270 271 272 |
|
InsertMany
Bases: BaseStatement
SQL statement to efficiently insert a list or generator of rows
Source code in hissdb/statements.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
|
__init__(table, cols, rows, or_=None, **kwargs)
InsertMany statement constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cols |
tuple[Column]
|
tuple of Column objects (or strings representing them) corresponding to the columns for which values will be provided. |
required |
rows |
list[tuple]
|
list or generator containing each row to insert. A row is a tuple whose values each represent the corresponding value in cols. |
required |
or_ |
str
|
what to do when the insert statement fails due to a table constraint. Options are 'ABORT', 'FAIL', 'IGNORE', 'REPLACE', and 'ROLLBACK'. |
None
|
Source code in hissdb/statements.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
|