summaryrefslogtreecommitdiff
path: root/examples/converters.py
blob: 7a34cb76cb043273b7926e1c28c4436eea1ad0a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
import sqlite

# Ok, let's define a user-defined type we can use with the SQLite database
class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y

    # The _quote function is currently the way a PySQLite user-defined type
    # returns its string representation to write to the database.
    def _quote(self):
        return "'%f,%f'" % (self.x, self.y)

    def __str__(self):
        return "Point(%f, %f)" % (self.x, self.y)

# The conversion callable needs to accept a string, parse it and return an
# instance of your user-defined type.
def pointConverter(s):
    x, y = s.split(",")
    return Point(float(x), float(y))

# Ensure we have an empty database
if os.path.exists("db"): os.remove("db")

cx = sqlite.connect("db", converters={"point": pointConverter}) 
cu = cx.cursor()
cu.execute("create table test(p point, n int)")
cu.execute("insert into test(p, n) values (%s, %s)", (Point(-3.2, 4.5), 25))

# For user-defined types, and for statements which return anything but direct
# columns, you need to use the "-- types" feature of PySQLite:
cu.execute("-- types point, int")
cu.execute("select p, n from test")
row = cu.fetchone()

print "p:", row.p       # .columnname instead of [0] is a PySQLite
print "n:", row.n       # extension to the DB-API! 
cx.close()