SQLite Modify Column

后端 未结 4 1372
迷失自我
迷失自我 2020-12-02 22:26

I need to modify a column in a SQLite database but I have to do it programatically due to the database already being in production. From my research I have found that in or

相关标签:
4条回答
  • 2020-12-02 22:57

    When I ran "CREATE TABLE tmp_table AS SELECT id, name FROM src_table", I lost all the column type formatting (e.g., time field turned into a integer field

    As initially stated seems like it should be easier, but here is what I did to fix. I had this problem b/c I wanted to change the Not Null field in a column and Sqlite doesnt really help there.

    Using the 'SQLite Manager' Firefox addon browser (use what you like). I created the new table by copying the old create statement, made my modification, and executed it. Then to get the data copied over, I just highlighted the rows, R-click 'Copy Row(s) as SQL', replaced "someTable" with my table name, and executed the SQL.

    0 讨论(0)
  • 2020-12-02 23:00

    As said here, these kind of features are not implemented by SQLite.

    As a side note, you could make your two first steps with a create table with select:

    CREATE TABLE tmp_table AS SELECT id, name FROM src_table
    
    0 讨论(0)
  • 2020-12-02 23:02

    That's one of the better-known drawbacks of SQLite (no MODIFY COLUMN support on ALTER TABLE), but it's on the list of SQL features that SQLite does not implement.

    edit: Removed bit that mentioned it may being supported in a future release as the page was updated to indicate that is no longer the case

    0 讨论(0)
  • 2020-12-02 23:04

    If the modification is not too big (e.g. change the length of a varchar), you can dump the db, manually edit the database definition and import it back again:

    echo '.dump' | sqlite3 test.db > test.dump
    

    then open the file with a text editor, search for the definition you want to modify and then:

    cat test.dump | sqlite3 new-test.db
    
    0 讨论(0)
提交回复
热议问题