Python 如何使用sqlalchemy向postgresql数据库添加大量数据
我需要帮助编写for循环,以便使用sql alchemy将数据手动添加到我的postgresql数据库中 我想做一个data=venture.query.all()并手动获取每个id,然后像嵌套的for循环那样修改数据。但是现在数据太多了,我迷路了 我整个星期都在忙这个。任何帮助都将不胜感激。谢谢 这是我的桌子:Python 如何使用sqlalchemy向postgresql数据库添加大量数据,python,postgresql,sqlalchemy,Python,Postgresql,Sqlalchemy,我需要帮助编写for循环,以便使用sql alchemy将数据手动添加到我的postgresql数据库中 我想做一个data=venture.query.all()并手动获取每个id,然后像嵌套的for循环那样修改数据。但是现在数据太多了,我迷路了 我整个星期都在忙这个。任何帮助都将不胜感激。谢谢 这是我的桌子: class Venue(db.Model): __tablename__ = 'Venue' id = db.Column(db.Integer, primary_
class Venue(db.Model):
__tablename__ = 'Venue'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String())
city = db.Column(db.String(120))
state = db.Column(db.String(120))
address = db.Column(db.String(120))
phone = db.Column(db.String(120))
image_link = db.Column(db.String(500))
facebook_link = db.Column(db.String(120))
num_upcoming_shows = db.Column(db.Integer())
genres = db.Column(db.ARRAY(db.String()))
website = db.Column(db.String(500))
seeking_talent = db.Column(db.Boolean()) #double check this later
seeking_description = db.Column(db.String(500))
past_shows = db.Column(db.Integer())
upcoming_shows = db.Column(db.ARRAY(db.String()))
past_shows_count = db.Column(db.Integer())
upcoming_shows_count = db.Column(db.Integer())
class Artist(db.Model):
__tablename__ = 'Artist'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
city = db.Column(db.String(120))
state = db.Column(db.String(120))
phone = db.Column(db.String(120))
genres = db.Column(db.String(120))
image_link = db.Column(db.String(500))
facebook_link = db.Column(db.String(120))
如果您有性能问题,请尝试进行批量插入或更新。如果您在一条语句中执行多个操作,而不是在每条语句中执行一个操作,那么SQL将做得更好。
#And here is the data that I want to manually add to the database in python.
data1={
"id": 1,
"name": "The Musical Hop",
"genres": ["Jazz", "Reggae", "Swing", "Classical", "Folk"],
"address": "1015 Folsom Street",
"city": "San Francisco",
"state": "CA",
"phone": "123-123-1234",
"website": "https://www.themusicalhop.com",
"facebook_link": "https://www.facebook.com/TheMusicalHop",
"seeking_talent": True,
"seeking_description": "We are on the lookout for a local artist to play every two weeks. Please call us.",
"image_link": "https://images.unsplash.com/photo-1543900694-133f37abaaa5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&q=60",
"past_shows": [{
"artist_id": 4,
"artist_name": "Guns N Petals",
"artist_image_link": "https://images.unsplash.com/photo-1549213783-8284d0336c4f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=300&q=80",
"start_time": "2019-05-21T21:30:00.000Z"
}],
"upcoming_shows": [],
"past_shows_count": 1,
"upcoming_shows_count": 0,
}
data2={
"id": 2,
"name": "The Dueling Pianos Bar",
"genres": ["Classical", "R&B", "Hip-Hop"],
"address": "335 Delancey Street",
"city": "New York",
"state": "NY",
"phone": "914-003-1132",
"website": "https://www.theduelingpianos.com",
"facebook_link": "https://www.facebook.com/theduelingpianos",
"seeking_talent": False,
"image_link": "https://images.unsplash.com/photo-1497032205916-ac775f0649ae?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=750&q=80",
"past_shows": [],
"upcoming_shows": [],
"past_shows_count": 0,
"upcoming_shows_count": 0,
}
data3={
"id": 3,
"name": "Park Square Live Music & Coffee",
"genres": ["Rock n Roll", "Jazz", "Classical", "Folk"],
"address": "34 Whiskey Moore Ave",
"city": "San Francisco",
"state": "CA",
"phone": "415-000-1234",
"website": "https://www.parksquarelivemusicandcoffee.com",
"facebook_link": "https://www.facebook.com/ParkSquareLiveMusicAndCoffee",
"seeking_talent": False,
"image_link": "https://images.unsplash.com/photo-1485686531765-ba63b07845a7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=747&q=80",
"past_shows": [{
"artist_id": 5,
"artist_name": "Matt Quevedo",
"artist_image_link": "https://images.unsplash.com/photo-1495223153807-b916f75de8c5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80",
"start_time": "2019-06-15T23:00:00.000Z"
}],
"upcoming_shows": [{
"artist_id": 6,
"artist_name": "The Wild Sax Band",
"artist_image_link": "https://images.unsplash.com/photo-1558369981-f9ca78462e61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=794&q=80",
"start_time": "2035-04-01T20:00:00.000Z"
}, {
"artist_id": 6,
"artist_name": "The Wild Sax Band",
"artist_image_link": "https://images.unsplash.com/photo-1558369981-f9ca78462e61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=794&q=80",
"start_time": "2035-04-08T20:00:00.000Z"
}, {
"artist_id": 6,
"artist_name": "The Wild Sax Band",
"artist_image_link": "https://images.unsplash.com/photo-1558369981-f9ca78462e61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=794&q=80",
"start_time": "2035-04-15T20:00:00.000Z"
}],
"past_shows_count": 1,
"upcoming_shows_count": 1,
}
data = list(filter(lambda d: d['id'] == venue_id, [data1, data2, data3]))[0]