473,545 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Psycopg2 date problems: "Can't adapt"

I'm trying to copy data from an Access database to PostgreSQL, as the
latter now appears to work well in the Windows environment. However I'm
having trouble with date columns.

The PostgreSQL table receiving the data has the following definition:

CREATE TABLE Lines (
LinID SERIAL PRIMARY KEY,
LinDate TIMESTAMP(0),
LinQty INTEGER,
LinPrdID INTEGER ,
LinPrice NUMERIC(8,2),
LinInvoice INTEGER)

Here's the problem in a nutshell:
d <DateTime object for '2003-10-02 00:00:00.00' at af9be0> ocurs.execute(" DELETE FROM Lines")
osql 'INSERT INTO Lines(LinID, LinDate, LinQty, LinPrdID, LinPrice,
LinInvoice) VALUES(%s, %s, %s, %s, %s, %s)' ocurs.execute(o sql, (1, d, 1, 1, 12500.0, 88)) Traceback (most recent call last):
File "<string>", line 1, in <string>
psycopg.Program mingError: can't adapt ocurs.execute(o sql, (1, None, 1, 1, 12500.0, 88))


Since the date value's the only difference between the two, I deduce
it's causing the problem.

I'd rather not have to manipulate the data (in other words, I'd rather
just change the definition of the receiving table to avoid the error if
possible), as the copying operation attempts to be table-independent. It
currently reads:

for tbl, cols in d.items():
print "Copying", tbl
dsql = "DELETE FROM %s" % tbl
ocurs.execute(d sql)
isql = "SELECT %s FROM %s" % (", ".join(cols ), tbl)
osql = "INSERT INTO %s(%s) VALUES(%s)" % (
tbl, ", ".join(cols ), ", ".join("%s" for c in cols))
print isql, '\n', osql
icurs.execute(i sql)
for row in icurs.fetchall( ):
ocurs.execute(o sql, row)

Though until I started stepping through the data row by row the last two
lines were replaced by

ocurs.executema ny(osql, icurs.fetchall( ))

Who can help me past this little sticking point?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Oct 21 '05 #1
2 5245
Is "None" a valid value for SQL ? Or should it be NULL ? May be it is
because your input is NULL which is being converted to None in python
but haven't been converted back to NULL on its way out.

Steve Holden wrote:
I'm trying to copy data from an Access database to PostgreSQL, as the
latter now appears to work well in the Windows environment. However I'm
having trouble with date columns.

The PostgreSQL table receiving the data has the following definition:

CREATE TABLE Lines (
LinID SERIAL PRIMARY KEY,
LinDate TIMESTAMP(0),
LinQty INTEGER,
LinPrdID INTEGER ,
LinPrice NUMERIC(8,2),
LinInvoice INTEGER)

Here's the problem in a nutshell:
>>> d <DateTime object for '2003-10-02 00:00:00.00' at af9be0> >>> ocurs.execute(" DELETE FROMI Lines")
>>> osql 'INSERT INTO Lines(LinID, LinDate, LinQty, LinPrdID, LinPrice,
LinInvoice) VALUES(%s, %s, %s, %s, %s, %s)' >>> ocurs.execute(o sql, (1, d, 1, 1, 12500.0, 88)) Traceback (most recent call last):
File "<string>", line 1, in <string>
psycopg.Program mingError: can't adapt >>> ocurs.execute(o sql, (1, None, 1, 1, 12500.0, 88))
>>>


Since the date value's the only difference between the two, I deduce
it's causing the problem.

I'd rather not have to manipulate the data (in other words, I'd rather
just change the definition of the receiving table to avoid the error if
possible), as the copying operation attempts to be table-independent. It
currently reads:

for tbl, cols in d.items():
print "Copying", tbl
dsql = "DELETE FROM %s" % tbl
ocurs.execute(d sql)
isql = "SELECT %s FROM %s" % (", ".join(cols ), tbl)
osql = "INSERT INTO %s(%s) VALUES(%s)" % (
tbl, ", ".join(cols ), ", ".join("%s" for c in cols))
print isql, '\n', osql
icurs.execute(i sql)
for row in icurs.fetchall( ):
ocurs.execute(o sql, row)

Though until I started stepping through the data row by row the last two
lines were replaced by

ocurs.executema ny(osql, icurs.fetchall( ))

Who can help me past this little sticking point?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/


Oct 21 '05 #2
bo****@gmail.co m wrote:
Steve Holden wrote:
I'm trying to copy data from an Access database to PostgreSQL, as the
latter now appears to work well in the Windows environment. However I'm
having trouble with date columns.

The PostgreSQL table receiving the data has the following definition:

CREATE TABLE Lines (
LinID SERIAL PRIMARY KEY,
LinDate TIMESTAMP(0),
LinQty INTEGER,
LinPrdID INTEGER ,
LinPrice NUMERIC(8,2),
LinInvoice INTEGER)

Here's the problem in a nutshell:
>>> d

<DateTime object for '2003-10-02 00:00:00.00' at af9be0>
>>> ocurs.execute(" DELETE FROMI Lines")
>>> osql

'INSERT INTO Lines(LinID, LinDate, LinQty, LinPrdID, LinPrice,
LinInvoice) VALUES(%s, %s, %s, %s, %s, %s)'
>>> ocurs.execute(o sql, (1, d, 1, 1, 12500.0, 88))

Traceback (most recent call last):
File "<string>", line 1, in <string>
psycopg.Progr ammingError: can't adapt
>>> ocurs.execute(o sql, (1, None, 1, 1, 12500.0, 88))
>>>


Since the date value's the only difference between the two, I deduce
it's causing the problem.
[...]

Is "None" a valid value for SQL ? Or should it be NULL ? May be it is
because your input is NULL which is being converted to None in python
but haven't been converted back to NULL on its way out.

Python's None is the way you communicate null values through the query
parameterisatio n mechanism. You will observe that the statement with the
None value for the date field runs fine, and the error occurs when I
provide an actual date object.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Oct 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3404
by: Askari | last post by:
Yesterday, ALL code in python work and nothing when I close(finish) a code. Today, when I close, some raise this windows error : Instruction at "0x00FC3D70" use memory address "0x00000000". Can't be "read". Click on "OK" to exit program. Why this error, today and not before today?
2
8673
by: Robin Tucker | last post by:
I have some code that dynamically creates a database (name is @FullName) and then creates a table within that database. Is it possible to wrap these things into a transaction such that if any one of the following fails, the database "creation" is rolledback. Otherwise, I would try deleting on error detection, but it could get messy. IF...
119
4517
by: rhat | last post by:
I heard that beta 2 now makes ASP.NET xhtml compliant. Can anyone shed some light on what this will change and it will break stuff as converting HTML to XHTML pages DO break things. see, http://www.alistapart.com/articles/betterliving/ I read on...
4
1740
by: wangzhihuii | last post by:
Hi all, I'm really confused, can cout<<""; contribute anything to the routine ?!! my programm won't work properly without this trivial sentence. Sincerely vivian
13
2498
by: baumann.Pan | last post by:
when define char *p = " can not modify"; p ='b' ;is not allowed, but if you declare p as char p = "can modify"; p = 'b'; is ok? why?
0
383
by: mvanier | last post by:
There was a thread a while back dealing with an error message the psycopg2 Postgres interface gives when trying to convert some mxDateTime values: "can't adapt". The answer given was that psycopg2 wasn't finding the mxDateTime include files during setup, so it assumed they weren't there. I'm having the same problem. I've installed both...
6
2241
by: vl106 | last post by:
A static code analysis tool gave me a warning on if (ptr && ptr->data) { ... } I assumed the tool doesn't get the "short circuit behaviour" in the if statement. But a collegue said it may be the missing check ot ptr against NULL.
0
7499
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7689
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7943
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.