From 3ecb22fb7bd10acc9c76c4cb4a8055228b7580ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Tue, 2 Jul 2019 23:29:47 +0200 Subject: [PATCH] SQLite --- README.md | 29 +++++++++++++++++++++-------- index.html | 29 +++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 25e5d52..9eb970c 100644 --- a/README.md +++ b/README.md @@ -929,21 +929,22 @@ class : * **Return value of repr() should be unambiguous and of str() readable.** * **If only repr() is defined, it will also be used for str().** -#### Str() is used by: +#### Str() use cases: ```python print() -f'{}' +print(f'{}') raise Exception() logging.debug() csv.writer().writerow([]) ``` -#### Repr() is used by: +#### Repr() use cases: ```python print([]) -f'{!r}' +print(f'{!r}') >>> loguru.logger.exception() +Z = dataclasses.make_dataclass('Z', ['a']); print(Z()) ``` ### Constructor Overloading @@ -1502,12 +1503,14 @@ def write_to_pickle_file(filename, an_object): SQLite ------ +**Server-less database engine that stores each database into separate file.** ```python import sqlite3 db = sqlite3.connect('') # Also ':memory:'. ... db.close() ``` +* **New database will be created if path doesn't exist.** ### Read ```python @@ -1516,7 +1519,7 @@ if cursor: = cursor.fetchone() # First row. = cursor.fetchall() # Remaining rows. ``` -* **Returned values can be of type str, int, float or bytes.** +* **Returned values can be of type str, int, float, bytes or None.** ### Write ```python @@ -1526,10 +1529,20 @@ db.commit() ### Placeholders ```python -db.execute('', ) # Replaces '?' in query with value. -db.execute('', ) # Replaces ':' with value. +db.execute('', ) # Replaces '?'s in query with values. +db.execute('', ) # Replaces ':'s with values. +db.executemany('', ) # Runs execute() many times. +``` +* **Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.** + +### MySQL +```python +# $ pip3 install mysql-connector +from mysql import connector +db = connector.connect(host=, user=, password=, database=) +cursor = db.cursor() +cursor.execute('') ``` -* **Passed values can be of type str, int, float, bytes, bool, datetime.date and datetime.datetme.** Bytes diff --git a/index.html b/index.html index 5fa14ef..e65e473 100644 --- a/index.html +++ b/index.html @@ -883,18 +883,19 @@ creature = Creature(Point(0, Str() is used by: +

Str() use cases:

print(<el>)
-f'{<el>}'
+print(f'{<el>}')
 raise Exception(<el>)
 logging.debug(<el>)
 csv.writer(<file>).writerow([<el>])
 
-

Repr() is used by:

+

Repr() use cases:

print([<el>])
-f'{<el>!r}'
+print(f'{<el>!r}')
 >>> <el>
 loguru.logger.exception()
+Z = dataclasses.make_dataclass('Z', ['a']); print(Z(<el>))
 

Constructor Overloading

class <name>:
@@ -1323,11 +1324,15 @@ value = args.<name>
         pickle.dump(an_object, file)
 

#SQLite

+

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

import sqlite3
 db = sqlite3.connect('<path>')   # Also ':memory:'.
 ...
 db.close()
 
+
    +
  • New database will be created if path doesn't exist.
  • +

Read

cursor = db.execute('<query>')
 if cursor:
@@ -1335,19 +1340,27 @@ db.close()
     <list>  = cursor.fetchall()  # Remaining rows.
 
    -
  • Returned values can be of type str, int, float or bytes.
  • +
  • Returned values can be of type str, int, float, bytes or None.

Write

db.execute('<query>')
 db.commit()
 

Placeholders

-
db.execute('<query>', <list/tuple>)       # Replaces '?' in query with value.
-db.execute('<query>', <dict/namedtuple>)  # Replaces ':<key>' with value.
+
db.execute('<query>', <list/tuple>)         # Replaces '?'s in query with values.
+db.execute('<query>', <dict/namedtuple>)    # Replaces ':<key>'s with values.
+db.executemany('<query>', <coll_of_above>)  # Runs execute() many times.
 
    -
  • Passed values can be of type str, int, float, bytes, bool, datetime.date and datetime.datetme.
  • +
  • Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.
+

MySQL

+
# $ pip3 install mysql-connector
+from mysql import connector
+db = connector.connect(host=<str>, user=<str>, password=<str>, database=<str>)
+cursor = db.cursor()
+cursor.execute('<query>')
+

#Bytes

Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.

<bytes> = b'<str>'                       # Only accepts ASCII characters and \x00 - \xff.