'Str' Object Is Not Callable While Execute Sql Command [Closed]

I'm try to get 3 data from one function. X,Y,Z data from get_gyro_data() function. When i'm try to execute these datas in order to insert tab TAB_MPU and columns GX,GY,GZ . I use i2c for gettin data from MPU6050

  math functions 
  math functions
  .
  .
  .
  x= x/scaler
  y= y/scaler
  z=z/scaler
  return {'x': x, 'y': y, 'z': z}

def get_all_data(self):
  gyro = get_gyro_data()

After get gyro data im tryin to write database like ;

while True :
    mpu = MPU6050(0x68) //my sensor
    gyro_data = mpu.get_gyro_data()
    print(gyro_data['x'])
    print(gyro_data['y'])
    print(gyro_data['z'])
    Time = (time.strftime("%H:%M:%S"))
    Date = (time.strftime("%Y-%m-%d"))
    print (Time + " - " + Date)
    sql = ("""INSERT INTO `TAB_TEMP` (`Date`,`Time`,`JX`,`JY`,`JZ`,`IX`,`IY`,`IZ`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"""(Date,Time,accel_data['x'],accel_data['y'],accel_data['z'],gyro_data['x'],gyro_data['y'],gyro_data['z'])) 
    cursor.execute(sql)  // execute sql command

When i debug program, I got exception TypeError : 'str' object is not callable at sql= (""" INSERT .... ) line

1

On that line you're basicly doing this: ""()

You'll have to separate the SQL query string and the arguments, best in the cursor.execute() call. Something like this:

sql = """INSERT INTO `TAB_TEMP` (`Date`,`Time`,`JX`,`JY`,`JZ`,`IX`,`IY`,`IZ`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"""
args = (Date,Time,accel_data['x'],accel_data['y'],accel_data['z'],gyro_data['x'],gyro_data['y'],gyro_data['z'])
cursor.execute(sql, args)

Also depending on your database connector you'd need question marks instead of the string formatter.

1
Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest