473,714 Members | 3,933 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 5283
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
3422
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
8686
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 @Error = 0 BEGIN SET @ExecString = 'CREATE DATABASE ' + @FullName EXEC sp_executesql @ExecString
119
4600
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 http://msdn.microsoft.com/netframework/default.aspx?pull=/library/en-us/dnnetdep/html/netfxcompat.asp It said they changed stuff like this
4
1763
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
2512
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 packages from source. However, as far as I can tell, the mxDateTime include files are never copied to...
6
2251
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
8712
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9316
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9179
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9023
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5959
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4729
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2525
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2113
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.