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} |
| 5 | \modulesynopsis{A DB-API 2.0 interface based on SQLite 3.x.} |
| 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} |
| 23 | |
| 24 | Setting this makes pysqlite parse the column name for each column it returns. |
| 25 | It will look for a string formed [mytype] in there, and then decide that |
| 26 | 'mytype' is the type of the column. It will try to find an entry of 'mytype' in |
| 27 | the converters dictionary and then use the converter function found there to |
| 28 | return the value. The column name found in cursor.description is only the first |
| 29 | word of the column name, i. e. if you use something like 'as "x [datetime]"' |
| 30 | in your SQL, then pysqlite will parse out everything until the first blank for |
| 31 | the column name: the column name would simply be "x". |
| 32 | \end{datadesc} |
| 33 | |
| 34 | \begin{funcdesc}{connect}{database\optional{, timeout, isolation_level, detect_types, check_same_thread, factory}} |
| 35 | Opens a connection to the SQLite database file \var{database}. You can use |
| 36 | \code{":memory:"} to open a database connection to a database that resides in |
| 37 | RAM instead of on disk. |
| 38 | |
| 39 | When a database is accessed by multiple connections, and one of the processes |
| 40 | modifies the database, the SQLite database is locked until that transaction is |
| 41 | committed. The \var{timeout} parameter specifies how long the connection should |
| 42 | wait for the lock to go away until raising an exception. The default for the |
| 43 | timeout parameter is 5.0 (five seconds). |
| 44 | |
| 45 | For the \var{isolation_level} parameter, please see TODO: link property of |
| 46 | Connection objects. |
| 47 | |
| 48 | SQLite natively supports only the types TEXT, INTEGER, FLOAT, BLOB and NULL. If |
| 49 | you want to use other types, like you have to add support for them yourself. |
| 50 | The \var{detect_types} parameter and the using custom *converters* registered with |
| 51 | the module-level *register_converter* function allow you to easily do that. |
| 52 | |
| 53 | \var{detect_types} defaults to 0 (i. e. off, no type detection), you can set it |
| 54 | to any combination of *PARSE_DECLTYPES* and *PARSE_COLNAMES* to turn type |
| 55 | detection on. |
| 56 | |
| 57 | By default, the sqlite3 module uses its Connection class for the connect call. |
| 58 | You can, however, subclass the Connection class and make .connect() use your |
| 59 | class instead by providing your class for the \var{factory} parameter. |
| 60 | |
| 61 | Consult the section `4. SQLite and Python types`_ of this manual for details. |
| 62 | |
| 63 | The sqlite3 module internally uses a statement cache to avoid SQL parsing |
| 64 | overhead. If you want to explicitly set the number of statements that are |
| 65 | cached for the connection, you can set the \var{cached_statements} parameter. |
| 66 | The currently implemented default is to cache 100 statements. |
| 67 | \end{funcdesc} |
| 68 | |
| 69 | \begin{funcdesc}{register_converter}{typename, callable} |
| 70 | |
| 71 | Registers a callable to convert a bytestring from the database into a custom |
| 72 | Python type. The callable will be invoked for all database values that are of |
| 73 | the type \var{typename}. Confer the parameter **detect_types** of the |
| 74 | **connect** method for how the type detection works. Note that the case of |
| 75 | \var{typename} and the name of the type in your query must match! |
| 76 | \end{funcdesc} |
| 77 | |
| 78 | \begin{funcdesc}{register_adapter}{type, callable} |
| 79 | Registers a callable to convert the custom Python type \var{type} into one of |
| 80 | SQLite's supported types. The callable \var{callable} accepts as single |
| 81 | parameter the Python value, and must return a value of the following types: |
| 82 | int, long, float, str (UTF-8 encoded), unicode or buffer. |
| 83 | \end{funcdesc} |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | \subsection{Connection Objects \label{Connection-Objects}} |
| 91 | |
| 92 | A \class{Connection} instance has the following attributes and methods: |
| 93 | |
| 94 | \member{isolation_level} |
| 95 | Get or set the current isolation level. None for autocommit mode or one |
| 96 | of "DEFERRED", "IMMEDIATE" or "EXLUSIVE". See `5. Controlling |
| 97 | Transactions`_ for a more detailed explanation. |
| 98 | |
| 99 | \begin{methoddesc}{cursor}{\optional{cursorClass}} |
| 100 | The cursor method accepts a single optional parameter \var{cursorClass}. |
| 101 | This is a custom cursor class which must extend sqlite3.Cursor. |
| 102 | \end{methoddesc} |
| 103 | |
| 104 | TODO: execute* |
| 105 | |