diff options
Diffstat (limited to 'Doc/includes/sqlite3')
28 files changed, 84 insertions, 106 deletions
diff --git a/Doc/includes/sqlite3/adapter_datetime.py b/Doc/includes/sqlite3/adapter_datetime.py index d5221d8..3460498 100644 --- a/Doc/includes/sqlite3/adapter_datetime.py +++ b/Doc/includes/sqlite3/adapter_datetime.py @@ -1,6 +1,5 @@ import sqlite3 -import datetime -import time +import datetime, time def adapt_datetime(ts): return time.mktime(ts.timetuple()) @@ -12,6 +11,4 @@ cur = con.cursor() now = datetime.datetime.now() cur.execute("select ?", (now,)) -print(cur.fetchone()[0]) - -con.close() +print cur.fetchone()[0] diff --git a/Doc/includes/sqlite3/adapter_point_1.py b/Doc/includes/sqlite3/adapter_point_1.py index 77daf8f..a741f6c 100644 --- a/Doc/includes/sqlite3/adapter_point_1.py +++ b/Doc/includes/sqlite3/adapter_point_1.py @@ -1,6 +1,6 @@ import sqlite3 -class Point: +class Point(object): def __init__(self, x, y): self.x, self.y = x, y @@ -13,6 +13,4 @@ cur = con.cursor() p = Point(4.0, -3.2) cur.execute("select ?", (p,)) -print(cur.fetchone()[0]) - -con.close() +print cur.fetchone()[0] diff --git a/Doc/includes/sqlite3/adapter_point_2.py b/Doc/includes/sqlite3/adapter_point_2.py index cb86331..200a064 100644 --- a/Doc/includes/sqlite3/adapter_point_2.py +++ b/Doc/includes/sqlite3/adapter_point_2.py @@ -1,6 +1,6 @@ import sqlite3 -class Point: +class Point(object): def __init__(self, x, y): self.x, self.y = x, y @@ -14,6 +14,4 @@ cur = con.cursor() p = Point(4.0, -3.2) cur.execute("select ?", (p,)) -print(cur.fetchone()[0]) - -con.close() +print cur.fetchone()[0] diff --git a/Doc/includes/sqlite3/collation_reverse.py b/Doc/includes/sqlite3/collation_reverse.py index 3504a35..e956402 100644 --- a/Doc/includes/sqlite3/collation_reverse.py +++ b/Doc/includes/sqlite3/collation_reverse.py @@ -1,12 +1,7 @@ import sqlite3 def collate_reverse(string1, string2): - if string1 == string2: - return 0 - elif string1 < string2: - return 1 - else: - return -1 + return -cmp(string1, string2) con = sqlite3.connect(":memory:") con.create_collation("reverse", collate_reverse) @@ -16,5 +11,5 @@ 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) + print row con.close() diff --git a/Doc/includes/sqlite3/complete_statement.py b/Doc/includes/sqlite3/complete_statement.py index cd38d73..76ea7f6 100644 --- a/Doc/includes/sqlite3/complete_statement.py +++ b/Doc/includes/sqlite3/complete_statement.py @@ -8,11 +8,11 @@ cur = con.cursor() buffer = "" -print("Enter your SQL commands to execute in sqlite3.") -print("Enter a blank line to exit.") +print "Enter your SQL commands to execute in sqlite3." +print "Enter a blank line to exit." while True: - line = input() + line = raw_input() if line == "": break buffer += line @@ -22,9 +22,9 @@ while True: cur.execute(buffer) if buffer.lstrip().upper().startswith("SELECT"): - print(cur.fetchall()) + print cur.fetchall() except sqlite3.Error as e: - print("An error occurred:", e.args[0]) + print "An error occurred:", e.args[0] buffer = "" con.close() diff --git a/Doc/includes/sqlite3/connect_db_1.py b/Doc/includes/sqlite3/connect_db_1.py new file mode 100644 index 0000000..1b97523 --- /dev/null +++ b/Doc/includes/sqlite3/connect_db_1.py @@ -0,0 +1,3 @@ +import sqlite3 + +con = sqlite3.connect("mydb") diff --git a/Doc/includes/sqlite3/connect_db_2.py b/Doc/includes/sqlite3/connect_db_2.py new file mode 100644 index 0000000..f9728b36 --- /dev/null +++ b/Doc/includes/sqlite3/connect_db_2.py @@ -0,0 +1,3 @@ +import sqlite3 + +con = sqlite3.connect(":memory:") diff --git a/Doc/includes/sqlite3/converter_point.py b/Doc/includes/sqlite3/converter_point.py index 5df828e..e220e9b 100644 --- a/Doc/includes/sqlite3/converter_point.py +++ b/Doc/includes/sqlite3/converter_point.py @@ -1,6 +1,6 @@ import sqlite3 -class Point: +class Point(object): def __init__(self, x, y): self.x, self.y = x, y @@ -8,10 +8,10 @@ class Point: return "(%f;%f)" % (self.x, self.y) def adapt_point(point): - return ("%f;%f" % (point.x, point.y)).encode('ascii') + return "%f;%f" % (point.x, point.y) def convert_point(s): - x, y = list(map(float, s.split(b";"))) + x, y = map(float, s.split(";")) return Point(x, y) # Register the adapter @@ -30,7 +30,7 @@ cur.execute("create table test(p point)") cur.execute("insert into test(p) values (?)", (p,)) cur.execute("select p from test") -print("with declared types:", cur.fetchone()[0]) +print "with declared types:", cur.fetchone()[0] cur.close() con.close() @@ -42,6 +42,6 @@ cur.execute("create table test(p)") cur.execute("insert into test(p) values (?)", (p,)) cur.execute('select p as "p [point]" from test') -print("with column names:", cur.fetchone()[0]) +print "with column names:", cur.fetchone()[0] cur.close() con.close() diff --git a/Doc/includes/sqlite3/countcursors.py b/Doc/includes/sqlite3/countcursors.py index 112f477..df04cad 100644 --- a/Doc/includes/sqlite3/countcursors.py +++ b/Doc/includes/sqlite3/countcursors.py @@ -12,6 +12,4 @@ class CountCursorsConnection(sqlite3.Connection): con = sqlite3.connect(":memory:", factory=CountCursorsConnection) cur1 = con.cursor() cur2 = con.cursor() -print(con.numcursors) - -con.close() +print con.numcursors diff --git a/Doc/includes/sqlite3/ctx_manager.py b/Doc/includes/sqlite3/ctx_manager.py index 6db77d4..d6f27e6 100644 --- a/Doc/includes/sqlite3/ctx_manager.py +++ b/Doc/includes/sqlite3/ctx_manager.py @@ -13,8 +13,4 @@ try: with con: con.execute("insert into person(firstname) values (?)", ("Joe",)) except sqlite3.IntegrityError: - print("couldn't add Joe twice") - -# Connection object used as context manager only commits or rollbacks transactions, -# so the connection object should be closed manually -con.close() + print "couldn't add Joe twice" diff --git a/Doc/includes/sqlite3/execsql_fetchonerow.py b/Doc/includes/sqlite3/execsql_fetchonerow.py index 115bcb5..8044ecf 100644 --- a/Doc/includes/sqlite3/execsql_fetchonerow.py +++ b/Doc/includes/sqlite3/execsql_fetchonerow.py @@ -9,11 +9,9 @@ SELECT = "select name_last, age from people order by age, name_last" # resulting sequences to yield their elements (name_last, age): cur.execute(SELECT) for (name_last, age) in cur: - print('%s is %d years old.' % (name_last, age)) + print '%s is %d years old.' % (name_last, age) # 2. Equivalently: cur.execute(SELECT) for row in cur: - print('%s is %d years old.' % (row[0], row[1])) - -con.close() + print '%s is %d years old.' % (row[0], row[1]) diff --git a/Doc/includes/sqlite3/execsql_printall_1.py b/Doc/includes/sqlite3/execsql_printall_1.py index 19306e6..d27d735 100644 --- a/Doc/includes/sqlite3/execsql_printall_1.py +++ b/Doc/includes/sqlite3/execsql_printall_1.py @@ -10,6 +10,4 @@ cur = con.cursor() cur.execute("select * from people order by age") # Retrieve all rows as a sequence and print that sequence: -print(cur.fetchall()) - -con.close() +print cur.fetchall() diff --git a/Doc/includes/sqlite3/execute_1.py b/Doc/includes/sqlite3/execute_1.py index 3466b12..763167c 100644 --- a/Doc/includes/sqlite3/execute_1.py +++ b/Doc/includes/sqlite3/execute_1.py @@ -13,6 +13,4 @@ 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()) - -con.close() +print cur.fetchone() diff --git a/Doc/includes/sqlite3/execute_3.py b/Doc/includes/sqlite3/execute_3.py new file mode 100644 index 0000000..b64621f --- /dev/null +++ b/Doc/includes/sqlite3/execute_3.py @@ -0,0 +1,12 @@ +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", + locals()) +print cur.fetchone() diff --git a/Doc/includes/sqlite3/executemany_1.py b/Doc/includes/sqlite3/executemany_1.py index edf6f8b..24357c5 100644 --- a/Doc/includes/sqlite3/executemany_1.py +++ b/Doc/includes/sqlite3/executemany_1.py @@ -7,7 +7,7 @@ class IterChars: def __iter__(self): return self - def __next__(self): + def next(self): if self.count > ord('z'): raise StopIteration self.count += 1 @@ -21,6 +21,4 @@ theIter = IterChars() cur.executemany("insert into characters(c) values (?)", theIter) cur.execute("select c from characters") -print(cur.fetchall()) - -con.close() +print cur.fetchall() diff --git a/Doc/includes/sqlite3/executemany_2.py b/Doc/includes/sqlite3/executemany_2.py index 02a594c..0b12688 100644 --- a/Doc/includes/sqlite3/executemany_2.py +++ b/Doc/includes/sqlite3/executemany_2.py @@ -2,7 +2,7 @@ import sqlite3 import string def char_generator(): - for c in string.ascii_lowercase: + for c in string.lowercase: yield (c,) con = sqlite3.connect(":memory:") @@ -12,6 +12,4 @@ cur.execute("create table characters(c)") cur.executemany("insert into characters(c) values (?)", char_generator()) cur.execute("select c from characters") -print(cur.fetchall()) - -con.close() +print cur.fetchall() diff --git a/Doc/includes/sqlite3/executescript.py b/Doc/includes/sqlite3/executescript.py index aea8943..7e53581 100644 --- a/Doc/includes/sqlite3/executescript.py +++ b/Doc/includes/sqlite3/executescript.py @@ -22,4 +22,3 @@ cur.executescript(""" 1987 ); """) -con.close() diff --git a/Doc/includes/sqlite3/insert_more_people.py b/Doc/includes/sqlite3/insert_more_people.py index 10cf937..edbc79e 100644 --- a/Doc/includes/sqlite3/insert_more_people.py +++ b/Doc/includes/sqlite3/insert_more_people.py @@ -14,5 +14,3 @@ for person in newPeople: # The changes will not be saved unless the transaction is committed explicitly: con.commit() - -con.close() diff --git a/Doc/includes/sqlite3/load_extension.py b/Doc/includes/sqlite3/load_extension.py index 624cfe2..f1a92b3 100644 --- a/Doc/includes/sqlite3/load_extension.py +++ b/Doc/includes/sqlite3/load_extension.py @@ -23,6 +23,4 @@ con.executescript(""" 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) - -con.close() + print row diff --git a/Doc/includes/sqlite3/md5func.py b/Doc/includes/sqlite3/md5func.py index 16dc348..5769687 100644 --- a/Doc/includes/sqlite3/md5func.py +++ b/Doc/includes/sqlite3/md5func.py @@ -1,13 +1,11 @@ import sqlite3 -import hashlib +import md5 def md5sum(t): - return hashlib.md5(t).hexdigest() + return md5.md5(t).hexdigest() con = sqlite3.connect(":memory:") con.create_function("md5", 1, md5sum) cur = con.cursor() -cur.execute("select md5(?)", (b"foo",)) -print(cur.fetchone()[0]) - -con.close() +cur.execute("select md5(?)", ("foo",)) +print cur.fetchone()[0] diff --git a/Doc/includes/sqlite3/mysumaggr.py b/Doc/includes/sqlite3/mysumaggr.py index 11f9639..6d0cd55 100644 --- a/Doc/includes/sqlite3/mysumaggr.py +++ b/Doc/includes/sqlite3/mysumaggr.py @@ -17,6 +17,4 @@ 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]) - -con.close() +print cur.fetchone()[0] diff --git a/Doc/includes/sqlite3/parse_colnames.py b/Doc/includes/sqlite3/parse_colnames.py index 5f01dbf..fcded00 100644 --- a/Doc/includes/sqlite3/parse_colnames.py +++ b/Doc/includes/sqlite3/parse_colnames.py @@ -5,6 +5,4 @@ con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_COLNAMES) cur = con.cursor() cur.execute('select ? as "x [timestamp]"', (datetime.datetime.now(),)) dt = cur.fetchone()[0] -print(dt, type(dt)) - -con.close() +print dt, type(dt) diff --git a/Doc/includes/sqlite3/pysqlite_datetime.py b/Doc/includes/sqlite3/pysqlite_datetime.py index 5d843f9..efa4b06 100644 --- a/Doc/includes/sqlite3/pysqlite_datetime.py +++ b/Doc/includes/sqlite3/pysqlite_datetime.py @@ -11,12 +11,10 @@ now = datetime.datetime.now() 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])) +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]"') row = cur.fetchone() -print("current_date", row[0], type(row[0])) -print("current_timestamp", row[1], type(row[1])) - -con.close() +print "current_date", row[0], type(row[0]) +print "current_timestamp", row[1], type(row[1]) diff --git a/Doc/includes/sqlite3/row_factory.py b/Doc/includes/sqlite3/row_factory.py index 9de6e7b..64676c8 100644 --- a/Doc/includes/sqlite3/row_factory.py +++ b/Doc/includes/sqlite3/row_factory.py @@ -10,6 +10,4 @@ con = sqlite3.connect(":memory:") con.row_factory = dict_factory cur = con.cursor() cur.execute("select 1 as a") -print(cur.fetchone()["a"]) - -con.close() +print cur.fetchone()["a"] diff --git a/Doc/includes/sqlite3/rowclass.py b/Doc/includes/sqlite3/rowclass.py index fc60287..92b5ad6 100644 --- a/Doc/includes/sqlite3/rowclass.py +++ b/Doc/includes/sqlite3/rowclass.py @@ -10,5 +10,3 @@ for row in cur: assert row["name"] == row["nAmE"] assert row[1] == row["age"] assert row[1] == row["AgE"] - -con.close() diff --git a/Doc/includes/sqlite3/shortcut_methods.py b/Doc/includes/sqlite3/shortcut_methods.py index 98a3941..e128a3b 100644 --- a/Doc/includes/sqlite3/shortcut_methods.py +++ b/Doc/includes/sqlite3/shortcut_methods.py @@ -15,10 +15,6 @@ con.executemany("insert into person(firstname, lastname) values (?, ?)", persons # Print the table contents for row in con.execute("select firstname, lastname from person"): - print(row) + print row -print("I just deleted", con.execute("delete from person").rowcount, "rows") - -# close is not a shortcut method and it's not called automatically, -# so the connection object should be closed manually -con.close() +print "I just deleted", con.execute("delete from person").rowcount, "rows" diff --git a/Doc/includes/sqlite3/simple_tableprinter.py b/Doc/includes/sqlite3/simple_tableprinter.py index 148a170..67ea6a2 100644 --- a/Doc/includes/sqlite3/simple_tableprinter.py +++ b/Doc/includes/sqlite3/simple_tableprinter.py @@ -11,9 +11,9 @@ cur.execute(SELECT) # Print a header. for fieldDesc in cur.description: - print(fieldDesc[0].ljust(FIELD_MAX_WIDTH), end=' ') -print() # Finish the header with a newline. -print('-' * 78) + print fieldDesc[0].ljust(FIELD_MAX_WIDTH) , +print # Finish the header with a newline. +print '-' * 78 # For each row, print the value of each field left-justified within # the maximum possible width of that field. @@ -21,8 +21,6 @@ fieldIndices = range(len(cur.description)) for row in cur: for fieldIndex in fieldIndices: fieldValue = str(row[fieldIndex]) - print(fieldValue.ljust(FIELD_MAX_WIDTH), end=' ') + print fieldValue.ljust(FIELD_MAX_WIDTH) , - print() # Finish the row with a newline. - -con.close() + print # Finish the row with a newline. diff --git a/Doc/includes/sqlite3/text_factory.py b/Doc/includes/sqlite3/text_factory.py index a857a15..577378f 100644 --- a/Doc/includes/sqlite3/text_factory.py +++ b/Doc/includes/sqlite3/text_factory.py @@ -3,7 +3,7 @@ import sqlite3 con = sqlite3.connect(":memory:") cur = con.cursor() -AUSTRIA = "\xd6sterreich" +AUSTRIA = u"\xd6sterreich" # by default, rows are returned as Unicode cur.execute("select ?", (AUSTRIA,)) @@ -11,19 +11,30 @@ row = cur.fetchone() assert row[0] == AUSTRIA # but we can make sqlite3 always return bytestrings ... -con.text_factory = bytes +con.text_factory = str cur.execute("select ?", (AUSTRIA,)) row = cur.fetchone() -assert type(row[0]) is bytes +assert type(row[0]) is str # 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 appends "foo" to all strings -con.text_factory = lambda x: x.decode("utf-8") + "foo" -cur.execute("select ?", ("bar",)) +# here we implement one that will ignore Unicode characters that cannot be +# decoded from UTF-8 +con.text_factory = lambda x: unicode(x, "utf-8", "ignore") +cur.execute("select ?", ("this is latin1 and would normally create errors" + + u"\xe4\xf6\xfc".encode("latin1"),)) row = cur.fetchone() -assert row[0] == "barfoo" +assert type(row[0]) is unicode -con.close() +# 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]) is unicode + +cur.execute("select ?", ("Germany",)) +row = cur.fetchone() +assert type(row[0]) is str |