Home > Back-end >  Access database instance from command prompt
Access database instance from command prompt

Time:11-28

I’ve tried looking for a similar question but couldn’t find one. Sorry if this question has already been asked.

To my question, I’ve created a flask application that creates a database instance using SQLalchemy. My question is how do I access this .db file from my command prompt? I’d like for example get a list of all the users. In my app I’d write User.query.all() but it gets messy quickly. I’d rather just do it from the command line on my Mac if possible.

Thanks in advance!!

What I’ve tried from the terminal:

  • sqlite3 database.db
  • User.query.all() (nothing happens)

And then:

  • python3 main.py
  • User.query.all() (nothing happens)

CodePudding user response:

sqlite3 is correct to open the database file. you're going to need to use SQL statements from terminal:

sqlite3 database.db
show tables;
select * from Users; # depends on your table names, obviously
insert into Users... # and so on, please refer to sqlite3 manual if you need to or ask a direct question about what you're trying to manually do to the database

The python3 command is just going to run the interpreter across main.py.. I think flask is started with flask run but you're probably looking for flask shell .. it's been a minute for me on flask, sorry.

https://www.sqlite.org/cli.html

  • Related