diff options
Diffstat (limited to 'Doc/includes/sqlite3')
| -rw-r--r-- | Doc/includes/sqlite3/adapter_datetime.py | 3 | ||||
| -rw-r--r-- | Doc/includes/sqlite3/converter_point.py | 4 | ||||
| -rw-r--r-- | Doc/includes/sqlite3/ctx_manager.py | 2 | ||||
| -rw-r--r-- | Doc/includes/sqlite3/execute_1.py | 11 | ||||
| -rw-r--r-- | Doc/includes/sqlite3/execute_2.py | 12 | ||||
| -rw-r--r-- | Doc/includes/sqlite3/executemany_2.py | 4 | ||||
| -rw-r--r-- | Doc/includes/sqlite3/load_extension.py | 26 | ||||
| -rw-r--r-- | Doc/includes/sqlite3/md5func.py | 2 | ||||
| -rw-r--r-- | Doc/includes/sqlite3/rowclass.py | 8 | ||||
| -rw-r--r-- | Doc/includes/sqlite3/shortcut_methods.py | 3 | ||||
| -rw-r--r-- | Doc/includes/sqlite3/text_factory.py | 28 |
11 files changed, 53 insertions, 50 deletions
diff --git a/Doc/includes/sqlite3/adapter_datetime.py b/Doc/includes/sqlite3/adapter_datetime.py index 5869e22..be33395 100644 --- a/Doc/includes/sqlite3/adapter_datetime.py +++ b/Doc/includes/sqlite3/adapter_datetime.py @@ -1,5 +1,6 @@ import sqlite3 -import datetime, time +import datetime +import time def adapt_datetime(ts): return time.mktime(ts.timetuple()) diff --git a/Doc/includes/sqlite3/converter_point.py b/Doc/includes/sqlite3/converter_point.py index a8861bc..5df828e 100644 --- a/Doc/includes/sqlite3/converter_point.py +++ b/Doc/includes/sqlite3/converter_point.py @@ -8,10 +8,10 @@ class Point: return "(%f;%f)" % (self.x, self.y) def adapt_point(point): - return "%f;%f" % (point.x, point.y) + return ("%f;%f" % (point.x, point.y)).encode('ascii') def convert_point(s): - x, y = list(map(float, s.split(";"))) + x, y = list(map(float, s.split(b";"))) return Point(x, y) # Register the adapter diff --git a/Doc/includes/sqlite3/ctx_manager.py b/Doc/includes/sqlite3/ctx_manager.py index b8e4332..7af4ad1 100644 --- a/Doc/includes/sqlite3/ctx_manager.py +++ b/Doc/includes/sqlite3/ctx_manager.py @@ -8,7 +8,7 @@ with con: 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 catched +# exception is still raised and must be caught try: with con: con.execute("insert into person(firstname) values (?)", ("Joe",)) diff --git a/Doc/includes/sqlite3/execute_1.py b/Doc/includes/sqlite3/execute_1.py index 3d08840..f864a89 100644 --- a/Doc/includes/sqlite3/execute_1.py +++ b/Doc/includes/sqlite3/execute_1.py @@ -1,11 +1,16 @@ import sqlite3 -con = sqlite3.connect("mydb") - +con = sqlite3.connect(":memory:") cur = con.cursor() +cur.execute("create table people (name_last, age)") who = "Yeltsin" age = 72 -cur.execute("select name_last, age from people where name_last=? and age=?", (who, age)) +# This is the qmark style: +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}) + print(cur.fetchone()) diff --git a/Doc/includes/sqlite3/execute_2.py b/Doc/includes/sqlite3/execute_2.py deleted file mode 100644 index 84734f9..0000000 --- a/Doc/includes/sqlite3/execute_2.py +++ /dev/null @@ -1,12 +0,0 @@ -import sqlite3 - -con = sqlite3.connect("mydb") - -cur = con.cursor() - -who = "Yeltsin" -age = 72 - -cur.execute("select name_last, age from people where name_last=:who and age=:age", - {"who": who, "age": age}) -print(cur.fetchone()) diff --git a/Doc/includes/sqlite3/executemany_2.py b/Doc/includes/sqlite3/executemany_2.py index 518cd94..527358e 100644 --- a/Doc/includes/sqlite3/executemany_2.py +++ b/Doc/includes/sqlite3/executemany_2.py @@ -1,8 +1,8 @@ import sqlite3 +import string def char_generator(): - import string - for c in string.letters[:26]: + for c in string.ascii_lowercase: yield (c,) con = sqlite3.connect(":memory:") diff --git a/Doc/includes/sqlite3/load_extension.py b/Doc/includes/sqlite3/load_extension.py new file mode 100644 index 0000000..015aa0d --- /dev/null +++ b/Doc/includes/sqlite3/load_extension.py @@ -0,0 +1,26 @@ +import sqlite3 + +con = sqlite3.connect(":memory:") + +# enable extension loading +con.enable_load_extension(True) + +# Load the fulltext search extension +con.execute("select load_extension('./fts3.so')") + +# alternatively you can load the extension using an API call: +# con.load_extension("./fts3.so") + +# disable extension laoding again +con.enable_load_extension(False) + +# example from SQLite wiki +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'); + """) +for row in con.execute("select rowid, name, ingredients from recipe where name match 'pie'"): + print(row) diff --git a/Doc/includes/sqlite3/md5func.py b/Doc/includes/sqlite3/md5func.py index b7bc05b..0056b2d 100644 --- a/Doc/includes/sqlite3/md5func.py +++ b/Doc/includes/sqlite3/md5func.py @@ -7,5 +7,5 @@ def md5sum(t): con = sqlite3.connect(":memory:") con.create_function("md5", 1, md5sum) cur = con.cursor() -cur.execute("select md5(?)", ("foo",)) +cur.execute("select md5(?)", (b"foo",)) print(cur.fetchone()[0]) diff --git a/Doc/includes/sqlite3/rowclass.py b/Doc/includes/sqlite3/rowclass.py index 3fa0b87..92b5ad6 100644 --- a/Doc/includes/sqlite3/rowclass.py +++ b/Doc/includes/sqlite3/rowclass.py @@ -1,12 +1,12 @@ import sqlite3 -con = sqlite3.connect("mydb") +con = sqlite3.connect(":memory:") con.row_factory = sqlite3.Row cur = con.cursor() -cur.execute("select name_last, age from people") +cur.execute("select 'John' as name, 42 as age") for row in cur: - assert row[0] == row["name_last"] - assert row["name_last"] == row["nAmE_lAsT"] + assert row[0] == row["name"] + assert row["name"] == row["nAmE"] assert row[1] == row["age"] assert row[1] == row["AgE"] diff --git a/Doc/includes/sqlite3/shortcut_methods.py b/Doc/includes/sqlite3/shortcut_methods.py index 596d87c..71600d4 100644 --- a/Doc/includes/sqlite3/shortcut_methods.py +++ b/Doc/includes/sqlite3/shortcut_methods.py @@ -17,5 +17,4 @@ con.executemany("insert into person(firstname, lastname) values (?, ?)", persons for row in con.execute("select firstname, lastname from person"): print(row) -# Using a dummy WHERE clause to not let SQLite take the shortcut table deletes. -print("I just deleted", con.execute("delete from person where 1=1").rowcount, "rows") +print("I just deleted", con.execute("delete from person").rowcount, "rows") diff --git a/Doc/includes/sqlite3/text_factory.py b/Doc/includes/sqlite3/text_factory.py index 22c2970..5f96cdb 100644 --- a/Doc/includes/sqlite3/text_factory.py +++ b/Doc/includes/sqlite3/text_factory.py @@ -3,9 +3,6 @@ import sqlite3 con = sqlite3.connect(":memory:") cur = con.cursor() -# Create the table -con.execute("create table person(lastname, firstname)") - AUSTRIA = "\xd6sterreich" # by default, rows are returned as Unicode @@ -14,30 +11,17 @@ row = cur.fetchone() assert row[0] == AUSTRIA # but we can make sqlite3 always return bytestrings ... -con.text_factory = str +con.text_factory = bytes cur.execute("select ?", (AUSTRIA,)) row = cur.fetchone() -assert type(row[0]) == str +assert type(row[0]) is bytes # the bytestrings 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 will ignore Unicode characters that cannot be -# decoded from UTF-8 -con.text_factory = lambda x: str(x, "utf-8", "ignore") -cur.execute("select ?", ("this is latin1 and would normally create errors" + - "\xe4\xf6\xfc".encode("latin1"),)) -row = cur.fetchone() -assert type(row[0]) == str - -# sqlite3 offers a built-in optimized text_factory that will return bytestring -# objects, if the data is in ASCII only, and otherwise return unicode objects -con.text_factory = sqlite3.OptimizedUnicode -cur.execute("select ?", (AUSTRIA,)) -row = cur.fetchone() -assert type(row[0]) == str - -cur.execute("select ?", ("Germany",)) +# here we implement one that appends "foo" to all strings +con.text_factory = lambda x: x.decode("utf-8") + "foo" +cur.execute("select ?", ("bar",)) row = cur.fetchone() -assert type(row[0]) == str +assert row[0] == "barfoo" |
