Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with homework
#1
I need help with this part for the code where i need to make a function that makes modification in the db file.

@app.route('/update/<commit>', methods=['GET', 'POST'])
def update_record(commit):
    if request.method == 'GET':
        return render_template('whattoupdate.html', whichpath='/update/search')
    elif request.method == 'POST':
        if commit == 'search':
           searchtext=request.form['searchtext']
           updatelist=[]
           index=0
           with open('mycontacts.db', 'r') as db:
               for line in db:
                   line=line.rstrip()
                   if searchtext in line:
                       updatelist.append([index, line])
                   index+=1
           return render_template('visualisecontacts.html', whichpath=url_for('update_record', commit='write'), contactdb=updatelist)
        elif commit == 'write':
            return "I need help with this part"
        
        else:
           abort(400)
    else:
        return "Invalid request"
Reply
#2
What is "write" supposed to do?
Reply
#3
(Apr-28-2020, 05:56 PM)deanhystad Wrote: What is "write" supposed to do?
To modify the db. The project is to make contact book and this is the part of modifying a record. "search" is to find the record and "write" is to modify it.
Reply
#4
How do you modify the db? What command would you use?
Reply
#5
what kind of file is the db file? Do you get any errors? Best would be to provide sample input and expected result.
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
#6
(Apr-28-2020, 06:03 PM)deanhystad Wrote: How do you modify the db? What command would you use?

?
with open('mycontacts.db', 'w') as db:
                db.write()
Reply
#7
And what are you going to write to that file? Since I see nothing else I expect it is the contents of something on your web page? How do you convert information from the web page to strings you can write to a file? I'm blind here without any information about what your program does, where information from this file goes, or knowing what is in the html files. Its like that hot/cold game. Am I getting warmer?
Reply
#8
(Apr-28-2020, 06:23 PM)deanhystad Wrote: And what are you going to write to that file? Since I see nothing else I expect it is the contents of something on your web page? How do you convert information from the web page to strings you can write to a file? I'm blind here without any information about what your program does, where information from this file goes, or knowing what is in the html files. Its like that hot/cold game. Am I getting warmer?

https://github.com/mitacheto/Contact-Book
Reply
#9
Ok, lets make sure I've got this right. You have a contact book. The information is saved in 'mycontacts.db'. You already have a function for adding contacts to the database (create_record). Now you are writing update_record that does something similar, but only changing entries in 'mycontacts.db', not adding any. Is that right?

So now you are wondering how you can read through the contacts file until you find the record to update and then write the updated information without messing up the file. Is that right?
Reply
#10
(Apr-28-2020, 06:47 PM)deanhystad Wrote: Ok, lets make sure I've got this right. You have a contact book. The information is saved in 'mycontacts.db'. You already have a function for adding contacts to the database (create_record). Now you are writing update_record that does something similar, but only changing entries in 'mycontacts.db', not adding any. Is that right?

So now you are wondering how you can read through the contacts file until you find the record to update and then write the updated information without messing up the file. Is that right?

yes

maybe something like this
elif commit == 'write':
            con = sqlite3.connect('mycontacts.db')
            def sql_update(con):
                cursorObj = con.cursor()
                cursorObj.execute('update *')
                con.commit()
                sql_update(con)
Reply


Forum Jump:

User Panel Messages

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