简体   繁体   English

我如何在 python 中转储单个 sqlite3 表?

[英]how do i dump a single sqlite3 table in python?

I would like to dump only one table but by the looks of it, there is no parameter for this.我只想转储一张桌子,但从外观上看,没有参数。

I found this example of the dump but it is for all the tables in the DB:我找到了这个转储示例,但它适用于数据库中的所有表:

# Convert file existing_db.db to SQL dump file dump.sql
import sqlite3, os

con = sqlite3.connect('existing_db.db')
with open('dump.sql', 'w') as f:
    for line in con.iterdump():
        f.write('%s\n' % line)

You can copy only the single table in an in memory db:您只能复制 memory db 中的单个表:

import sqlite3

def getTableDump(db_file, table_to_dump):
    conn = sqlite3.connect(':memory:')    
    cu = conn.cursor()
    cu.execute("attach database '" + db_file + "' as attached_db")
    cu.execute("select sql from attached_db.sqlite_master "
               "where type='table' and name='" + table_to_dump + "'")
    sql_create_table = cu.fetchone()[0]
    cu.execute(sql_create_table);
    cu.execute("insert into " + table_to_dump +
               " select * from attached_db." + table_to_dump)
    conn.commit()
    cu.execute("detach database attached_db")
    return "\n".join(conn.iterdump())

TABLE_TO_DUMP = 'table_to_dump'
DB_FILE = 'db_file'

print getTableDump(DB_FILE, TABLE_TO_DUMP)

Pro : Simplicity and reliability: you don't have to re-write any library method, and you are more assured that the code is compatible with future versions of the sqlite3 module.优点:简单可靠:您不必重新编写任何库方法,并且您更放心代码与 sqlite3 模块的未来版本兼容。

Con : You need to load the whole table in memory, which may or may not be a big deal depending on how big the table is, and how much memory is available.缺点:您需要将整个表加载到memory中,这取决于表的大小以及可用的 memory 的大小,这可能不是什么大问题。

Dump realization lies here http://coverage.livinglogic.de/Lib/sqlite3/dump.py.html (local path: PythonPath/Lib/sqlite3/dump.py) dump实现就在这里http://coverage.livinglogic.de/Lib/sqlite3/dump.py.html (本地路径:PythonPath/Lib/sqlite3/dump.py)

You can modify it a little:你可以稍微修改一下:

# Mimic the sqlite3 console shell's .dump command
# Author: Paul Kippes <kippesp@gmail.com>

def _iterdump(connection, table_name):
    """
    Returns an iterator to the dump of the database in an SQL text format.

    Used to produce an SQL dump of the database.  Useful to save an in-memory
    database for later restoration.  This function should not be called
    directly but instead called from the Connection method, iterdump().
    """

    cu = connection.cursor()
    table_name = table_name

    yield('BEGIN TRANSACTION;')

    # sqlite_master table contains the SQL CREATE statements for the database.
    q = """
       SELECT name, type, sql
        FROM sqlite_master
            WHERE sql NOT NULL AND
            type == 'table' AND
            name == :table_name
        """
    schema_res = cu.execute(q, {'table_name': table_name})
    for table_name, type, sql in schema_res.fetchall():
        if table_name == 'sqlite_sequence':
            yield('DELETE FROM sqlite_sequence;')
        elif table_name == 'sqlite_stat1':
            yield('ANALYZE sqlite_master;')
        elif table_name.startswith('sqlite_'):
            continue
        else:
            yield('%s;' % sql)

        # Build the insert statement for each row of the current table
        res = cu.execute("PRAGMA table_info('%s')" % table_name)
        column_names = [str(table_info[1]) for table_info in res.fetchall()]
        q = "SELECT 'INSERT INTO \"%(tbl_name)s\" VALUES("
        q += ",".join(["'||quote(" + col + ")||'" for col in column_names])
        q += ")' FROM '%(tbl_name)s'"
        query_res = cu.execute(q % {'tbl_name': table_name})
        for row in query_res:
            yield("%s;" % row[0])

    # Now when the type is 'index', 'trigger', or 'view'
    #q = """
    #    SELECT name, type, sql
    #    FROM sqlite_master
    #        WHERE sql NOT NULL AND
    #        type IN ('index', 'trigger', 'view')
    #    """
    #schema_res = cu.execute(q)
    #for name, type, sql in schema_res.fetchall():
    #    yield('%s;' % sql)

    yield('COMMIT;')

Now it accepts table name as second argument.现在它接受表名作为第二个参数。
You can use it like this:你可以像这样使用它:

