Here is my code in Python:
queryuniq = "SELECT COUNT(distinct src_ip), COUNT(distinct video_id)FROM video"
cur.execute(queryuniq)
uniq = []
uniq = cur.fetchall()
print uniq
ip = str(uniq[0])
video = str(uniq[1])
fd2.write("There are %d ip addresses and %d video in total" %(int(ip), int(video)))
This is the value of “uniq” variable I got:
((2052L, 163581L),)
And this error message:
fd2.write("There are %d ip addresses in total" %(int(ip)))
ValueError: invalid literal for int() with base 10: '((2052L,),)'
video = str(uniq[1])
IndexError: tuple index out of range
I just simply want to count the distinct items in a column in the database, and print the INT value in a file.
Can anyone explain why the SELECT command return a weird data format like ((2052L, 163581L),) ? Don’t understand why there is a “L”after the number..
How can I solve this problem? Many thanks!
uniqis a tuple of tuples (each entry at the outer level represents a database row, within which there is a tuple of column values).You query always returns one row. Therefore the outer tuple always contains one element, and you could fix your code by replacing:
with
Also, the conversions from int to string and then back to int are unnecessary.
To summarize, the following is a tidied up version of your code: