Copy table structure to new table in sqlite3

前端 未结 5 1500
梦谈多话
梦谈多话 2020-12-02 14:20

Is there an easy way to copy an existing table structure to a new one? (dont need the data, only the structure -> like id INTEGER, name varchar(20) ...)

Thx

5条回答
  •  一生所求
    2020-12-02 14:58

    SQLite cannot clone table with PK, defaults and indices.

    Hacking by another tool is necessary.

    In shell, replace the table name by sed.

    sqlite3 dbfile '.schema oldtable' | sed '1s/oldtable/newtable/' | sqlite3 dbfile
    

    And you can check new table.

    sqlite3 dbfile '.schema newtable'
    

    Primary key, defaults and indices will be reserved.

    I hope this command can help you.

提交回复
热议问题