Search Sqlite Database - All Tables and Columns

前端 未结 3 1690

Is there a library or open source utility available to search all the tables and columns of an Sqlite database? The only input would be the name of the sqlite DB file.

相关标签:
3条回答
  • 2020-12-16 03:07

    You could use "SELECT name FROM sqlite_master WHERE type='table'" to find out the names of the tables in the database. From there it is easy to SELECT all rows of each table.

    For example:

    import sqlite3
    import os
    
    filename = ...
    with sqlite3.connect(filename) as conn:
        conn.row_factory = sqlite3.Row
        cursor = conn.cursor()    
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
        for tablerow in cursor.fetchall():
            table = tablerow[0]
            cursor.execute("SELECT * FROM {t}".format(t = table))
            for row in cursor:
                for field in row.keys():
                    print(table, field, row[field])
    
    0 讨论(0)
  • 2020-12-16 03:11

    I know this is late to the party, but I had a similar issue but since it was inside of a docker image I had no access to python, so I solved it like so:

    for X in $(sqlite3 database.db .tables) ; do sqlite3 database.db "SELECT * FROM $X;" | grep >/dev/null 'STRING I WANT' && echo $X; done
    

    This will iterate through all tables in a database file and perform a select all operation which I then grep for the string. If it finds the string, it prints the table, and from there I can simply use sqlite3 to find out how it was used.

    Figured it might be helpful to other who cannot use python.

    0 讨论(0)
  • 2020-12-16 03:23

    @MrWorf's answer didn't work for my sqlite file (an .exb file from Evernote) but this similar method worked:

    • Open the file with DB Browser for SQLite sqlitebrowser mynotes.exb
    • File / Export to SQL file (will create mynotes.exb.sql)
    • grep 'STRING I WANT" mynotes.exb.sql
    0 讨论(0)
提交回复
热议问题