jeudi 11 juin 2015

Python SQLite3 Mac Address with colons lookup

I'm busy with a little project that keeps track of MAC Addresses on certain networks. Everything is working great except when I do a lookup in the DB (SQLite3) for the MAC Address caught by pcapy in python2.7.

Here's the Def in the code.

def db_check(_loc, _mac):
    con = lite.connect('database.db')
    con.text_factory = str

    cur = con.cursor()
    cur.execute("SELECT * FROM "+_loc+" WHERE mac_addr=?", (_mac,))
   # _loc = Tablename
   # _mac = variable with mac address 
   # mac_addr = column name in sqlite3
   # MAC Address format xx:xx:xx:xx:xx

    _data = cur.fetchall()

    if len(_data)==0:
        print('There is no mac %s'%_mac)
    else:
        print('Component %s found with rowids %s'%(_mac,','.join(map(str,zip(*_data)[0]))))

I want to grab the row number of the row where the Mac address was seen. But when I print the fetchall() I only get [ ] as the output.

When I run the sql query in sqlite then I get the whole row as expected.

Can anyone provide some guidance please?

Aucun commentaire:

Enregistrer un commentaire