Learn SQL The Hard way - Creating .sql with .db in SQL Lite 3 - Why and How?

前端 未结 3 1423
攒了一身酷
攒了一身酷 2020-12-30 14:52

As a beginning programmer with +20 hours of Python coding and novice familiarity with the command-line, I opened up Zed Shaw\'s \"Learn SQL The Hard Way\" and was quickly st

相关标签:
3条回答
  • 2020-12-30 15:09
    1. Save your code in a file with the extension .sql
    2. Then in the terminal: sqlite3 ex1.db < ex1.sql for create a ex1.db
    3. Put in the terminal: sqlite3 ex1.db .schema
    4. Or put: sqlite3 ex1.db [... and then....] .schema
    0 讨论(0)
  • 2020-12-30 15:14

    I ran into same problem today, and like you I was wondering why my command is not working. As mentioned above author assumes that you create the initial command in a text editor and save as a .sql file. And then ex1.db < ex1.sql command converts the file to a .db file.

    0 讨论(0)
  • 2020-12-30 15:20
    sqlite3 ex1.db < ex1.sql
    

    For the above to work, ex1.sql should already exist. < is a character used in shells for input redirection. sqlite3 is started here with a new or existing database (it will create the database as needed), and gets SQL statements from ex1.sql, executing them and modifying ex1.db accordingly.

    Now let's generate ex1.sql from ex1.db, which you apparently want to do:

    sqlite3 ex1.db .dump > ex1.sql
    

    We use shell redirection facility again, now redirecting the output to ex1.sql. The .dump command makes sqlite write out SQL statements that will recreate similar database when they are executed in an empty database: tables are recreated and populated with INSERT, etc.

    Now you can go to step 1:

    sqlite3 ex1copy.db < ex1.sql
    
    0 讨论(0)
提交回复
热议问题