Question

The code is below, by the way the database I use is teradata ,and in a windows 7 operative system and python version 2.7.

import pyodbc
cnxn = pyodbc.connect('DSN=thisIsAbsolutelyCorrect;UID=cannottellyou;PWD=iamsosorry')
cursor1 = cnxn.cursor()
cursor1=cursor1.execute(
##################        OR put your SQL dirctly between here        ################
'''

create volatile table table1
(
field1  integer
,field2 integer
)on commit preserve rows;
--insert into table1
--values(12,13);
--select   * from table1;






''')  
#########################        and here         ########################
cnxn.commit()
for row in cursor1:
    print row
raw_input()

But I get the error like this:

Traceback (most recent call last):
  File "C:\Users\issuser\Desktop\py\test.py", line 25, in <module>
    for row in cursor1:
ProgrammingError: No results.  Previous SQL was not a query.

How can I solve this error?

Was it helpful?

Solution

A cursor object will have no rows to iterate through. What I think you want is to iterate through the results of an execute.

rows = curs.execute(""" sql code """).fetchall()
for row in rows:
    print row

here is a template to upload to a volatile table in teradata from python2.7 using pyodbc:


import pyodbc
cnxn = pyodbc.connect('your_connection_string')
curs = cnxn.cursor()
curs.execute("""
    CREATE VOLATILE TABLE TABLE_NAME
        (
        c_0 dec(10,0),
        ...
        c_n dec(10,0)
        ) PRIMARY INDEX (c0)
        ON COMMIT PRESERVE ROWS;
        END TRANSACTION;
        """)

curs.execute("""
    INSERT INTO TABLE_NAME (c_0,...,c_n) VALUES (%s);
    """%value_string)

Depending on your settings in Teradata you must explicitly END TRANSACTION. You can add your loop around the INSERT to upload information line by line.

OTHER TIPS

Have you considered the following:

import pyodbc
cnxn = pyodbc.connect('DSN=thisIsAbsolutelyCorrect;UID=cannottellyou;PWD=iamsosorry')
cursor1 = cnxn.cursor()
RowCount=cursor1.execute(
'''
create volatile table table1
(
field1  integer
,field2 integer
)on commit preserve rows;
''').rowcount   

RowCount=cursor1.execute('''insert into table1 values(12,13);''').rowcount

cnxn.commit()

for row in cursor1:
    print row
raw_input()

I believe the issue is that the EXECUTE() method as you have written is expecting a cursor to be returned. DDL and DML statements like INSERT, UPDATE, DELETE to not return result sets. You may have more success in using the ROWCOUNT variable with the EXECUTE() method to process the volatile table creation and its population.

You may also have to issue a commit between the creation of the volatile table and populating the table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top