def root(): # after creating db, we want to display this information to the client # first connect to db mysql = connectToMySQL('crpets') query = "SELECT * FROM pets" pets = mysql.query_db(query) return render_template('index.html', pets=pets)
def delete(id): print(id) # connect to db! mysql = connectToMySQL('crpets') # insert your delete query! add id at the end to tell MySQL which one to delete query = "DELETE FROM pets WHERE id=" + id mysql.query_db(query) return redirect('/')
def showEdit(id): #we need to show the information to the client first #connect to DB! # mysql = connectToMySQL('crpets') # # make query # query = "SELECT * FROM pets WHERE id="+id # #step two: connect to db # pet = mysql.connectToMySQL('crpets') # return render_template("edit.html", pet=pet) mysql = connectToMySQL("crpets") query = "SELECT * FROM pets where id =" + id pet = mysql.query_db(query) return render_template("edit.html", pet=pet)
def create(): #print insert query. this will print in your terminal!! print(request.form['name']) print(request.form['type']) #connect to db! mysql = connectToMySQL('crpets') #now to call on the insert query #insert into ___TABLE NAME____ query = "INSERT into pets (name, type, created_at, updated_at) VALUES (%(name)s, %(type)s, NOW(), NOW())" #do the data object table data = {'name': request.form['name'], 'type': request.form['type']} mysql.query_db(query, data) return redirect('/')
def edit(): print(request.form['name']) print(request.form['type']) print(request.form['id']) #update table , set name = value. no need for created at but need ID! query = "UPDATE pets name=%(name)s, type=%(type)s, updated_at=NOW() WHERE id =%(id)s" data = { 'name': request.form['name'], 'type': request.form['type'], 'id': request.form['id'] } #connect to database!! mysql = connectToMySQL('crPets') mysql.query_db(query, data) return redirect('/')