Simple, fast SQL queries for flat files

前端 未结 8 915
广开言路
广开言路 2021-02-04 07:24

Does anyone know of any tools to provide simple, fast queries of flat files using a SQL-like declarative query language? I\'d rather not pay the overhead of loading the file in

8条回答
  •  南笙
    南笙 (楼主)
    2021-02-04 08:14

    you can use sqlite. Here's an example using Python.

    import sqlite3
    conn = sqlite3.connect('/tmp/test.db')
    cursor = conn.cursor()
    try:
        cursor.execute("""create table table1 (word varchar not null, number varchar not null)""")
    except: pass
    cursor.execute("insert into table1 values ('dog', '15')")
    cursor.execute("insert into table1 values ('cat', '20')")
    cursor.execute("insert into table1 values ('dog', '10')")
    cursor.execute("select max(number) , word from table1 group by word")
    print cursor.fetchall()
    

    output

    $ ./python.py
    [(u'20', u'cat'), (u'15', u'dog')]
    

提交回复
热议问题