question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Any simple function to inserto a row into table from JS Object?

See original GitHub issue

Right now I use this cumbersome approach when I want to add a row whose data is in a JS Object

Adding a row to a table:

var db = mysql.createConnection(DBInfo)

var databaseObj = {val1: '1', name: 'John', age: 40}

var query = 'INSERT INTO my_table ('
var databaseKeys = Object.keys(databaseObj)
for (let i = 0; i < databaseKeys.length; i++) {
  query += databaseKeys[i] + (i !== databaseKeys.length - 1 ? ', ' : ')')
}
query += ' ' + 'VALUES('
for (let i = 0; i < databaseKeys.length; i++) {
  query += '\'' + databaseObj[databaseKeys[i]] + '\'' + (i !== databaseKeys.length - 1 ? ', ' : ')')
}

db.query(query, function (err, results, fields) {...

Is there any simpler or neater way to add a row into a table, where such row data is in a JS Object?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dougwilsoncommented, Jun 15, 2021

Here is your original code written that way if it helps:

var db = mysql.createConnection(DBInfo)

var databaseObj = {val1: '1', name: 'John', age: 40}

var query = 'INSERT INTO my_table SET ?'

db.query(query, databaseObj, function (err, results, fields) {...
0reactions
jfoclpfcommented, Jun 15, 2021

Thanks again, it worked 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to insert a row in an HTML table body in JavaScript
If you want to add a row into the tbody , get a reference to it and call its insertRow method. var tbodyRef...
Read more >
JavaScript function to add rows to a table - w3resource
var table = document.getElementById('sampleTable'), row = table.insertRow(0), cell0 = row.insertCell(0), cell1 = row.
Read more >
HTMLTableElement.insertRow() - Web APIs | MDN
The HTMLTableElement.insertRow() method inserts a new row ( <tr> ) in a given <table> , and returns a reference to the new row....
Read more >
HTML DOM Table insertRow() Method - W3Schools
The insertRow() method creates an empty <tr> element and adds it to a table. The insertRow() method inserts the new row(s) at the...
Read more >
Back To The Basics: How To Generate a Table With JavaScript
function generateTableHead(table, data) { let thead = table.createTHead(); let row = thead.insertRow() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found