使用冒号查找的Python SQLite3 Mac地址

时间:2015-06-11 11:40:18

标签: python sqlite

我正忙于一个跟踪某些网络上的MAC地址的小项目。一切都很好,除非我在数据库(SQLite3)中查找pcapy在python2.7中捕获的MAC地址。

这是代码中的Def。

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]))))

我想获取看到Mac地址的行的行号。但是当我打印fetchall()时,我只得到[]作为输出。

当我在sqlite中运行sql查询时,我按预期得到了整行。

有人可以提供一些指导吗?

1 个答案:

答案 0 :(得分:0)

好的,我设法得到了我想要的结果。

"SELECT mac_addr, timestamp, (SELECT COUNT(*) FROM mytblname AS t2 WHERE t2.mac_addr <= t1.mac_addr) AS row_Num FROM mytblname AS t1 ORDER BY timestamp, mac_addr;"

这给了我所需的行号。

相关问题