No SQLlite database in directory after calling create_engine in SQLAlchemy - python

After navigating to a directory and typing the commands in the Python shell:
from sqlalchemy import *
db = create_engine('sqlite:///tutorial.db')
I do not see a database file called tutorial.db file in the directory. Do I have to use a different command to create the actual database file and save it?

SQLAlchemy's Engine object lazily constructs the actual underlying database connection, waiting until the first database operation before trying to connect to the database.
Try running a query or creating a table and see if the database appears.

Related

How do I create a .db file from an sql script?

I'm creating a gui in python to manipulate stored records and I have the mysql script to set up the database and enter all information. How do I get from the mysql script to the .db file so that python can access and manipulate it?
db files are SQLite databases most of the time. What you are trying to do is converting a dumped MySQL database into an SQLite database. Doing this is not trivial, as I think both dialects are not compatible. If the input is simple enough, you can try running each part of it using an SQLite connection in your Python script. If it uses more complex features, you may want to actually connect to a (filled) MySQL database and fetch the data from there, inserting it back into a local SQLite file.

Postgresql sessions from psql and psycopg2

I am running two terminal sessions, in the first one I've opened psql, and in the second one ipython with psycopg2 imported.
I'm connected to the same db in both sessions. When I update a table through ipython/psycopg2, psql session queries won't reflect the updates (i.e. I add a row in a table via psycopg2, and psql still fetches no rows).
What am I doing wrong?
Probably, after executing update you didn't execute commit() (it makes the changes to the database persistent) on the connection object.
See the first example in the docs http://initd.org/psycopg/docs/usage.html

How to drop table and recreate in amazon RDS with Elasticbeanstalk?

My database on Amazon currently has only a little data in it (I am making a web app but it is still in development) and I am looking to delete it, make changes to the schema, and put it back up again. The past few times I have done this, I have completely recreated my elasticbeanstalk app, but there seems like there is a better way. On my local machine, I will take the following steps:
"dropdb databasename" and then "createdb databasename"
python manage.py makemigrations
python manage.py migrate
Is there something like this that I can do on amazon to delete my database and put it back online again without deleting the entire application? When I tried just deleting the RDS instance a while ago and making a new one, I was having problems with elasticbeanstalk.
The easiest way to accomplish this is to SSH to one of your EC2 instances, that has acccess to the RDS DB, and then connect to the DB from there. Make sure that your python scripts can read your app configuration to access the configured DB, or add arguments for DB hostname. To drop and create your DB, you must just add the necessary arguments to connect to the DB. For example:
$ createdb -h <RDS endpoint> -U <user> -W ebdb
You can also create a RDS snapshot when the DB is empty, and use the RDS instance actions Restore to Point in Time or Migrate Latest Snapshot.
I had the same problem and came up with a workaround. In your python code just add and run the following method when deploying your app the next time:
FOR SQLALCHEMY AFTER VERSION 2.0
from sqlalchemy import create_engine, text
tables = ["table1_name", "table2_name"] # the names of the tables you want to delte
engine = create_engine("sqlite:///example.db") # here you create your engine
def delete_tables(tables):
for table in tables:
sql = text(f"DROP TABLE IF EXISTS {table} CASCADE;") # CASCADE deltes the tables even if they had some connections to other tables
with engine.connect() as connection:
with connection.begin():
connection.execute(sql)
delete_tables(tables) # Comment this line out after running it once.
FOR SQLALCHEMY BEFORE VERSION 2 (I guess)
def delete_tables(tables):
for table in tables:
engine.execute(f"DROP TABLE IF EXISTS {table} CASCADE;")
delete_tables(tables) # Comment this line out after running it once.
After you deployed and ran this code 1 time, all your tables will be deleted.
IMPORTANT: Delete or comment out this code after that, otherwise you will delete all your tables every time when you deploy your code to AWS

Unknown database error after creating it (sqlalchemy)

I'm getting the next error while trying to run my Flask app:
sqlalchemy.exc.OperationalError
OperationalError: (_mysql_exceptions.OperationalError) (1049, "Unknown database '/home/gerardo/Documentos/python_web_dev/flask-intro/app2.db'")
so it seems like there is no database.. but I ran the next scripts using sqlalchemy_utils and everything was ok:
engine = create_engine("mysql://root:#localhost/home/gerardo/Documentos/python_web_dev/flask-intro/app2.db")
create_database(engine.url)
but still I get the error..
You have confused MySQL, a database server, with SQLite, a database in a file. You created a SQLite file, but are trying to tell MySQL to connect to it, which makes no sense.
Use the sqlite dialect in the connection string.
You can avoid typing the whole path (and tying the app to that path) by pointing to the file relative to the app's location.
import os
db_path = os.path.realpath(os.path.join(os.path.dirname(__file__), 'app2.db'))
engine = create_engine('sqlite://{}'.format(db_path))
Consider using Flask-SQLAlchemy rather than trying to manage the database yourself.

Can i access different database in django than other default database

I have the database backup script in python which inserts some data in mysql database .
Now my Django is in different database.
How can i access different database because i don't have any objects in Models.py.
i want to display some data in django interface
Yes, you can setup multiple database and access every one of them.
you can get the specified database connection cursor using this:
from django.db import connections
cursor = connections['my_db_alias'].cursor()
where my_db_alias is your another db alias .
check the doc:
https://docs.djangoproject.com/en/1.3/topics/db/multi-db/

Categories

Resources