'Syntax to take user input from SQL and insert those values into a table in MySQL?
So, I'm not sure how to go about this. I've installed and imported MySQL Connector.
fit=sql.connect(host='localhost',user='root',passwd='xez23%',database='fitnesscentre')
uID= int(input('Enter user ID: '))
pwd= input('Enter password: ')
wgt1=int(input('Enter your weight: '))
bmi= float(input('Enter BMI: '))
actlevel= int(input('Enter activity level in minutes: '))
upd= input('Update? Y/N')
if upd=='Y':
d=fit.cursor()
rec1='UPDATE userstats SET weight=%s, BMI=%s, activitylevel=%s WHERE ID=%d; '
What do I do next? This is the table userstats: userstats
Solution 1:[1]
i use this to input:
import mysql.connector
conn = mysql.connector.connect(
user='root', password='*******', host='127.0.0.1', database='data')
cursor = conn.cursor()
insert_stmt = (
"insert into datauser(nama_depan,nama_belakang,email,iat,at_hash)"
"VALUES (%s, %s, %s, %s, %s)"
)
data = ('value', 'value', 'value', value, 'value')
try:
cursor.execute(insert_stmt, data)
conn.commit()
except:
conn.rollback()
print("Data inserted")
conn.close()
Solution 2:[2]
What to do next is call execute() on the cursor. The first argument is the SQL string with parameter placeholders. The next argument is a tuple containing the values you want to use for those placeholders, in the same order as the placeholders in the SQL string.
d.exec(rec1, (wgt1, bmi, actlevel, uID,))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hansen Marcelino Azali |
| Solution 2 | Bill Karwin |
