2
from sqlalchemy import create_engine
engine = create_engine('mssql+pymssql://myusername:[email protected]:1433/AQOA_Core')
connection = engine.connect()
result = connection.execute("""SELECT DISTINCT Distributor FROM Product""")
for row in result:
    print row[0]
connection.close()

The above code returns a result set as:

FRANCETV
GAUMONT
M6SND
PATHE
STUDIOCANAL
TF1
WARNER

What if I want to print just one value without changing the query?

Tried print row[0][1], print row[0:1] This was tried to print just the first value FRANCETV

Basically I want to be able to print each one of the values in the result set seperately without making changes to the query.

6
  • What does print result[0][0] return? Commented Jul 6, 2015 at 14:50
  • S E G G W O M Traceback (most recent call last): File "connect.py", line 6, in <module> print row[0][0] TypeError: 'NoneType' object has no attribute '__getitem__' Commented Jul 6, 2015 at 14:54
  • 1
    That's not PyMsSQL at all. Commented Jul 6, 2015 at 15:45
  • oops! That what I thought it was. Commented Jul 6, 2015 at 15:49
  • It's SQLAlchemy. I would look at their docs for the equivalent fetchone function. Commented Jul 6, 2015 at 15:54

3 Answers 3

3

You can try to access the data by:

connection = engine.connect()
result = connection.execute("""SELECT DISTINCT Distributor FROM Product""")
result_list = result.fetchall()  
result_list[0][0]
connection.close()
Sign up to request clarification or add additional context in comments.

3 Comments

This does the same thing as my code. What if I wanted to print only 1 value?
Works perfect. I make necessary changes in result_list to print different values of the result set.
Good! The fetchall() method returns a list of tuples, which you can access and modify like you intended to do.
1

If you take a look at the pymssql docs, you'll see the cursor object has a fetchone() method.

with engine.connect() as connection:
    cursor = connection.cursor()
    cursor.execute("""SELECT DISTINCT Distributor FROM Product""")
    first_row = cursor.fetchone()
    print first_row

4 Comments

I am using pymssql for the connection.
@TauseefHussain Does this work for you? If not, what is the error?
AttributeError: 'Connection' object has no attribute 'cursor'
You said you're using pymssql? Can you edit your main post to show exactly what you're trying?
0
 file = open("testfile.txt", "w", encoding='utf-8')
 mycursor.execute('SELECT * from COMPANY')
 myresult = mycursor.fetchall()
 counter = 0
 y = 0
   for x in myresult:
   y = 0
   while y < 3:
      print(y)
      file.write("%s\n" % myresult[counter][y])
      y = y + 1
   counter = counter + 1

file.close()

This is what I concluded from the answers above

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
A code-only answer is not high quality. While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please edit your answer to include explanation and link to relevant documentation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.