From 44e77fcbf05a26c23f6be51a6498a0828aaf9fc4 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 4 Mar 2021 15:57:33 +0100 Subject: [PATCH 1/3] Normalise docs: conn => con --- Doc/library/sqlite3.rst | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index c017dacff88976..aa30a8ed28ea33 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -26,14 +26,14 @@ represents the database. Here the data will be stored in the :file:`example.db` file:: import sqlite3 - conn = sqlite3.connect('example.db') + con = sqlite3.connect('example.db') You can also supply the special name ``:memory:`` to create a database in RAM. Once you have a :class:`Connection`, you can create a :class:`Cursor` object and call its :meth:`~Cursor.execute` method to perform SQL commands:: - c = conn.cursor() + c = con.cursor() # Create table c.execute('''CREATE TABLE stocks @@ -43,17 +43,17 @@ and call its :meth:`~Cursor.execute` method to perform SQL commands:: c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)") # Save (commit) the changes - conn.commit() + con.commit() # 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() + con.close() The data you've saved is persistent and is available in subsequent sessions:: import sqlite3 - conn = sqlite3.connect('example.db') - c = conn.cursor() + con = sqlite3.connect('example.db') + c = con.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 @@ -764,20 +764,20 @@ Row Objects Let's assume we initialize a table as in the example given above:: - conn = sqlite3.connect(":memory:") - c = conn.cursor() + con = sqlite3.connect(":memory:") + c = con.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)""") - conn.commit() + con.commit() c.close() Now we plug :class:`Row` in:: - >>> conn.row_factory = sqlite3.Row - >>> c = conn.cursor() + >>> con.row_factory = sqlite3.Row + >>> c = con.cursor() >>> c.execute('select * from stocks') >>> r = c.fetchone() From 9ff5290946ec31c4624170b18e0ce2506f3682b4 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 4 Mar 2021 15:58:22 +0100 Subject: [PATCH 2/3] Normalise docs: c => cur --- Doc/library/sqlite3.rst | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index aa30a8ed28ea33..fdd54759c83000 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -33,14 +33,14 @@ You can also supply the special name ``:memory:`` to create a database in RAM. Once you have a :class:`Connection`, you can create a :class:`Cursor` object and call its :meth:`~Cursor.execute` method to perform SQL commands:: - c = con.cursor() + cur = con.cursor() # Create table - c.execute('''CREATE TABLE stocks + cur.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)") + cur.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)") # Save (commit) the changes con.commit() @@ -53,7 +53,7 @@ The data you've saved is persistent and is available in subsequent sessions:: import sqlite3 con = sqlite3.connect('example.db') - c = con.cursor() + cur = con.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 @@ -68,19 +68,19 @@ example:: # Never do this -- insecure! symbol = 'RHAT' - c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol) + cur.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol) # Do this instead t = ('RHAT',) - c.execute('SELECT * FROM stocks WHERE symbol=?', t) - print(c.fetchone()) + cur.execute('SELECT * FROM stocks WHERE symbol=?', t) + print(cur.fetchone()) # 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) + cur.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 @@ -89,7 +89,7 @@ matching rows. This example uses the iterator form:: - >>> for row in c.execute('SELECT * FROM stocks ORDER BY price'): + >>> for row in cur.execute('SELECT * FROM stocks ORDER BY price'): print(row) ('2006-01-05', 'BUY', 'RHAT', 100, 35.14) @@ -765,22 +765,22 @@ Row Objects Let's assume we initialize a table as in the example given above:: con = sqlite3.connect(":memory:") - c = con.cursor() - c.execute('''create table stocks + cur = con.cursor() + cur.execute('''create table stocks (date text, trans text, symbol text, qty real, price real)''') - c.execute("""insert into stocks + cur.execute("""insert into stocks values ('2006-01-05','BUY','RHAT',100,35.14)""") con.commit() - c.close() + cur.close() Now we plug :class:`Row` in:: >>> con.row_factory = sqlite3.Row - >>> c = con.cursor() - >>> c.execute('select * from stocks') + >>> cur = con.cursor() + >>> cur.execute('select * from stocks') - >>> r = c.fetchone() + >>> r = cur.fetchone() >>> type(r) >>> tuple(r) From 8724545b8277b1fb8474de1f1b8db8f56579d93f Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Thu, 4 Mar 2021 16:02:43 +0100 Subject: [PATCH 3/3] Fix continuation line indents --- Doc/library/sqlite3.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index fdd54759c83000..172ce6c6bb03ba 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -37,7 +37,7 @@ and call its :meth:`~Cursor.execute` method to perform SQL commands:: # Create table cur.execute('''CREATE TABLE stocks - (date text, trans text, symbol text, qty real, price real)''') + (date text, trans text, symbol text, qty real, price real)''') # Insert a row of data cur.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)") @@ -770,7 +770,7 @@ Let's assume we initialize a table as in the example given above:: (date text, trans text, symbol text, qty real, price real)''') cur.execute("""insert into stocks - values ('2006-01-05','BUY','RHAT',100,35.14)""") + values ('2006-01-05','BUY','RHAT',100,35.14)""") con.commit() cur.close()