Jump to content

how to insert a list of tuples into the db using Peewee


dil_bert

Recommended Posts

hello  dear php-experts,

 

 

i am currently workin on a litte contact manager.

 

and i want to store the data in a db.  Note: i work with Python at the moment. So this is related to Python since i want to dive into Peewee.

 

What is aimed:  i am musing on how to insert a list of tuples into the db using Peewee (/note the pyton object-layer-model). My db is setup as follows: class Stats(Model):
 

name = TextField(index=True)
gender = TextField()
age = TextField()
city = TextField()
state = TextField()

class Meta:
database = db
My list of tuples looks like this:
records = [("Joe Smoe", "Male", 34, "Joe Fracer", " Staten Island")],
[("Jane Doe", "Female", 21, "Jane bell", "Capetown")]

Well - i wonder how to work out the concrete db-inserts.

 

Should I iterate over the list inserting one row at a time?

Can this be bulk inserted or does it need to be made into a dictionary to achieve that solution?

 

 

i have rows like so (see below). I can do this manually. Of course:

rows = [
{"name": "Joe Jackson", "gender": "Male", "age": 44,
"city": "Billi  Kid ", "state": "Mosqow"},
{"name": "Howard Foster", "gender": "Female", "age": 22,
"city": "Jane Austen", "state" :"Capetown"},
...
]

Well - one can do this manually.

But honestly: isnt it  much better and a lot more efficient to use peewee's Model.insert_many function to do a bulk insert of the data into the database - well with a single SQL INSERT statement?

 

 

Love to hear from you experts,

your dilbert!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.