Opening database file from within SQLite command-line shell

后端 未结 8 1008
时光说笑
时光说笑 2021-01-29 21:07

I\'m using the SQLite Command Line Shell. As documented, I can open a database by supplying it as an argument to the executable:

sqlite3 data.db

8条回答
  •  没有蜡笔的小新
    2021-01-29 21:56

    You can simply specify the database file name in the command line:

    bash-3.2 # sqlite3 UserDb.sqlite
    SQLite version 3.16.2 2017-01-06 16:32:41
    Enter ".help" for usage hints.
    
    sqlite> .databases
    main: /db/UserDb.sqlite
    
    sqlite> .tables
    accountLevelSettings  genres               syncedThumbs
    collectionActivity    recordingFilter      thumbs
    contentStatus         syncedContentStatus 
    
    sqlite> select count(*) from genres;
    10
    

    Moreover, you can execute your query from the command line:

    bash-3.2 # sqlite3 UserDb.sqlite 'select count(*) from genres'
    10
    

    You could attach another database file from the SQLite shell:

    sqlite> attach database 'RelDb.sqlite' as RelDb;
    
    sqlite> .databases
    main: /db/UserDb.sqlite
    RelDb: /db/RelDb_1.sqlite
    
    sqlite> .tables
    RelDb.collectionRelationship  contentStatus               
    RelDb.contentRelationship     genres                      
    RelDb.leagueRelationship      recordingFilter             
    RelDb.localizedString         syncedContentStatus         
    accountLevelSettings          syncedThumbs                
    collectionActivity            thumbs                      
    

    The tables from this 2nd database will be accessible via prefix of the database:

    sqlite> select count(*) from RelDb.localizedString;
    2442
    

    But who knows how to specify multiple database files from the command line to execute the query from the command line?

提交回复
热议问题