2

I want to fetch data from one source database and insert it into my targeted database using flask SQL alchemy

Database_connection

 app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:root@localhost/store'
 app.config['SQLALCHEMY_BINDS'] = {'store1' : 'mysql+pymysql://root:root@localhost/store1'}
 app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

Model for first database

class Product(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  name = db.Column(db.String(100), unique=True)
  description = db.Column(db.String(200))
  price = db.Column(db.Float)
  qty = db.Column(db.Integer)

def __init__(self, name, description, price, qty):
   self.name = name
   self.description = description
   self.price = price
   self.qty = qty

Model for second database

class home_products(db.Model):
   __bind_key__= 'store1'
   id = db.Column(db.Integer, primary_key=True)
   name = db.Column(db.String(100), unique=True)
   description = db.Column(db.String(200))
   price = db.Column(db.Float)
   qty = db.Column(db.Integer)

def __init__(self, name, description, price, qty):
   self.name = name
   self.description = description
   self.price = price
   self.qty = qty

Now how can i fetch data from STORE database and can insert in STORE1 database..?

In SQL command :

`INSERT INTO store1.home_products( name, description, price, qty) SELECT name, description, price, qty FROM store.product LIMIT 21, 1; `

this is working i want to learn how to implement this command in python flask sqlalchemy, Any help is appreciated :)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.