mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-02-12 07:10:33 +03:00
psycopg->psycopg2 switch in the examples.
This commit is contained in:
parent
0c56bc95df
commit
9c284f4570
|
@ -19,13 +19,13 @@ DSN = 'dbname=test'
|
||||||
## don't modify anything below tis line (except for experimenting)
|
## don't modify anything below tis line (except for experimenting)
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import psycopg
|
import psycopg2
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
DSN = sys.argv[1]
|
DSN = sys.argv[1]
|
||||||
|
|
||||||
print "Opening connection using dns:", DSN
|
print "Opening connection using dns:", DSN
|
||||||
conn = psycopg.connect(DSN)
|
conn = psycopg2.connect(DSN)
|
||||||
print "Encoding for this connection is", conn.encoding
|
print "Encoding for this connection is", conn.encoding
|
||||||
|
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
|
@ -41,7 +41,7 @@ conn.commit()
|
||||||
# using a buffer on a file object.
|
# using a buffer on a file object.
|
||||||
|
|
||||||
data1 = {'id':1, 'name':'somehackers.jpg',
|
data1 = {'id':1, 'name':'somehackers.jpg',
|
||||||
'img':psycopg.Binary(open('somehackers.jpg').read())}
|
'img':psycopg2.Binary(open('somehackers.jpg').read())}
|
||||||
data2 = {'id':2, 'name':'whereareyou.jpg',
|
data2 = {'id':2, 'name':'whereareyou.jpg',
|
||||||
'img':buffer(open('whereareyou.jpg').read())}
|
'img':buffer(open('whereareyou.jpg').read())}
|
||||||
|
|
||||||
|
|
|
@ -23,13 +23,13 @@ DSN = 'dbname=test'
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import StringIO
|
import StringIO
|
||||||
import psycopg
|
import psycopg2
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
DSN = sys.argv[1]
|
DSN = sys.argv[1]
|
||||||
|
|
||||||
print "Opening connection using dns:", DSN
|
print "Opening connection using dns:", DSN
|
||||||
conn = psycopg.connect(DSN)
|
conn = psycopg2.connect(DSN)
|
||||||
print "Encoding for this connection is", conn.encoding
|
print "Encoding for this connection is", conn.encoding
|
||||||
|
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
|
|
|
@ -23,13 +23,13 @@ DSN = 'dbname=test'
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import StringIO
|
import StringIO
|
||||||
import psycopg
|
import psycopg2
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
DSN = sys.argv[1]
|
DSN = sys.argv[1]
|
||||||
|
|
||||||
print "Opening connection using dns:", DSN
|
print "Opening connection using dns:", DSN
|
||||||
conn = psycopg.connect(DSN)
|
conn = psycopg2.connect(DSN)
|
||||||
print "Encoding for this connection is", conn.encoding
|
print "Encoding for this connection is", conn.encoding
|
||||||
|
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
|
|
|
@ -19,22 +19,22 @@ DSN = 'dbname=test'
|
||||||
## don't modify anything below this line (except for experimenting)
|
## don't modify anything below this line (except for experimenting)
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import psycopg
|
import psycopg2
|
||||||
import psycopg.extensions
|
import psycopg2.extensions
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
DSN = sys.argv[1]
|
DSN = sys.argv[1]
|
||||||
|
|
||||||
print "Opening connection using dsn:", DSN
|
print "Opening connection using dsn:", DSN
|
||||||
conn = psycopg.connect(DSN)
|
conn = psycopg2.connect(DSN)
|
||||||
print "Encoding for this connection is", conn.encoding
|
print "Encoding for this connection is", conn.encoding
|
||||||
|
|
||||||
|
|
||||||
class NoDataError(psycopg.ProgrammingError):
|
class NoDataError(psycopg2.ProgrammingError):
|
||||||
"""Exception that will be raised by our cursor."""
|
"""Exception that will be raised by our cursor."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class Cursor(psycopg.extensions.cursor):
|
class Cursor(psycopg2.extensions.cursor):
|
||||||
"""A custom cursor."""
|
"""A custom cursor."""
|
||||||
|
|
||||||
def fetchone(self):
|
def fetchone(self):
|
||||||
|
@ -45,7 +45,7 @@ class Cursor(psycopg.extensions.cursor):
|
||||||
uses the same function to fetch rows, the code path from Python is
|
uses the same function to fetch rows, the code path from Python is
|
||||||
different.
|
different.
|
||||||
"""
|
"""
|
||||||
d = psycopg.extensions.cursor.fetchone(self)
|
d = psycopg2.extensions.cursor.fetchone(self)
|
||||||
if d is None:
|
if d is None:
|
||||||
raise NoDataError("no more data")
|
raise NoDataError("no more data")
|
||||||
return d
|
return d
|
||||||
|
|
|
@ -14,8 +14,8 @@ intrusive for your classes (don't want inheritance from an 'Item' or
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
import psycopg
|
import psycopg2
|
||||||
from psycopg.extensions import adapt, register_adapter
|
from psycopg2.extensions import adapt, register_adapter
|
||||||
|
|
||||||
try: sorted()
|
try: sorted()
|
||||||
except NameError:
|
except NameError:
|
||||||
|
|
|
@ -19,18 +19,18 @@ DSN = 'dbname=test'
|
||||||
## don't modify anything below this line (except for experimenting)
|
## don't modify anything below this line (except for experimenting)
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import psycopg
|
import psycopg2
|
||||||
import psycopg.extras
|
import psycopg2.extras
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
DSN = sys.argv[1]
|
DSN = sys.argv[1]
|
||||||
|
|
||||||
print "Opening connection using dsn:", DSN
|
print "Opening connection using dsn:", DSN
|
||||||
conn = psycopg.connect(DSN)
|
conn = psycopg2.connect(DSN)
|
||||||
print "Encoding for this connection is", conn.encoding
|
print "Encoding for this connection is", conn.encoding
|
||||||
|
|
||||||
|
|
||||||
curs = conn.cursor(cursor_factory=psycopg.extras.DictCursor)
|
curs = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
|
||||||
curs.execute("SELECT 1 AS foo, 'cip' AS bar, date(now()) as zot")
|
curs.execute("SELECT 1 AS foo, 'cip' AS bar, date(now()) as zot")
|
||||||
|
|
||||||
data = curs.fetchone()
|
data = curs.fetchone()
|
||||||
|
|
|
@ -18,7 +18,7 @@ DSN = 'dbname=test'
|
||||||
|
|
||||||
## don't modify anything below tis line (except for experimenting)
|
## don't modify anything below tis line (except for experimenting)
|
||||||
|
|
||||||
import sys, psycopg
|
import sys, psycopg2
|
||||||
import mx.DateTime
|
import mx.DateTime
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ if len(sys.argv) > 1:
|
||||||
DSN = sys.argv[1]
|
DSN = sys.argv[1]
|
||||||
|
|
||||||
print "Opening connection using dns:", DSN
|
print "Opening connection using dns:", DSN
|
||||||
conn = psycopg.connect(DSN)
|
conn = psycopg2.connect(DSN)
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -19,63 +19,63 @@ DSN = 'dbname=test'
|
||||||
|
|
||||||
## don't modify anything below this line (except for experimenting)
|
## don't modify anything below this line (except for experimenting)
|
||||||
|
|
||||||
import sys, psycopg
|
import sys, psycopg2
|
||||||
import psycopg.extensions
|
import psycopg.extensions
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
DSN = sys.argv[1]
|
DSN = sys.argv[1]
|
||||||
|
|
||||||
print "Opening connection using dns:", DSN
|
print "Opening connection using dns:", DSN
|
||||||
conn = psycopg.connect(DSN)
|
conn = psycopg2.connect(DSN)
|
||||||
print "Initial encoding for this connection is", conn.encoding
|
print "Initial encoding for this connection is", conn.encoding
|
||||||
|
|
||||||
print "\n** This example is supposed to be run in a UNICODE terminal! **\n"
|
print "\n** This example is supposed to be run in a UNICODE terminal! **\n"
|
||||||
|
|
||||||
print "Available encodings:"
|
print "Available encodings:"
|
||||||
for a, b in psycopg.extensions.encodings.items():
|
for a, b in psycopg2.extensions.encodings.items():
|
||||||
print " ", a, "<->", b
|
print " ", a, "<->", b
|
||||||
|
|
||||||
print "Using STRING typecaster"
|
print "Using STRING typecaster"
|
||||||
print "Setting backend encoding to LATIN1 and executing queries:"
|
print "Setting backend encoding to LATIN1 and executing queries:"
|
||||||
conn.set_client_encoding('LATIN1')
|
conn.set_client_encoding('LATIN1')
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
curs.execute("SELECT %s::TEXT AS foo", ('àèìòù',))
|
curs.execute("SELECT %s::TEXT AS foo", ('<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>',))
|
||||||
x = curs.fetchone()[0]
|
x = curs.fetchone()[0]
|
||||||
print " ->", unicode(x, 'latin-1').encode('utf-8'), type(x)
|
print " ->", unicode(x, 'latin-1').encode('utf-8'), type(x)
|
||||||
curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',))
|
curs.execute("SELECT %s::TEXT AS foo", (u'<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>',))
|
||||||
x = curs.fetchone()[0]
|
x = curs.fetchone()[0]
|
||||||
print " ->", unicode(x, 'latin-1').encode('utf-8'), type(x)
|
print " ->", unicode(x, 'latin-1').encode('utf-8'), type(x)
|
||||||
|
|
||||||
print "Setting backend encoding to UTF8 and executing queries:"
|
print "Setting backend encoding to UTF8 and executing queries:"
|
||||||
conn.set_client_encoding('UNICODE')
|
conn.set_client_encoding('UNICODE')
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù'.encode('utf-8'),))
|
curs.execute("SELECT %s::TEXT AS foo", (u'<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>'.encode('utf-8'),))
|
||||||
x = curs.fetchone()[0]
|
x = curs.fetchone()[0]
|
||||||
print " ->", x, type(x)
|
print " ->", x, type(x)
|
||||||
curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',))
|
curs.execute("SELECT %s::TEXT AS foo", (u'<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>',))
|
||||||
x = curs.fetchone()[0]
|
x = curs.fetchone()[0]
|
||||||
print " ->", x, type(x)
|
print " ->", x, type(x)
|
||||||
|
|
||||||
print "Using UNICODE typecaster"
|
print "Using UNICODE typecaster"
|
||||||
psycopg.extensions.register_type(psycopg.extensions.UNICODE)
|
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
|
||||||
|
|
||||||
print "Setting backend encoding to LATIN1 and executing queries:"
|
print "Setting backend encoding to LATIN1 and executing queries:"
|
||||||
conn.set_client_encoding('LATIN1')
|
conn.set_client_encoding('LATIN1')
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
curs.execute("SELECT %s::TEXT AS foo", ('àèìòù',))
|
curs.execute("SELECT %s::TEXT AS foo", ('<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>',))
|
||||||
x = curs.fetchone()[0]
|
x = curs.fetchone()[0]
|
||||||
print " ->", x.encode('utf-8'), ":", type(x)
|
print " ->", x.encode('utf-8'), ":", type(x)
|
||||||
curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',))
|
curs.execute("SELECT %s::TEXT AS foo", (u'<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>',))
|
||||||
x = curs.fetchone()[0]
|
x = curs.fetchone()[0]
|
||||||
print " ->", x.encode('utf-8'), ":", type(x)
|
print " ->", x.encode('utf-8'), ":", type(x)
|
||||||
|
|
||||||
print "Setting backend encoding to UTF8 and executing queries:"
|
print "Setting backend encoding to UTF8 and executing queries:"
|
||||||
conn.set_client_encoding('UNICODE')
|
conn.set_client_encoding('UNICODE')
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù'.encode('utf-8'),))
|
curs.execute("SELECT %s::TEXT AS foo", (u'<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>'.encode('utf-8'),))
|
||||||
x = curs.fetchone()[0]
|
x = curs.fetchone()[0]
|
||||||
print " ->", x.encode('utf-8'), ":", type(x)
|
print " ->", x.encode('utf-8'), ":", type(x)
|
||||||
curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',))
|
curs.execute("SELECT %s::TEXT AS foo", (u'<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>',))
|
||||||
x = curs.fetchone()[0]
|
x = curs.fetchone()[0]
|
||||||
print " ->", x.encode('utf-8'), ":", type(x)
|
print " ->", x.encode('utf-8'), ":", type(x)
|
||||||
|
|
||||||
|
@ -84,19 +84,19 @@ print "Executing full UNICODE queries"
|
||||||
print "Setting backend encoding to LATIN1 and executing queries:"
|
print "Setting backend encoding to LATIN1 and executing queries:"
|
||||||
conn.set_client_encoding('LATIN1')
|
conn.set_client_encoding('LATIN1')
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
curs.execute(u"SELECT %s::TEXT AS foo", ('àèìòù',))
|
curs.execute(u"SELECT %s::TEXT AS foo", ('<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>',))
|
||||||
x = curs.fetchone()[0]
|
x = curs.fetchone()[0]
|
||||||
print " ->", x.encode('utf-8'), ":", type(x)
|
print " ->", x.encode('utf-8'), ":", type(x)
|
||||||
curs.execute(u"SELECT %s::TEXT AS foo", (u'àèìòù',))
|
curs.execute(u"SELECT %s::TEXT AS foo", (u'<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>',))
|
||||||
x = curs.fetchone()[0]
|
x = curs.fetchone()[0]
|
||||||
print " ->", x.encode('utf-8'), ":", type(x)
|
print " ->", x.encode('utf-8'), ":", type(x)
|
||||||
|
|
||||||
print "Setting backend encoding to UTF8 and executing queries:"
|
print "Setting backend encoding to UTF8 and executing queries:"
|
||||||
conn.set_client_encoding('UNICODE')
|
conn.set_client_encoding('UNICODE')
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
curs.execute(u"SELECT %s::TEXT AS foo", (u'àèìòù'.encode('utf-8'),))
|
curs.execute(u"SELECT %s::TEXT AS foo", (u'<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>'.encode('utf-8'),))
|
||||||
x = curs.fetchone()[0]
|
x = curs.fetchone()[0]
|
||||||
print " ->", x.encode('utf-8'), ":", type(x)
|
print " ->", x.encode('utf-8'), ":", type(x)
|
||||||
curs.execute(u"SELECT %s::TEXT AS foo", (u'àèìòù',))
|
curs.execute(u"SELECT %s::TEXT AS foo", (u'<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>',))
|
||||||
x = curs.fetchone()[0]
|
x = curs.fetchone()[0]
|
||||||
print " ->", x.encode('utf-8'), ":", type(x)
|
print " ->", x.encode('utf-8'), ":", type(x)
|
||||||
|
|
|
@ -20,13 +20,13 @@ DSN = 'dbname=test'
|
||||||
## don't modify anything below tis line (except for experimenting)
|
## don't modify anything below tis line (except for experimenting)
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import psycopg
|
import psycopg2
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
DSN = sys.argv[1]
|
DSN = sys.argv[1]
|
||||||
|
|
||||||
print "Opening connection using dns:", DSN
|
print "Opening connection using dns:", DSN
|
||||||
conn = psycopg.connect(DSN)
|
conn = psycopg2.connect(DSN)
|
||||||
print "Encoding for this connection is", conn.encoding
|
print "Encoding for this connection is", conn.encoding
|
||||||
|
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
|
|
|
@ -18,13 +18,13 @@ DSN = 'dbname=test'
|
||||||
|
|
||||||
## don't modify anything below tis line (except for experimenting)
|
## don't modify anything below tis line (except for experimenting)
|
||||||
|
|
||||||
import sys, psycopg
|
import sys, psycopg2
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
DSN = sys.argv[1]
|
DSN = sys.argv[1]
|
||||||
|
|
||||||
print "Opening connection using dns:", DSN
|
print "Opening connection using dns:", DSN
|
||||||
conn = psycopg.connect(DSN)
|
conn = psycopg2.connect(DSN)
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -19,14 +19,14 @@ DSN = 'dbname=test'
|
||||||
|
|
||||||
## don't modify anything below this line (except for experimenting)
|
## don't modify anything below this line (except for experimenting)
|
||||||
|
|
||||||
import sys, psycopg
|
import sys, psycopg2
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
DSN = sys.argv[1]
|
DSN = sys.argv[1]
|
||||||
|
|
||||||
print "Opening connection using dns:", DSN
|
print "Opening connection using dns:", DSN
|
||||||
|
|
||||||
conn = psycopg.connect(DSN)
|
conn = psycopg2.connect(DSN)
|
||||||
print "Encoding for this connection is", conn.encoding
|
print "Encoding for this connection is", conn.encoding
|
||||||
|
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
|
@ -34,14 +34,14 @@ curs.execute("SELECT %(foo)s AS foo", {'foo':'bar'})
|
||||||
curs.execute("SELECT %(foo)s AS foo", {'foo':None})
|
curs.execute("SELECT %(foo)s AS foo", {'foo':None})
|
||||||
curs.execute("SELECT %(foo)s AS foo", {'foo':True})
|
curs.execute("SELECT %(foo)s AS foo", {'foo':True})
|
||||||
curs.execute("SELECT %(foo)s AS foo", {'foo':42})
|
curs.execute("SELECT %(foo)s AS foo", {'foo':42})
|
||||||
curs.execute("SELECT %(foo)s AS foo", {'foo':u'yattà!'})
|
curs.execute("SELECT %(foo)s AS foo", {'foo':u'yatt<EFBFBD>!'})
|
||||||
curs.execute("SELECT %(foo)s AS foo", {'foo':u'bar'})
|
curs.execute("SELECT %(foo)s AS foo", {'foo':u'bar'})
|
||||||
|
|
||||||
print curs.mogrify("SELECT %(foo)s AS foo", {'foo':'bar'})
|
print curs.mogrify("SELECT %(foo)s AS foo", {'foo':'bar'})
|
||||||
print curs.mogrify("SELECT %(foo)s AS foo", {'foo':None})
|
print curs.mogrify("SELECT %(foo)s AS foo", {'foo':None})
|
||||||
print curs.mogrify("SELECT %(foo)s AS foo", {'foo':True})
|
print curs.mogrify("SELECT %(foo)s AS foo", {'foo':True})
|
||||||
print curs.mogrify("SELECT %(foo)s AS foo", {'foo':42})
|
print curs.mogrify("SELECT %(foo)s AS foo", {'foo':42})
|
||||||
print curs.mogrify("SELECT %(foo)s AS foo", {'foo':u'yattà!'})
|
print curs.mogrify("SELECT %(foo)s AS foo", {'foo':u'yatt<EFBFBD>!'})
|
||||||
print curs.mogrify("SELECT %(foo)s AS foo", {'foo':u'bar'})
|
print curs.mogrify("SELECT %(foo)s AS foo", {'foo':u'bar'})
|
||||||
|
|
||||||
conn.rollback()
|
conn.rollback()
|
||||||
|
|
|
@ -66,10 +66,10 @@ and on the psycopg 2 wiki:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import psycopg
|
import psycopg2
|
||||||
import psycopg.extensions
|
import psycopg2.extensions
|
||||||
from psycopg.extensions import adapt as psycoadapt
|
from psycopg2.extensions import adapt as psycoadapt
|
||||||
from psycopg.extensions import register_adapter
|
from psycopg2.extensions import register_adapter
|
||||||
|
|
||||||
class AsIs(object):
|
class AsIs(object):
|
||||||
"""An adapter that just return the object 'as is'.
|
"""An adapter that just return the object 'as is'.
|
||||||
|
|
|
@ -23,13 +23,13 @@ class SimpleQuoter(object):
|
||||||
def sqlquote(x=None):
|
def sqlquote(x=None):
|
||||||
return "'bar'"
|
return "'bar'"
|
||||||
|
|
||||||
import sys, psycopg
|
import sys, psycopg2
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
DSN = sys.argv[1]
|
DSN = sys.argv[1]
|
||||||
|
|
||||||
print "Opening connection using dns:", DSN
|
print "Opening connection using dns:", DSN
|
||||||
conn = psycopg.connect(DSN)
|
conn = psycopg2.connect(DSN)
|
||||||
print "Encoding for this connection is", conn.encoding
|
print "Encoding for this connection is", conn.encoding
|
||||||
|
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
|
|
|
@ -36,7 +36,7 @@ MODE = 1
|
||||||
|
|
||||||
## don't modify anything below tis line (except for experimenting)
|
## don't modify anything below tis line (except for experimenting)
|
||||||
|
|
||||||
import sys, psycopg, threading
|
import sys, psycopg2, threading
|
||||||
from psycopg.pool import ThreadedConnectionPool
|
from psycopg.pool import ThreadedConnectionPool
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
|
@ -45,7 +45,7 @@ if len(sys.argv) > 2:
|
||||||
MODE = int(sys.argv[2])
|
MODE = int(sys.argv[2])
|
||||||
|
|
||||||
print "Opening connection using dns:", DSN
|
print "Opening connection using dns:", DSN
|
||||||
conn = psycopg.connect(DSN)
|
conn = psycopg2.connect(DSN)
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -83,7 +83,7 @@ def insert_func(conn_or_pool, rows):
|
||||||
try:
|
try:
|
||||||
c.execute("INSERT INTO test_threads VALUES (%s, %s, %s)",
|
c.execute("INSERT INTO test_threads VALUES (%s, %s, %s)",
|
||||||
(str(i), i, float(i)))
|
(str(i), i, float(i)))
|
||||||
except psycopg.ProgrammingError, err:
|
except psycopg2.ProgrammingError, err:
|
||||||
print name, ": an error occurred; skipping this insert"
|
print name, ": an error occurred; skipping this insert"
|
||||||
print err
|
print err
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
@ -112,14 +112,14 @@ def select_func(conn_or_pool, z):
|
||||||
conn_or_pool.putconn(conn)
|
conn_or_pool.putconn(conn)
|
||||||
s = name + ": number of rows fetched: " + str(len(l))
|
s = name + ": number of rows fetched: " + str(len(l))
|
||||||
print s
|
print s
|
||||||
except psycopg.ProgrammingError, err:
|
except psycopg2.ProgrammingError, err:
|
||||||
print name, ": an error occurred; skipping this select"
|
print name, ": an error occurred; skipping this select"
|
||||||
print err
|
print err
|
||||||
|
|
||||||
## create the connection pool or the connections
|
## create the connection pool or the connections
|
||||||
if MODE == 0:
|
if MODE == 0:
|
||||||
conn_insert = psycopg.connect(DSN)
|
conn_insert = psycopg2.connect(DSN)
|
||||||
conn_select = psycopg.connect(DSN)
|
conn_select = psycopg2.connect(DSN)
|
||||||
else:
|
else:
|
||||||
m = len(INSERT_THREADS) + len(SELECT_THREADS)
|
m = len(INSERT_THREADS) + len(SELECT_THREADS)
|
||||||
n = m/2
|
n = m/2
|
||||||
|
|
|
@ -19,16 +19,17 @@ DSN = 'dbname=test'
|
||||||
|
|
||||||
## don't modify anything below this line (except for experimenting)
|
## don't modify anything below this line (except for experimenting)
|
||||||
|
|
||||||
import sys, psycopg
|
import sys
|
||||||
|
import psycopg2
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from psycopg.tz import ZERO, LOCAL, FixedOffsetTimezone
|
from psycopg2.tz import ZERO, LOCAL, FixedOffsetTimezone
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
DSN = sys.argv[1]
|
DSN = sys.argv[1]
|
||||||
|
|
||||||
print "Opening connection using dns:", DSN
|
print "Opening connection using dns:", DSN
|
||||||
conn = psycopg.connect(DSN)
|
conn = psycopg2.connect(DSN)
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -19,7 +19,7 @@ DSN = 'dbname=test'
|
||||||
|
|
||||||
## don't modify anything below tis line (except for experimenting)
|
## don't modify anything below tis line (except for experimenting)
|
||||||
|
|
||||||
import sys, psycopg
|
import sys, psycopg2
|
||||||
import psycopg.extensions
|
import psycopg.extensions
|
||||||
import whrandom
|
import whrandom
|
||||||
|
|
||||||
|
@ -27,13 +27,13 @@ import whrandom
|
||||||
# because the adapter is meant to be used in SQL IN clauses while we use
|
# because the adapter is meant to be used in SQL IN clauses while we use
|
||||||
# tuples to represent points but it works and the example is about Rect, not
|
# tuples to represent points but it works and the example is about Rect, not
|
||||||
# "Point"
|
# "Point"
|
||||||
import psycopg.extras
|
import psycopg2.extras
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
DSN = sys.argv[1]
|
DSN = sys.argv[1]
|
||||||
|
|
||||||
print "Opening connection using dns:", DSN
|
print "Opening connection using dns:", DSN
|
||||||
conn = psycopg.connect(DSN)
|
conn = psycopg2.connect(DSN)
|
||||||
print "Initial encoding for this connection is", conn.encoding
|
print "Initial encoding for this connection is", conn.encoding
|
||||||
|
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
|
@ -65,7 +65,7 @@ class Rect(object):
|
||||||
|
|
||||||
def __conform__(self, proto):
|
def __conform__(self, proto):
|
||||||
"""This is a terrible hack, just ignore proto and return self."""
|
"""This is a terrible hack, just ignore proto and return self."""
|
||||||
if proto == psycopg.extensions.ISQLQuote:
|
if proto == psycopg2.extensions.ISQLQuote:
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def from_points(self, x0, y0, x1, y1):
|
def from_points(self, x0, y0, x1, y1):
|
||||||
|
@ -100,8 +100,8 @@ boxoid = curs.description[0][1]
|
||||||
print "Oid for the box datatype is", boxoid
|
print "Oid for the box datatype is", boxoid
|
||||||
|
|
||||||
# and build the user cast object
|
# and build the user cast object
|
||||||
BOX = psycopg.extensions.new_type((boxoid,), "BOX", Rect)
|
BOX = psycopg2.extensions.new_type((boxoid,), "BOX", Rect)
|
||||||
psycopg.extensions.register_type(BOX)
|
psycopg2.extensions.register_type(BOX)
|
||||||
|
|
||||||
# now insert 100 random data (2 points and a box in each row)
|
# now insert 100 random data (2 points and a box in each row)
|
||||||
for i in range(100):
|
for i in range(100):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user