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!

Edited by dil_bert
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.