Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex


13.13.1 Module functions and constants

PARSE_DECLTYPES
This constant is meant to be used with thedetect_types parameter of theconnect function.

Setting it makes thesqlite3 module parse the declared type for each column itreturns. It will parse out the first word of the declared type, i. e. for"integer primary key", it will parse out "integer". Then for that column, itwill look into the converters dictionary and use the converter functionregistered for that type there. Converter names are case-sensitive!

PARSE_COLNAMES
This constant is meant to be used with thedetect_types parameter of theconnect function.

Setting this makes the SQLite interface parse the column name for each columnit returns. It will look for a string formed [mytype] in there, and thendecide that 'mytype' is the type of the column. It will try to find an entry of'mytype' in the converters dictionary and then use the converter function foundthere to return the value. The column name found incursor.description is onlythe first word of the column name, i. e. if you use something like'as "x [datetime]"' in your SQL, then we will parse out everything until thefirst blank for the column name: the column name would simply be "x".

connect(database[, timeout, isolation_level, detect_types, factory])
Opens a connection to the SQLite database filedatabase. You can use":memory:" to open a database connection to a database that resides inRAM instead of on disk.

When a database is accessed by multiple connections, and one of the processesmodifies the database, the SQLite database is locked until that transaction iscommitted. Thetimeout parameter specifies how long the connection shouldwait for the lock to go away until raising an exception. The default for thetimeout parameter is 5.0 (five seconds).

For theisolation_level parameter, please see theisolation_levelproperty ofConnection objects in section 13.13.2.

SQLite natively supports only the types TEXT, INTEGER, FLOAT, BLOB and NULL. Ifyou want to use other types, like you have to add support for them yourself.Thedetect_types parameter and the using customconverters registered withthe module-levelregister_converter function allow you to easily do that.

detect_types defaults to 0 (i. e. off, no type detection), you can set itto any combination ofPARSE_DECLTYPES andPARSE_COLNAMES to turn typedetection on.

By default, thesqlite3 module uses itsConnection class for theconnect call. You can, however, subclass theConnection class and makeconnect use your class instead by providing your class for thefactory parameter.

Consult the section13.13.4 of this manual for details.

Thesqlite3 module internally uses a statement cache to avoid SQL parsingoverhead. If you want to explicitly set the number of statements that arecached for the connection, you can set thecached_statements parameter.The currently implemented default is to cache 100 statements.

register_converter(typename, callable)
Registers a callable to convert a bytestring from the database into a customPython type. The callable will be invoked for all database values that are ofthe typetypename. Confer the parameterdetect_types of theconnect function for how the type detection works. Note that the case oftypename and the name of the type in your query must match!

register_adapter(type, callable)
Registers a callable to convert the custom Python typetype into one ofSQLite's supported types. The callablecallable accepts as singleparameter the Python value, and must return a value of the following types:int, long, float, str (UTF-8 encoded), unicode or buffer.

complete_statement(sql)
ReturnsTrue if the stringsql contains one or more complete SQLstatements terminated by semicolons. It does not verify that the SQL issyntactically correct, only that there are no unclosed string literals and thestatement is terminated by a semicolon.

This can be used to build a shell for SQLite, as in the following example:

# A minimal SQLite shell for experimentsimport sqlite3con = sqlite3.connect(":memory:")con.isolation_level = Nonecur = con.cursor()buffer = ""print "Enter your SQL commands to execute in sqlite3."print "Enter a blank line to exit."while True:    line = raw_input()    if line == "":        break    buffer += line    if sqlite3.complete_statement(buffer):        try:            buffer = buffer.strip()            cur.execute(buffer)            if buffer.lstrip().upper().startswith("SELECT"):                print cur.fetchall()        except sqlite3.Error, e:            print "An error occurred:", e.args[0]        buffer = ""con.close()
Download as text (original file name:sqlite3/complete_statement.py).

enable_callback_tracebacks(flag)
By default you will not get any tracebacks in user-defined functions,aggregates, converters, authorizer callbacks etc. If you want to debug them,you can call this function withflag as True. Afterwards, you will gettracebacks from callbacks onsys.stderr. UseFalse to disablethe feature again.


Up one LevelPython Library ReferenceContentsModule IndexIndex

Release 2.5.2, documentation updated on 21st February, 2008.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2025 Movatter.jp