diff -r e95e647040de Doc/includes/sqlite3/adapter_datetime.py --- a/Doc/includes/sqlite3/adapter_datetime.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/adapter_datetime.py Thu Nov 01 12:16:35 2012 -0500 @@ -11,5 +11,5 @@ cur = con.cursor() now = datetime.datetime.now() -cur.execute("select ?", (now,)) +cur.execute("SELECT ?", (now,)) print(cur.fetchone()[0]) diff -r e95e647040de Doc/includes/sqlite3/adapter_point_1.py --- a/Doc/includes/sqlite3/adapter_point_1.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/adapter_point_1.py Thu Nov 01 12:16:35 2012 -0500 @@ -12,5 +12,5 @@ cur = con.cursor() p = Point(4.0, -3.2) -cur.execute("select ?", (p,)) +cur.execute("SELECT ?", (p,)) print(cur.fetchone()[0]) diff -r e95e647040de Doc/includes/sqlite3/adapter_point_2.py --- a/Doc/includes/sqlite3/adapter_point_2.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/adapter_point_2.py Thu Nov 01 12:16:35 2012 -0500 @@ -13,5 +13,5 @@ cur = con.cursor() p = Point(4.0, -3.2) -cur.execute("select ?", (p,)) +cur.execute("SELECT ?", (p,)) print(cur.fetchone()[0]) diff -r e95e647040de Doc/includes/sqlite3/collation_reverse.py --- a/Doc/includes/sqlite3/collation_reverse.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/collation_reverse.py Thu Nov 01 12:16:35 2012 -0500 @@ -12,9 +12,9 @@ con.create_collation("reverse", collate_reverse) cur = con.cursor() -cur.execute("create table test(x)") -cur.executemany("insert into test(x) values (?)", [("a",), ("b",)]) -cur.execute("select x from test order by x collate reverse") +cur.execute("CREATE TABLE test(x)") +cur.executemany("INSERT INTO test(x) VALUES (?)", [("a",), ("b",)]) +cur.execute("SELECT x FROM test ORDER BY x COLLATE reverse") for row in cur: print(row) con.close() diff -r e95e647040de Doc/includes/sqlite3/converter_point.py --- a/Doc/includes/sqlite3/converter_point.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/converter_point.py Thu Nov 01 12:16:35 2012 -0500 @@ -26,10 +26,10 @@ # 1) Using declared types con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES) cur = con.cursor() -cur.execute("create table test(p point)") +cur.execute("CREATE TABLE test(p point)") -cur.execute("insert into test(p) values (?)", (p,)) -cur.execute("select p from test") +cur.execute("INSERT INTO test(p) VALUES (?)", (p,)) +cur.execute("SELECT p FROM test") print("with declared types:", cur.fetchone()[0]) cur.close() con.close() @@ -38,10 +38,10 @@ # 1) Using column names con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_COLNAMES) cur = con.cursor() -cur.execute("create table test(p)") +cur.execute("CREATE TABLE test(p)") -cur.execute("insert into test(p) values (?)", (p,)) -cur.execute('select p as "p [point]" from test') +cur.execute("INSERT INTO test(p) VALUES (?)", (p,)) +cur.execute('SELECT p AS "p [point]" FROM test') print("with column names:", cur.fetchone()[0]) cur.close() con.close() diff -r e95e647040de Doc/includes/sqlite3/ctx_manager.py --- a/Doc/includes/sqlite3/ctx_manager.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/ctx_manager.py Thu Nov 01 12:16:35 2012 -0500 @@ -1,16 +1,16 @@ import sqlite3 con = sqlite3.connect(":memory:") -con.execute("create table person (id integer primary key, firstname varchar unique)") +con.execute("CREATE TABLE person (id integer primary key, firstname varchar UNIQUE)") # Successful, con.commit() is called automatically afterwards with con: - con.execute("insert into person(firstname) values (?)", ("Joe",)) + con.execute("INSERT INTO person(firstname) VALUES (?)", ("Joe",)) # con.rollback() is called after the with block finishes with an exception, the # exception is still raised and must be caught try: with con: - con.execute("insert into person(firstname) values (?)", ("Joe",)) + con.execute("INSERT INTO person(firstname) VALUES (?)", ("Joe",)) except sqlite3.IntegrityError: print("couldn't add Joe twice") diff -r e95e647040de Doc/includes/sqlite3/execsql_fetchonerow.py --- a/Doc/includes/sqlite3/execsql_fetchonerow.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/execsql_fetchonerow.py Thu Nov 01 12:16:35 2012 -0500 @@ -3,7 +3,7 @@ con = sqlite3.connect("mydb") cur = con.cursor() -SELECT = "select name_last, age from people order by age, name_last" +SELECT = "SELECT name_last, age FROM people ORDER BY age, name_last" # 1. Iterate over the rows available from the cursor, unpacking the # resulting sequences to yield their elements (name_last, age): diff -r e95e647040de Doc/includes/sqlite3/execsql_printall_1.py --- a/Doc/includes/sqlite3/execsql_printall_1.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/execsql_printall_1.py Thu Nov 01 12:16:35 2012 -0500 @@ -7,7 +7,7 @@ cur = con.cursor() # Execute the SELECT statement: -cur.execute("select * from people order by age") +cur.execute("SELECT * FROM people ORDER BY age") # Retrieve all rows as a sequence and print that sequence: print(cur.fetchall()) diff -r e95e647040de Doc/includes/sqlite3/execute_1.py --- a/Doc/includes/sqlite3/execute_1.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/execute_1.py Thu Nov 01 12:16:35 2012 -0500 @@ -2,15 +2,15 @@ con = sqlite3.connect(":memory:") cur = con.cursor() -cur.execute("create table people (name_last, age)") +cur.execute("CREATE TABLE people (name_last, age)") who = "Yeltsin" age = 72 # This is the qmark style: -cur.execute("insert into people values (?, ?)", (who, age)) +cur.execute("INSERT INTO people VALUES (?, ?)", (who, age)) # And this is the named style: -cur.execute("select * from people where name_last=:who and age=:age", {"who": who, "age": age}) +cur.execute("SELECT * FROM people WHERE name_last=:who AND age=:age", {"who": who, "age": age}) print(cur.fetchone()) diff -r e95e647040de Doc/includes/sqlite3/execute_3.py --- a/Doc/includes/sqlite3/execute_3.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/execute_3.py Thu Nov 01 12:16:35 2012 -0500 @@ -7,6 +7,6 @@ who = "Yeltsin" age = 72 -cur.execute("select name_last, age from people where name_last=:who and age=:age", +cur.execute("SELECT name_last, age FROM people WHERE name_last=:who AND age=:age", locals()) print(cur.fetchone()) diff -r e95e647040de Doc/includes/sqlite3/executemany_1.py --- a/Doc/includes/sqlite3/executemany_1.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/executemany_1.py Thu Nov 01 12:16:35 2012 -0500 @@ -15,10 +15,10 @@ con = sqlite3.connect(":memory:") cur = con.cursor() -cur.execute("create table characters(c)") +cur.execute("CREATE TABLE characters(c)") theIter = IterChars() -cur.executemany("insert into characters(c) values (?)", theIter) +cur.executemany("INSERT INTO characters(c) VALUES (?)", theIter) -cur.execute("select c from characters") +cur.execute("SELECT c FROM characters") print(cur.fetchall()) diff -r e95e647040de Doc/includes/sqlite3/executemany_2.py --- a/Doc/includes/sqlite3/executemany_2.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/executemany_2.py Thu Nov 01 12:16:35 2012 -0500 @@ -7,9 +7,9 @@ con = sqlite3.connect(":memory:") cur = con.cursor() -cur.execute("create table characters(c)") +cur.execute("CREATE TABLE characters(c)") -cur.executemany("insert into characters(c) values (?)", char_generator()) +cur.executemany("INSERT INTO characters(c) VALUES (?)", char_generator()) -cur.execute("select c from characters") +cur.execute("SELECT c FROM characters") print(cur.fetchall()) diff -r e95e647040de Doc/includes/sqlite3/executescript.py --- a/Doc/includes/sqlite3/executescript.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/executescript.py Thu Nov 01 12:16:35 2012 -0500 @@ -3,20 +3,20 @@ con = sqlite3.connect(":memory:") cur = con.cursor() cur.executescript(""" - create table person( + CREATE TABLE person( firstname, lastname, age ); - create table book( + CREATE TABLE book( title, author, published ); - insert into book(title, author, published) - values ( + INSERT INTO book(title, author, published) + VALUES ( 'Dirk Gently''s Holistic Detective Agency', 'Douglas Adams', 1987 diff -r e95e647040de Doc/includes/sqlite3/insert_more_people.py --- a/Doc/includes/sqlite3/insert_more_people.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/insert_more_people.py Thu Nov 01 12:16:35 2012 -0500 @@ -10,7 +10,7 @@ ) for person in newPeople: - cur.execute("insert into people (name_last, age) values (?, ?)", person) + cur.execute("INSERT INTO people (name_last, age) VALUES (?, ?)", person) # The changes will not be saved unless the transaction is committed explicitly: con.commit() diff -r e95e647040de Doc/includes/sqlite3/load_extension.py --- a/Doc/includes/sqlite3/load_extension.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/load_extension.py Thu Nov 01 12:16:35 2012 -0500 @@ -6,7 +6,7 @@ con.enable_load_extension(True) # Load the fulltext search extension -con.execute("select load_extension('./fts3.so')") +con.execute("SELECT load_extension('./fts3.so')") # alternatively you can load the extension using an API call: # con.load_extension("./fts3.so") @@ -15,12 +15,12 @@ con.enable_load_extension(False) # example from SQLite wiki -con.execute("create virtual table recipe using fts3(name, ingredients)") +con.execute("CREATE VIRTUAL TABLE recipe USING fts3(name, ingredients)") con.executescript(""" - insert into recipe (name, ingredients) values ('broccoli stew', 'broccoli peppers cheese tomatoes'); - insert into recipe (name, ingredients) values ('pumpkin stew', 'pumpkin onions garlic celery'); - insert into recipe (name, ingredients) values ('broccoli pie', 'broccoli cheese onions flour'); - insert into recipe (name, ingredients) values ('pumpkin pie', 'pumpkin sugar flour butter'); + INSERT INTO recipe (name, ingredients) VALUES ('broccoli stew', 'broccoli peppers cheese tomatoes'); + INSERT INTO recipe (name, ingredients) VALUES ('pumpkin stew', 'pumpkin onions garlic celery'); + INSERT INTO recipe (name, ingredients) VALUES ('broccoli pie', 'broccoli cheese onions flour'); + INSERT INTO recipe (name, ingredients) VALUES ('pumpkin pie', 'pumpkin sugar flour butter'); """) -for row in con.execute("select rowid, name, ingredients from recipe where name match 'pie'"): +for row in con.execute("SELECT rowid, name, ingredients FROM recipe WHERE name MATCH 'pie'"): print(row) diff -r e95e647040de Doc/includes/sqlite3/md5func.py --- a/Doc/includes/sqlite3/md5func.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/md5func.py Thu Nov 01 12:16:35 2012 -0500 @@ -7,5 +7,5 @@ con = sqlite3.connect(":memory:") con.create_function("md5", 1, md5sum) cur = con.cursor() -cur.execute("select md5(?)", (b"foo",)) +cur.execute("SELECT md5(?)", (b"foo",)) print(cur.fetchone()[0]) diff -r e95e647040de Doc/includes/sqlite3/mysumaggr.py --- a/Doc/includes/sqlite3/mysumaggr.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/mysumaggr.py Thu Nov 01 12:16:35 2012 -0500 @@ -13,8 +13,8 @@ con = sqlite3.connect(":memory:") con.create_aggregate("mysum", 1, MySum) cur = con.cursor() -cur.execute("create table test(i)") -cur.execute("insert into test(i) values (1)") -cur.execute("insert into test(i) values (2)") -cur.execute("select mysum(i) from test") +cur.execute("CREATE TABLE test(i)") +cur.execute("INSERT INTO test(i) VALUES (1)") +cur.execute("INSERT INTO test(i) VALUES (2)") +cur.execute("SELECT mysum(i) FROM test") print(cur.fetchone()[0]) diff -r e95e647040de Doc/includes/sqlite3/parse_colnames.py --- a/Doc/includes/sqlite3/parse_colnames.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/parse_colnames.py Thu Nov 01 12:16:35 2012 -0500 @@ -3,6 +3,6 @@ con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_COLNAMES) cur = con.cursor() -cur.execute('select ? as "x [timestamp]"', (datetime.datetime.now(),)) +cur.execute('SELECT ? AS "x [timestamp]"', (datetime.datetime.now(),)) dt = cur.fetchone()[0] print(dt, type(dt)) diff -r e95e647040de Doc/includes/sqlite3/pysqlite_datetime.py --- a/Doc/includes/sqlite3/pysqlite_datetime.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/pysqlite_datetime.py Thu Nov 01 12:16:35 2012 -0500 @@ -3,18 +3,18 @@ con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES) cur = con.cursor() -cur.execute("create table test(d date, ts timestamp)") +cur.execute("CREATE TABLE test(d date, ts timestamp)") today = datetime.date.today() now = datetime.datetime.now() -cur.execute("insert into test(d, ts) values (?, ?)", (today, now)) -cur.execute("select d, ts from test") +cur.execute("INSERT INTO test(d, ts) VALUES (?, ?)", (today, now)) +cur.execute("SELECT d, ts FROM test") row = cur.fetchone() print(today, "=>", row[0], type(row[0])) print(now, "=>", row[1], type(row[1])) -cur.execute('select current_date as "d [date]", current_timestamp as "ts [timestamp]"') +cur.execute('SELECT current_date AS "d [date]", current_timestamp AS "ts [timestamp]"') row = cur.fetchone() print("current_date", row[0], type(row[0])) print("current_timestamp", row[1], type(row[1])) diff -r e95e647040de Doc/includes/sqlite3/row_factory.py --- a/Doc/includes/sqlite3/row_factory.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/row_factory.py Thu Nov 01 12:16:35 2012 -0500 @@ -9,5 +9,5 @@ con = sqlite3.connect(":memory:") con.row_factory = dict_factory cur = con.cursor() -cur.execute("select 1 as a") +cur.execute("SELECT 1 AS a") print(cur.fetchone()["a"]) diff -r e95e647040de Doc/includes/sqlite3/rowclass.py --- a/Doc/includes/sqlite3/rowclass.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/rowclass.py Thu Nov 01 12:16:35 2012 -0500 @@ -4,7 +4,7 @@ con.row_factory = sqlite3.Row cur = con.cursor() -cur.execute("select 'John' as name, 42 as age") +cur.execute("SELECT 'John' AS name, 42 AS age") for row in cur: assert row[0] == row["name"] assert row["name"] == row["nAmE"] diff -r e95e647040de Doc/includes/sqlite3/shortcut_methods.py --- a/Doc/includes/sqlite3/shortcut_methods.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/shortcut_methods.py Thu Nov 01 12:16:35 2012 -0500 @@ -8,13 +8,13 @@ con = sqlite3.connect(":memory:") # Create the table -con.execute("create table person(firstname, lastname)") +con.execute("CREATE TABLE person(firstname, lastname)") # Fill the table -con.executemany("insert into person(firstname, lastname) values (?, ?)", persons) +con.executemany("INSERT INTO person(firstname, lastname) VALUES (?, ?)", persons) # Print the table contents -for row in con.execute("select firstname, lastname from person"): +for row in con.execute("SELECT firstname, lastname FROM person"): print(row) -print("I just deleted", con.execute("delete from person").rowcount, "rows") +print("I just deleted", con.execute("DELETE FROM person").rowcount, "rows") diff -r e95e647040de Doc/includes/sqlite3/simple_tableprinter.py --- a/Doc/includes/sqlite3/simple_tableprinter.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/simple_tableprinter.py Thu Nov 01 12:16:35 2012 -0500 @@ -2,7 +2,7 @@ FIELD_MAX_WIDTH = 20 TABLE_NAME = 'people' -SELECT = 'select * from %s order by age, name_last' % TABLE_NAME +SELECT = 'SELECT * FROM %s ORDER BY age, name_last' % TABLE_NAME con = sqlite3.connect("mydb") diff -r e95e647040de Doc/includes/sqlite3/text_factory.py --- a/Doc/includes/sqlite3/text_factory.py Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/includes/sqlite3/text_factory.py Thu Nov 01 12:16:35 2012 -0500 @@ -5,23 +5,23 @@ AUSTRIA = "\xd6sterreich" -# by default, rows are returned as Unicode -cur.execute("select ?", (AUSTRIA,)) +# by default, rows are returned as str +cur.execute("SELECT ?", (AUSTRIA,)) row = cur.fetchone() assert row[0] == AUSTRIA -# but we can make sqlite3 always return bytestrings ... +# but we can make sqlite3 always return bytes ... con.text_factory = bytes cur.execute("select ?", (AUSTRIA,)) row = cur.fetchone() assert type(row[0]) is bytes -# the bytestrings will be encoded in UTF-8, unless you stored garbage in the +# the bytes will be encoded in UTF-8, unless you stored garbage in the # database ... assert row[0] == AUSTRIA.encode("utf-8") # we can also implement a custom text_factory ... # here we implement one that appends "foo" to all strings con.text_factory = lambda x: x.decode("utf-8") + "foo" -cur.execute("select ?", ("bar",)) +cur.execute("SELECT ?", ("bar",)) row = cur.fetchone() assert row[0] == "barfoo" diff -r e95e647040de Doc/library/sqlite3.rst --- a/Doc/library/sqlite3.rst Thu Nov 01 13:43:06 2012 +0200 +++ b/Doc/library/sqlite3.rst Thu Nov 01 12:16:35 2012 -0500 @@ -13,15 +13,15 @@ application using SQLite and then port the code to a larger database such as PostgreSQL or Oracle. -sqlite3 was written by Gerhard Häring and provides a SQL interface compliant -with the DB-API 2.0 specification described by :pep:`249`. +The sqlite3 module was written by Gerhard Häring. It provides a SQL interface +compliant with the DB-API 2.0 specification described by :pep:`249`. To use the module, you must first create a :class:`Connection` object that represents the database. Here the data will be stored in the -:file:`/tmp/example` file:: +:file:`example.db` file:: import sqlite3 - conn = sqlite3.connect('/tmp/example') + conn = sqlite3.connect('example.db') You can also supply the special name ``:memory:`` to create a database in RAM. @@ -31,23 +31,29 @@ c = conn.cursor() # Create table - c.execute('''create table stocks - (date text, trans text, symbol text, - qty real, price real)''') + c.execute('''CREATE TABLE stocks + (date text, trans text, symbol text, qty real, price real)''') # Insert a row of data - c.execute("""insert into stocks - values ('2006-01-05','BUY','RHAT',100,35.14)""") + c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)") # Save (commit) the changes conn.commit() - # We can also close the cursor if we are done with it - c.close() + # We can also close the connection if we are done with it. + # Just be sure any changes have been committed or they will be lost. + conn.close() + +The data you've saved is persistent and is available in subsequent sessions:: + + import sqlite3 + conn = sqlite3.connect('example.db') + c = conn.cursor() Usually your SQL operations will need to use values from Python variables. You shouldn't assemble your query using Python's string operations because doing so -is insecure; it makes your program vulnerable to an SQL injection attack. +is insecure; it makes your program vulnerable to an SQL injection attack +(see http://xkcd.com/327/ for humorous example of what can go wrong). Instead, use the DB-API's parameter substitution. Put ``?`` as a placeholder wherever you want to use a value, and then provide a tuple of values as the @@ -56,19 +62,20 @@ example:: # Never do this -- insecure! - symbol = 'IBM' - c.execute("select * from stocks where symbol = '%s'" % symbol) + symbol = 'RHAT' + c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol) # Do this instead - t = ('IBM',) - c.execute('select * from stocks where symbol=?', t) + t = ('RHAT',) + c.execute('SELECT * FROM stocks WHERE symbol=?', t) + print c.fetchone() - # Larger example - for t in [('2006-03-28', 'BUY', 'IBM', 1000, 45.00), - ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00), - ('2006-04-06', 'SELL', 'IBM', 500, 53.00), - ]: - c.execute('insert into stocks values (?,?,?,?,?)', t) + # Larger example that inserts many records at a time + purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00), + ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00), + ('2006-04-06', 'SELL', 'IBM', 500, 53.00), + ] + c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases) To retrieve data after executing a SELECT statement, you can either treat the cursor as an :term:`iterator`, call the cursor's :meth:`~Cursor.fetchone` method to @@ -77,16 +84,13 @@ This example uses the iterator form:: - >>> c = conn.cursor() - >>> c.execute('select * from stocks order by price') - >>> for row in c: + >>> for row in c.execute('SELECT * FROM stocks ORDER BY price'): ... print(row) ... ('2006-01-05', 'BUY', 'RHAT', 100, 35.14) ('2006-03-28', 'BUY', 'IBM', 1000, 45.0) ('2006-04-06', 'SELL', 'IBM', 500, 53.0) - ('2006-04-05', 'BUY', 'MSOFT', 1000, 72.0) - >>> + ('2006-04-05', 'BUY', 'MSFT', 1000, 72.0) .. seealso:: @@ -99,6 +103,9 @@ The SQLite web page; the documentation describes the syntax and the available data types for the supported SQL dialect. + http://www.w3schools.com/sql/ + Tutorial, reference and examples for learning SQL syntax. + :pep:`249` - Database API Specification 2.0 PEP written by Marc-André Lemburg. @@ -408,9 +415,7 @@ highly-optimized :class:`sqlite3.Row` type. :class:`Row` provides both index-based and case-insensitive name-based access to columns with almost no memory overhead. It will probably be better than your own custom - dictionary-based approach or even a db_row based solution. - - .. XXX what's a db_row-based solution? + dictionary-based approach. .. attribute:: text_factory @@ -467,7 +472,7 @@ .. method:: execute(sql, [parameters]) - Executes an SQL statement. The SQL statement may be parametrized (i. e. + Executes an SQL statement. The SQL statement may be parameterized (i. e. placeholders instead of SQL literals). The :mod:`sqlite3` module supports two kinds of placeholders: question marks (qmark style) and named placeholders (named style). @@ -596,11 +601,9 @@ conn = sqlite3.connect(":memory:") c = conn.cursor() - c.execute('''create table stocks - (date text, trans text, symbol text, - qty real, price real)''') - c.execute("""insert into stocks - values ('2006-01-05','BUY','RHAT',100,35.14)""") + c.execute('''CREATE TABLE stocks + (date text, trans text, symbol text, qty real, price real)''') + c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)") conn.commit() c.close() @@ -608,7 +611,7 @@ >>> conn.row_factory = sqlite3.Row >>> c = conn.cursor() - >>> c.execute('select * from stocks') + >>> c.execute('SELECT * FROM stocks') >>> r = c.fetchone() >>> type(r) @@ -647,36 +650,37 @@ The following Python types can thus be sent to SQLite without any problem: -+-------------------------------+-------------+ -| Python type | SQLite type | -+===============================+=============+ -| :const:`None` | ``NULL`` | -+-------------------------------+-------------+ -| :class:`int` | ``INTEGER`` | -+-------------------------------+-------------+ -| :class:`float` | ``REAL`` | -+-------------------------------+-------------+ -| :class:`str` | ``TEXT`` | -+-------------------------------+-------------+ -| :class:`bytes` | ``BLOB`` | -+-------------------------------+-------------+ ++-----------------------------+-------------+ +| Python type | SQLite type | ++=============================+=============+ +| :const:`None` | ``NULL`` | ++-----------------------------+-------------+ +| :class:`int` | ``INTEGER`` | ++-----------------------------+-------------+ +| :class:`float` | ``REAL`` | ++-----------------------------+-------------+ +| :class:`str` | ``TEXT`` | ++-----------------------------+-------------+ +| :class:`bytes` | ``BLOB`` | ++-----------------------------+-------------+ This is how SQLite types are converted to Python types by default: -+-------------+---------------------------------------------+ -| SQLite type | Python type | -+=============+=============================================+ -| ``NULL`` | :const:`None` | -+-------------+---------------------------------------------+ -| ``INTEGER`` | :class:`int` | -+-------------+---------------------------------------------+ -| ``REAL`` | :class:`float` | -+-------------+---------------------------------------------+ -| ``TEXT`` | depends on text_factory, str by default | -+-------------+---------------------------------------------+ -| ``BLOB`` | :class:`bytes` | -+-------------+---------------------------------------------+ ++-------------+----------------------------------------------+ +| SQLite type | Python type | ++=============+==============================================+ +| ``NULL`` | :const:`None` | ++-------------+----------------------------------------------+ +| ``INTEGER`` | :class:`int` | ++-------------+----------------------------------------------+ +| ``REAL`` | :class:`float` | ++-------------+----------------------------------------------+ +| ``TEXT`` | depends on :attr:`~Connection.text_factory`, | +| | :class:`str` by default | ++-------------+----------------------------------------------+ +| ``BLOB`` | :class:`bytes` | ++-------------+----------------------------------------------+ The type system of the :mod:`sqlite3` module is extensible in two ways: you can store additional Python types in a SQLite database via object adaptation, and @@ -692,9 +696,6 @@ sqlite3 module's supported types for SQLite: one of NoneType, int, float, str, bytes. -The :mod:`sqlite3` module uses Python object adaptation, as described in -:pep:`246` for this. The protocol to use is :class:`PrepareProtocol`. - There are two ways to enable the :mod:`sqlite3` module to adapt a custom Python type to one of the supported ones. @@ -750,8 +751,8 @@ .. note:: - Converter functions **always** get called with a string, no matter under which - data type you sent the value to SQLite. + Converter functions **always** get called with a :class:`bytes` object, no + matter under which data type you sent the value to SQLite. ::