I have following query cur.execute("SELECT COUNT(addr) FROM list_table WHERE addr = '192.168.1.1'")
to count the number of times the same address (192.168.1.1) appears on list_table
table. addr
is of inet type.
When I assign the query to a variable and print its result I get None
:
res = cur.execute("SELECT COUNT(addr) FROM list_table WHERE addr = '192.168.1.1'")
print res # None
What is the proper way to get such thing?
you can use the following steps for retrieving the data for a relational database using python:
#!/usr/bin/python
# import the desired package
import MySQLdb
# Open database connection
db = MySQLdb.connect(hostaddress,user,password,db_name)
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
cursor.execute("SELECT COUNT(addr) FROM list_table WHERE addr = '192.168.1.1'")
# Fetch a single row using fetchone() method and store the result in a variable.
data = cursor.fetchone()
#OR use fetchall() method to fetch multiple rows and store the result in a list variable.
data = cursor.fetchall()
print data
# disconnect from server
db.close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With