使用python和sqlite3如何打开文件?

时间:2016-05-22 06:53:18

标签: python sqlite

我理解如何使用python脚本打开数据库:

    import sqlite3 as lite
    .
    .
    con = lite.connect(db_name)

然而,我所拥有的是使用tsk_loaddb制作的大量数据库文件,当我在sqlite3中时我可以使用以下

    SQLite version 3.8.7.2 2014-11-18 20:57:56
    Enter ".help" for usage hints.
    Connected to a transient in-memory database.
    Use ".open FILENAME" to reopen on a persistent database.
    sqlite> .open /work/jmjohnso1/db_project/db_IN60-1020
    sqlite> .tables
    blackboard_artifact_types   tsk_files_derived_method
    blackboard_artifacts        tsk_files_path
    blackboard_attribute_types  tsk_fs_info
    blackboard_attributes       tsk_image_info
    tsk_db_info                 tsk_image_names
    tsk_file_layout             tsk_objects
    tsk_files                   tsk_vs_info
    tsk_files_derived           tsk_vs_parts

但是,我不知道如何在python中做到这一点。我的问题是如何使用sqlite3 python库翻译open命令。

原来我名字中有换行符,因为我正在读取文件。以下是有效的。

 def open_sql(db_folder, db_name, table):
        # databases are located at /work/jmjohnso1/db_project  
        path_name = os.path.join(db_folder,db_name).strip()
        con = lite.connect(path_name)
        cur = con.cursor()
        cur.execute('SELECT * FROM ' + table) 
        col_names = [cn[0] for cn in cur.description]
        rows = cur.fetchall()
        print_header(col_names)
        print_output(rows) 
        con.close()

0 个答案:

没有答案
相关问题