SQLite - Run multi-line SQL script from file?

后端 未结 4 673
盖世英雄少女心
盖世英雄少女心 2021-02-01 12:49

I have the following SQL in a file, user.sql:

CREATE TABLE user
(
  user_id INTEGER PRIMARY KEY,
  username varchar(255),
  password varchar(255)
);
4条回答
  •  失恋的感觉
    2021-02-01 13:29

    I realize that this is not a direct answer to your question. As Brian mentions, this could be a silly platform issue.

    If you interface with SQLite through Python, you will probably avoid most platform-specific issues and you get to have fun things like datetime columns :-)

    Something like this should work fine:

    import sqlite3
    
    qry = open('create_table_user.sql', 'r').read()
    conn = sqlite3.connect('/path/to/db')
    c = conn.cursor()
    c.execute(qry)
    conn.commit()
    c.close()
    conn.close()
    

提交回复
热议问题