Gerhard Häring | eb2e192 | 2006-04-29 23:12:41 +0000 | [diff] [blame] | 1 | \section{\module{sqlite3} --- |
| 2 | DB-API 2.0 interface for SQLite databases} |
| 3 | |
| 4 | \declaremodule{builtin}{sqlite3} |
Fred Drake | 6550f03 | 2006-05-01 06:25:58 +0000 | [diff] [blame] | 5 | \modulesynopsis{A DB-API 2.0 implementation using SQLite 3.x.} |
Gerhard Häring | eb2e192 | 2006-04-29 23:12:41 +0000 | [diff] [blame] | 6 | |
| 7 | |
| 8 | |
| 9 | The module defines the following: |
| 10 | |
| 11 | \begin{datadesc}{PARSE_DECLTYPES} |
| 12 | This constant is meant to be used with the detect_types parameter of the connect function. |
| 13 | |
| 14 | Setting it makes the sqlite3 module parse the declared type for each column it |
| 15 | returns. It will parse out the first word of the declared type, i. e. for |
| 16 | "integer primary key", it will parse out "integer". Then for that column, it |
| 17 | will look into pysqlite's converters dictionary and use the converter function |
| 18 | registered for that type there. Converter names are case-sensitive! |
| 19 | \end{datadesc} |
| 20 | |
| 21 | |
| 22 | \begin{datadesc}{PARSE_COLNAMES} |
Gerhard Häring | eb2e192 | 2006-04-29 23:12:41 +0000 | [diff] [blame] | 23 | Setting this makes pysqlite parse the column name for each column it returns. |
| 24 | It will look for a string formed [mytype] in there, and then decide that |
| 25 | 'mytype' is the type of the column. It will try to find an entry of 'mytype' in |
| 26 | the converters dictionary and then use the converter function found there to |
| 27 | return the value. The column name found in cursor.description is only the first |
| 28 | word of the column name, i. e. if you use something like 'as "x [datetime]"' |
| 29 | in your SQL, then pysqlite will parse out everything until the first blank for |
| 30 | the column name: the column name would simply be "x". |
| 31 | \end{datadesc} |
| 32 | |
| 33 | \begin{funcdesc}{connect}{database\optional{, timeout, isolation_level, detect_types, check_same_thread, factory}} |
| 34 | Opens a connection to the SQLite database file \var{database}. You can use |
| 35 | \code{":memory:"} to open a database connection to a database that resides in |
| 36 | RAM instead of on disk. |
| 37 | |
| 38 | When a database is accessed by multiple connections, and one of the processes |
| 39 | modifies the database, the SQLite database is locked until that transaction is |
| 40 | committed. The \var{timeout} parameter specifies how long the connection should |
| 41 | wait for the lock to go away until raising an exception. The default for the |
| 42 | timeout parameter is 5.0 (five seconds). |
| 43 | |
| 44 | For the \var{isolation_level} parameter, please see TODO: link property of |
| 45 | Connection objects. |
| 46 | |
| 47 | SQLite natively supports only the types TEXT, INTEGER, FLOAT, BLOB and NULL. If |
| 48 | you want to use other types, like you have to add support for them yourself. |
| 49 | The \var{detect_types} parameter and the using custom *converters* registered with |
| 50 | the module-level *register_converter* function allow you to easily do that. |
| 51 | |
| 52 | \var{detect_types} defaults to 0 (i. e. off, no type detection), you can set it |
| 53 | to any combination of *PARSE_DECLTYPES* and *PARSE_COLNAMES* to turn type |
| 54 | detection on. |
| 55 | |
| 56 | By default, the sqlite3 module uses its Connection class for the connect call. |
| 57 | You can, however, subclass the Connection class and make .connect() use your |
| 58 | class instead by providing your class for the \var{factory} parameter. |
| 59 | |
| 60 | Consult the section `4. SQLite and Python types`_ of this manual for details. |
| 61 | |
| 62 | The sqlite3 module internally uses a statement cache to avoid SQL parsing |
| 63 | overhead. If you want to explicitly set the number of statements that are |
| 64 | cached for the connection, you can set the \var{cached_statements} parameter. |
| 65 | The currently implemented default is to cache 100 statements. |
| 66 | \end{funcdesc} |
| 67 | |
| 68 | \begin{funcdesc}{register_converter}{typename, callable} |
Gerhard Häring | eb2e192 | 2006-04-29 23:12:41 +0000 | [diff] [blame] | 69 | Registers a callable to convert a bytestring from the database into a custom |
| 70 | Python type. The callable will be invoked for all database values that are of |
| 71 | the type \var{typename}. Confer the parameter **detect_types** of the |
| 72 | **connect** method for how the type detection works. Note that the case of |
| 73 | \var{typename} and the name of the type in your query must match! |
| 74 | \end{funcdesc} |
| 75 | |
| 76 | \begin{funcdesc}{register_adapter}{type, callable} |
| 77 | Registers a callable to convert the custom Python type \var{type} into one of |
| 78 | SQLite's supported types. The callable \var{callable} accepts as single |
| 79 | parameter the Python value, and must return a value of the following types: |
| 80 | int, long, float, str (UTF-8 encoded), unicode or buffer. |
| 81 | \end{funcdesc} |
| 82 | |
| 83 | |
Fred Drake | 6550f03 | 2006-05-01 06:25:58 +0000 | [diff] [blame] | 84 | \subsection{Connection Objects \label{sqlite3-Connection-Objects}} |
Gerhard Häring | eb2e192 | 2006-04-29 23:12:41 +0000 | [diff] [blame] | 85 | |
| 86 | A \class{Connection} instance has the following attributes and methods: |
| 87 | |
Fred Drake | 6550f03 | 2006-05-01 06:25:58 +0000 | [diff] [blame] | 88 | \begin{memberdesc}{isolation_level} |
Gerhard Häring | 82560eb | 2006-05-01 15:14:48 +0000 | [diff] [blame^] | 89 | Get or set the current isolation level. None for autocommit mode or one |
| 90 | of "DEFERRED", "IMMEDIATE" or "EXLUSIVE". See `5. Controlling |
Fred Drake | 6550f03 | 2006-05-01 06:25:58 +0000 | [diff] [blame] | 91 | Transactions`_ for a more detailed explanation. |
| 92 | \end{memberdesc} |
Gerhard Häring | eb2e192 | 2006-04-29 23:12:41 +0000 | [diff] [blame] | 93 | |
| 94 | \begin{methoddesc}{cursor}{\optional{cursorClass}} |
Fred Drake | 6550f03 | 2006-05-01 06:25:58 +0000 | [diff] [blame] | 95 | The cursor method accepts a single optional parameter \var{cursorClass}. |
| 96 | This is a custom cursor class which must extend \class{sqlite3.Cursor}. |
Gerhard Häring | eb2e192 | 2006-04-29 23:12:41 +0000 | [diff] [blame] | 97 | \end{methoddesc} |
| 98 | |
Gerhard Häring | 82560eb | 2006-05-01 15:14:48 +0000 | [diff] [blame^] | 99 | \begin{methoddesc}{execute}{sql, \optional{parameters}} |
| 100 | This is a nonstandard shortcut that creates an intermediate cursor object by |
| 101 | calling the cursor method, then calls the cursor's execute method with the |
| 102 | parameters given. |
| 103 | \end{methoddesc} |
| 104 | |
| 105 | \begin{methoddesc}{executemany}{sql, \optional{parameters}} |
| 106 | This is a nonstandard shortcut that creates an intermediate cursor object by |
| 107 | calling the cursor method, then calls the cursor's executemany method with the |
| 108 | parameters given. |
| 109 | \end{methoddesc} |
| 110 | |
| 111 | \begin{methoddesc}{executescript}{sql_script} |
| 112 | This is a nonstandard shortcut that creates an intermediate cursor object by |
| 113 | calling the cursor method, then calls the cursor's executescript method with the |
| 114 | parameters given. |
| 115 | \end{methoddesc} |
| 116 | |
| 117 | \begin{memberdesc}{row_factory} |
| 118 | You can change this attribute to a callable that accepts the cursor and |
| 119 | the original row as tuple and will return the real result row. This |
| 120 | way, you can implement more advanced ways of returning results, like |
| 121 | ones that can also access columns by name. |
| 122 | |
| 123 | Example: |
| 124 | |
| 125 | \verbatiminput{sqlite3/row_factory.py} |
| 126 | |
| 127 | If the standard tuple types don't suffice for you, and you want name-based |
| 128 | access to columns, you should consider setting \member{row_factory} to the |
| 129 | highly-optimized pysqlite2.dbapi2.Row type. It provides both |
| 130 | index-based and case-insensitive name-based access to columns with almost |
| 131 | no memory overhead. Much better than your own custom dictionary-based |
| 132 | approach or even a db_row based solution. |
| 133 | \end{memberdesc} |
| 134 | |
| 135 | \begin{memberdesc}{text_factory} |
| 136 | Using this attribute you can control what objects pysqlite returns for the |
| 137 | TEXT data type. By default, this attribute is set to ``unicode`` and |
| 138 | pysqlite will return Unicode objects for TEXT. If you want to return |
| 139 | bytestrings instead, you can set it to ``str``. |
| 140 | |
| 141 | For efficiency reasons, there's also a way to return Unicode objects only |
| 142 | for non-ASCII data, and bytestrings otherwise. To activate it, set this |
| 143 | attribute to ``pysqlite2.dbapi2.OptimizedUnicode``. |
| 144 | |
| 145 | You can also set it to any other callable that accepts a single bytestring |
| 146 | parameter and returns the result object. |
| 147 | |
| 148 | See the following example code for illustration: |
| 149 | |
| 150 | \verbatiminput{sqlite3/text_factory.py} |
| 151 | \end{memberdesc} |
| 152 | |
| 153 | \begin{memberdesc}{total_changes} |
| 154 | Returns the total number of database rows that have be modified, inserted, |
| 155 | or deleted since the database connection was opened. |
| 156 | \end{memberdesc} |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | \subsection{Cursor Objects \label{Cursor-Objects}} |
| 163 | |
| 164 | A \class{Cursor} instance has the following attributes and methods: |
| 165 | |
| 166 | \begin{methoddesc}{execute}{sql, \optional{parameters}} |
| 167 | |
| 168 | Executes a SQL statement. The SQL statement may be parametrized (i. e. |
| 169 | placeholders instead of SQL literals). The sqlite3 module supports two kinds of |
| 170 | placeholders: question marks (qmark style) and named placeholders (named |
| 171 | style). |
| 172 | |
| 173 | This example shows how to use parameters with qmark style: |
| 174 | |
| 175 | \verbatiminput{sqlite3/execute_1.py} |
| 176 | |
| 177 | This example shows how to use the named style: |
| 178 | |
| 179 | \verbatiminput{sqlite3/execute_2.py} |
| 180 | |
| 181 | \method{execute} will only execute a single SQL statement. If you try to |
| 182 | execute more than one statement with it, it will raise a Warning. Use |
| 183 | \method{executescript} if want to execute multiple SQL statements with one |
| 184 | call. |
| 185 | \end{methoddesc} |
| 186 | |
| 187 | |
| 188 | \begin{methoddesc}{executemany}{sql, seq_of_parameters} |
| 189 | Executes a SQL command against all parameter sequences or mappings found in the |
| 190 | sequence \var{sql}. The \module{sqlite3} module also allows |
| 191 | to use an iterator yielding parameters instead of a sequence. |
| 192 | |
| 193 | \verbatiminput{sqlite3/executemany_1.py} |
| 194 | |
| 195 | Here's a shorter example using a generator: |
| 196 | |
| 197 | \verbatiminput{sqlite3/executemany_2.py} |
| 198 | \end{methoddesc} |
| 199 | |
| 200 | \begin{methoddesc}{executescript}{sql_script} |
| 201 | |
| 202 | This is a nonstandard convenience method for executing multiple SQL statements |
| 203 | at once. It issues a COMMIT statement before, then executes the SQL script it |
| 204 | gets as a parameter. |
| 205 | |
| 206 | \var{sql_script} can be a bytestring or a Unicode string. |
| 207 | |
| 208 | Example: |
| 209 | |
| 210 | \verbatiminput{sqlite3/executescript.py} |
| 211 | \end{methoddesc} |
| 212 | |
| 213 | \begin{memberdesc}{rowcount} |
| 214 | Although the Cursors of the \module{sqlite3} module implement this |
| 215 | attribute, the database engine's own support for the determination of "rows |
| 216 | affected"/"rows selected" is quirky. |
| 217 | |
| 218 | For \code{SELECT} statements, \member{rowcount} is always None because we cannot |
| 219 | determine the number of rows a query produced until all rows were fetched. |
| 220 | |
| 221 | For \code{DELETE} statements, SQLite reports \member{rowcount} as 0 if you make a |
| 222 | \code{DELETE FROM table} without any condition. |
| 223 | |
| 224 | For \method{executemany} statements, pysqlite sums up the number of |
| 225 | modifications into \member{rowcount}. |
| 226 | |
| 227 | As required by the Python DB API Spec, the \member{rowcount} attribute "is -1 |
| 228 | in case no executeXX() has been performed on the cursor or the rowcount |
| 229 | of the last operation is not determinable by the interface". |
| 230 | \end{memberdesc} |
| 231 | |