Home > Software design >  How can table_name be a property in my post query?
How can table_name be a property in my post query?

Time:07-28

I want this query to be generic on the table_name meaning that there is in my JSON file a new property named "device" which indicates in which table the data will be inserted.

the problem is that in my SQL request I can't specify it. Here is what I tried:

 INSERT INTO ${device} (adc_v, adc_i, acc_axe_x, acc_axe_y, acc_axe_z, temperature, spo2, pa_diastolique, pa_systolique, indice_confiance, received_on, bpm)'  
            'values(${adc_v}, ${adc_i}, ${acc_axe_x}, ${acc_axe_y}, ${acc_axe_z}, ${temperature}, ${spo2}, ${pa_diastolique}, ${pa_systolique}, ${indice_confiance}, ${received_on}, ${bpm})'

here is my JSON on postman:

{
"device": "tag_7z8eq73",
"adc_v": 130,
"adc_i": {{RandomCourant}}, 
"acc_axe_x": {{RandomAccX}}, 
"acc_axe_y": {{RandomAccY}}, 
"acc_axe_z": {{RandomAccZ}}, 
"temperature": {{RandomTemp}}, 
"spo2": {{RandomSpo2}}, 
"pa_diastolique": {{RandomDias}},
"pa_systolique": {{RandomSys}}, 
"indice_confiance": {{RandomIndiceConf}}, 
"received_on": "{{$isoTimestamp}}", 
"bpm": {{RandomBpm}}}

The table name is : tag_7z8eq73

here is the error that is returned to me:

error: erreur de syntaxe sur ou près de « 'tag_7z8eq73' »

Looks like I am close to the solution but there is a syntax problem, the quote ? is my way the right one?

CodePudding user response:

const device = req.body.device;
console.log(device)
return db.none('INSERT INTO ' device ' (adc_v, adc_i, acc_axe_x, acc_axe_y, acc_axe_z, temperature, spo2, pa_diastolique, pa_systolique, indice_confiance, received_on, bpm)'  
        'values(${adc_v}, ${adc_i}, ${acc_axe_x}, ${acc_axe_y}, ${acc_axe_z}, ${temperature}, ${spo2}, ${pa_diastolique}, ${pa_systolique}, ${indice_confiance}, ${received_on}, ${bpm})',
        req.body)

try this

  • Related