From ff37d93483a12394ebb9cca175ea1508721d73ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Thu, 3 Dec 2020 19:48:56 +0100 Subject: [PATCH] SQLite --- README.md | 34 +++++++++++++++++----------------- index.html | 32 ++++++++++++++++---------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index cff4140..5653530 100644 --- a/README.md +++ b/README.md @@ -1841,48 +1841,48 @@ SQLite **Opens a connection to the database file. Creates a new file if path doesn't exist.** ```python import sqlite3 - = sqlite3.connect() # Also ':memory:'. -.close() # Closes the connection. + = sqlite3.connect() # Also ':memory:'. +.close() # Closes the connection. ``` ### Read **Returned values can be of type str, int, float, bytes or None.** ```python - = .execute('') # Can raise a subclass of sqlite3.Error. + = .execute('') # Can raise a subclass of sqlite3.Error. = .fetchone() # Returns next row. Also next(). = .fetchall() # Returns remaining rows. Also list(). ``` ### Write ```python -.execute('') # Can raise a subclass of sqlite3.Error. -.commit() # Commits all transactions since last commit. +.execute('') # Can raise a subclass of sqlite3.Error. +.commit() # Commits all transactions since last commit. ``` #### Or: ```python -with : - .execute('') +with : + .execute('') ``` ### Placeholders * **Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.** * **Bools will be stored and returned as ints and dates as [ISO formatted strings](#encode).** ```python -.execute('', ) # Replaces '?'s in query with values. -.execute('', ) # Replaces ':'s with values. -.executemany('', ) # Runs execute() multiple times. +.execute('', ) # Replaces '?'s in query with values. +.execute('', ) # Replaces ':'s with values. +.executemany('', ) # Runs execute() multiple times. ``` ### Example -**In this example values are not actually saved because `'con.commit()'` is omitted!** +**In this example values are not actually saved because `'conn.commit()'` is omitted!** ```python ->>> con = sqlite3.connect('test.db') ->>> con.execute('create table person (person_id integer primary key, name, height)') ->>> con.execute('insert into person values (null, ?, ?)', ('Jean-Luc', 187)).lastrowid +>>> conn = sqlite3.connect('test.db') +>>> conn.execute('create table person (person_id integer primary key, name, height)') +>>> conn.execute('insert into person values (null, ?, ?)', ('Jean-Luc', 187)).lastrowid 1 ->>> con.execute('select * from person').fetchall() +>>> conn.execute('select * from person').fetchall() [(1, 'Jean-Luc', 187)] ``` @@ -1891,8 +1891,8 @@ with : ```python # $ pip3 install mysql-connector from mysql import connector - = connector.connect(host=, …) # `user=, password=, database=`. - = .cursor() # Only cursor has execute method. + = connector.connect(host=, …) # `user=, password=, database=`. + = .cursor() # Only cursor has execute method. .execute('') # Can raise a subclass of connector.Error. .execute('', ) # Replaces '%s's in query with values. .execute('', ) # Replaces '%()s's with values. diff --git a/index.html b/index.html index f0e265b..b4adce7 100644 --- a/index.html +++ b/index.html @@ -1659,49 +1659,49 @@ CompletedProcess(args=['bc', #SQLite

Server-less database engine that stores each database into a separate file.

Connect

Opens a connection to the database file. Creates a new file if path doesn't exist.

import sqlite3
-<con> = sqlite3.connect(<path>)                 # Also ':memory:'.
-<con>.close()                                   # Closes the connection.
+<conn> = sqlite3.connect(<path>)                # Also ':memory:'.
+<conn>.close()                                  # Closes the connection.
 
-

Read

Returned values can be of type str, int, float, bytes or None.

<cursor> = <con>.execute('<query>')             # Can raise a subclass of sqlite3.Error.
+

Read

Returned values can be of type str, int, float, bytes or None.

<cursor> = <conn>.execute('<query>')            # Can raise a subclass of sqlite3.Error.
 <tuple>  = <cursor>.fetchone()                  # Returns next row. Also next(<cursor>).
 <list>   = <cursor>.fetchall()                  # Returns remaining rows. Also list(<cursor>).
 
-

Write

<con>.execute('<query>')                        # Can raise a subclass of sqlite3.Error.
-<con>.commit()                                  # Commits all transactions since last commit.
+

Write

<conn>.execute('<query>')                       # Can raise a subclass of sqlite3.Error.
+<conn>.commit()                                 # Commits all transactions since last commit.
 
-

Or:

with <con>:
-    <con>.execute('<query>')
+

Or:

with <conn>:
+    <conn>.execute('<query>')
 

Placeholders

  • Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.
  • Bools will be stored and returned as ints and dates as ISO formatted strings.
  • -
<con>.execute('<query>', <list/tuple>)          # Replaces '?'s in query with values.
-<con>.execute('<query>', <dict/namedtuple>)     # Replaces ':<key>'s with values.
-<con>.executemany('<query>', <coll_of_above>)   # Runs execute() multiple times.
+
<conn>.execute('<query>', <list/tuple>)         # Replaces '?'s in query with values.
+<conn>.execute('<query>', <dict/namedtuple>)    # Replaces ':<key>'s with values.
+<conn>.executemany('<query>', <coll_of_above>)  # Runs execute() multiple times.
 
-

Example

In this example values are not actually saved because 'con.commit()' is omitted!

>>> con = sqlite3.connect('test.db')
->>> con.execute('create table person (person_id integer primary key, name, height)')
->>> con.execute('insert into person values (null, ?, ?)', ('Jean-Luc', 187)).lastrowid
+

Example

In this example values are not actually saved because 'conn.commit()' is omitted!

>>> conn = sqlite3.connect('test.db')
+>>> conn.execute('create table person (person_id integer primary key, name, height)')
+>>> conn.execute('insert into person values (null, ?, ?)', ('Jean-Luc', 187)).lastrowid
 1
->>> con.execute('select * from person').fetchall()
+>>> conn.execute('select * from person').fetchall()
 [(1, 'Jean-Luc', 187)]
 

MySQL

Has a very similar interface, with differences listed below.

# $ pip3 install mysql-connector
 from mysql import connector
-<con> = connector.connect(host=<str>, …)        # `user=<str>, password=<str>, database=<str>`.
-<cursor> = <con>.cursor()                       # Only cursor has execute method.
+<conn>   = connector.connect(host=<str>, …)     # `user=<str>, password=<str>, database=<str>`.
+<cursor> = <conn>.cursor()                      # Only cursor has execute method.
 <cursor>.execute('<query>')                     # Can raise a subclass of connector.Error.
 <cursor>.execute('<query>', <list/tuple>)       # Replaces '%s's in query with values.
 <cursor>.execute('<query>', <dict/namedtuple>)  # Replaces '%(<key>)s's with values.