Python Forum
Inserting into Sqlalchemy when number of variables is unknown.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inserting into Sqlalchemy when number of variables is unknown.
#1
I cant give lots of codding information on this problem as i am pretty stumped by it. I am using a FLASK framework.

So I have created a table with: X amount of rows and 8 columns. A user will be entering information into the table.

They will have to enter information into multiple rows, so i have added a nice 'Add row' button with javascript so they can do so. Therefore as a programming the table 'size' ( number of rows) is dynamic and such my issue has emerged.

Once they have filled out all the rows ( Its quite likely they create between 5-30 rows) and submit the form I need to save this information to a mysql table.

My best guess is that you will have to loop the sqlalchemy upload query multiple times, uploading 1 row at a time. Similar to how you would upload a CSV file to sqlalchemy.

However I do not know how I would give each input a unique ID and thus allow the form to be uploaded.
Reply
#2
(Sep-07-2018, 12:30 PM)KirkmanJ Wrote: However I do not know how I would give each input a unique ID and thus allow the form to be uploaded.
Shouldn't the id be auto-increment field in the mysql table? i.e. it will be autopopulated
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Sorry I guess that is misleading, and I can provide HTML so i should of, I do apologise:
<tr>
    <td class = 'sct_components_input'> <input class = 'sct_components_input_5'> </input> </td>
    <td class = 'sct_components_input'> <input class = 'sct_components_input_5'> </input> </td>
    <td class = 'sct_components_input'> <input class = 'sct_components_input_10'> </input> </td>
    <td class = 'sct_components_input'> <input class = 'sct_components_input_10'> </input> </td>
    <td class = 'sct_components_input'> <textarea class = 'sct_components_input_15'> </textarea> </td>
    <td class = 'sct_components_input'> <input class = 'sct_components_input_5'> </input> </td>
    <td class = 'sct_components_input'> <input class = 'sct_components_input_5'> </input> </td>
    <td class = 'sct_components_input'> <textarea class = 'sct_components_input_15'> </textarea> </td>
</tr>
This is a row of my table, every time someone presses the 'New row' button this gets inserted into the bottom of the table, currently I do not 'name' the inputs as i am unsure whether that is correct. When i said 'ID' i meant 'name' as in the inputs html name ( <input NAME=XXX > ), The aspect which would be used in a request.form[XXX] to pull the information into a python script.

To my knowledge, each input would have to have a unique 'name' in order to upload to a sqlalchemy database ?? and somehow a loop would need to be formed to ensure no input was missed. But im guessing.
Reply
#4
Solution from:
https://stackoverflow.com/questions/5222...is-unknown


"Assuming your dynamically made table looks something like this:

<form>
  <table>
    <tr>
      <td><input name="first_name"></td>
      <td><input name="age"></td>
    </tr>
    <tr>
      <td><input name="first_name"></td>
      <td><input name="age"></td>
    </tr>
  </table>
</form>
Then, in python you can access all the data as follows:

first_names = request.form.getlist('first_name')
ages = request.form.getlist('age')
for first_name, age in zip(first_names, ages):
    person = Person(first_name, age)
    db.session.add(person)
db.session.commit()
"

Sorry if im not allowed to post a link to another python help forum, but i dont want to claim credit for something that is not mine
Reply
#5
(Sep-07-2018, 03:33 PM)KirkmanJ Wrote: Sorry if im not allowed to post a link to another python help forum, but i dont want to claim credit for something that is not mine
Thanks for posting the solution,there is no problem to post link to other forum that has a solution.
Especially if done in this honest way,often people get a answer a other place and don't post back.
Reply
#6
Reason for me posting :). Too many times you see someone with the same question as yours ( but 2-3 years old), but the thread goes quiet half way through a solution, or does not post one at all.

Also worth noting, this method did not work with my javascript: 'Add Row' script. That is an issue I am still trying to figure out a solution for, but if you can make all the rows for a user in the html template / there is no need for the user to make more. This method works really really well.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020