with open('dump.sql', 'w') as f:
    for line in _iterdump(con, 'GTS_vehicle'):
        f.write('%s\n' % line)

Will get something like:会得到类似的东西:

BEGIN TRANSACTION;
CREATE TABLE "GTS_vehicle" ("id" integer NOT NULL PRIMARY KEY, "name" varchar(20) NOT NULL, "company_id" integer NULL, "license_plate" varchar(20) NULL, "icon" varchar(100) NOT NULL DEFAULT 'baseicon.png', "car_brand" varchar(30) NULL, "content_type_id" integer NULL, "modemID" varchar(100) NULL, "distance" integer NULL, "max_speed" integer NULL DEFAULT 100, "max_rpm" integer NULL DEFAULT 4000, "fuel_tank_volume" integer NULL DEFAULT 70, "max_battery_voltage" integer NULL, "creation_date" datetime NOT NULL, "last_RFID" text NULL);
INSERT INTO "GTS_vehicle" VALUES(1,'lan1_op1_car1',1,'03115','baseicon.png','UFP',16,'lan_op1_car1',NULL,100,4000,70,12,'2011-06-23 11:54:32.395000',NULL);
INSERT INTO "GTS_vehicle" VALUES(2,'lang_op1_car2',1,'03','baseicon.png','ыва',16,'lan_op1_car2',NULL,100,4000,70,12,'2011-06-23 11:55:02.372000',NULL);
INSERT INTO "GTS_vehicle" VALUES(3,'lang_sup_car1',1,'0000','baseicon.png','Fiat',16,'lan_sup_car1',NULL,100,4000,70,12,'2011-06-23 12:32:09.017000',NULL);
INSERT INTO "GTS_vehicle" VALUES(4,'lang_sup_car2',1,'123','baseicon.png','ЗАЗ',16,'lan_sup_car2',NULL,100,4000,70,12,'2011-06-23 12:31:38.108000',NULL);
INSERT INTO "GTS_vehicle" VALUES(9,'lang_op2_car1',1,'','baseicon.png','',16,'1233211234',NULL,100,4000,70,12,'2011-07-05 13:32:09.865000',NULL);
INSERT INTO "GTS_vehicle" VALUES(11,'Big RIder',1,'','baseicon.png','0311523',16,'111',NULL,100,4000,70,20,'2011-07-07 12:12:40.358000',NULL);
COMMIT;

By iterdump(), all information would be displayed like this:通过 iterdump(),所有信息将显示如下:

INSERT INTO "name" VALUES(1, 'John')
INSERT INTO "name" VALUES(2, 'Jane')
INSERT INTO "phone" VALUES(1, '111000')
INSERT INTO "phone" VALUES(2, '111001')

An easy way is by filter certain keywords by string.startswith() method.一种简单的方法是通过 string.startswith() 方法过滤某些关键字。 For example, the table name is 'phone':例如,表名是“电话”:

# Convert file existing_db.db to SQL dump file dump.sql
import sqlite3, os

con = sqlite3.connect('existing_db.db')
with open('dump.sql', 'w') as f:
    for line in con.iterdump():
        if line.startswith('INSERT INTO "phone"'):
            f.write('%s\n' % line)

Not very smart, but can fit your objective.不是很聪明,但可以满足您的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python在SQLite3中显示表结构? - How do I display the table structure in SQLite3 with python? 如何使用 SQLite3 和 Python 更新 SQLite 表中的行 - How to do update rows in SQLite table using SQLite3 and Python python sqlite3 tkinter-如何为sqlite表中的每一行自动创建新的小部件? - python sqlite3 tkinter- How do I automatically create new widgets for every row in an sqlite table? 使用Python将SQLite3数据库表迁移到MySQL而不使用转储文件 - Migrate a SQLite3 database table to MySQL with Python without dump files 如何在 python sqlite3 命令中将变量作为列名和表名传递? - How do I pass variables as column and table names in a python sqlite3 command? 如何使用python从SQlite3数据库查看特定表? - How do I view a specific table from a SQlite3 databse using python? 如何使用Python和SQLite3在PyQT的表中获取用户已更改的信息 - How do I get the information that the user has changed in a table in PyQT with Python and SQLite3 如何使用我的 sqlite3 数据库中的表数据作为 python 变量 - How do i use table data from my sqlite3 database as a python variable 如何使用python将文本文件导入SQLite3表 - How do I import a text file into a SQLite3 table using python 如何正确地将Python“列表列表”映射到SQL表(使用SQLite3构建)? - How do I correctly map a Python “list of lists” into an SQL table (built with SQLite3)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM