简体   繁体   中英

Convert array of objects to sql query insert into

I'm trying to convert an array of objects to use it to do a query INSERT INTO

This is the array:

 [{ topicId: 4, provider: 'ex', postId: 'tT-t1IfwuyI' },
  { topicId: 4, provider: 'ex2', postId: '1382658099542084903' },
  { topicId: 4, provider: 'ex3', postId: 'BdePFBBBUdI' },
  { topicId: 4, provider: 'ex', postId: '-7UZr-wpL_Y' }]

I'm trying to do a map in array to do this...but its returns me just an array of postIds:

let sql = array.map(item => (item.topicId.toString(), item.provider.toString(), item.postId.toString()))

the result expect is a string:

( topicId: 4, provider: 'ex', postId: 'tT-t1IfwuyI' ),
      ( topicId: 4, provider: 'ex2', postId: '1382658099542084903' ),
      ( topicId: 4, provider: 'ex3', postId: 'BdePFBBBUdI' ),
      ( topicId: 4, provider: 'ex', postId: '-7UZr-wpL_Y' )

Im want to convert them to insert in this query:

query("INSERT INTO table (topic_id, provider, post_id) VALUES " + sql)

How can I do this?

I had a problem, when I wanted to send the query, mysql didn't recognize my strings because they didn't have apostrophe, so i solved it this simple way, based on Prince Hernandez answer.

const array = [
  {
idservicio: 1,
precio: 20.5,
comentario: 'hello',
descuento: 0,
total: 20.5,
estado: 1
 },
{
idservicio: 2,
precio: 20.5,
comentario: 'World',
descuento: 0,
total: 20.5,
estado: 1
},
{
idservicio: 3,
precio: 20.5,
comentario: '..........',
descuento: 0,
total: 20.5,
estado: 1
}
]
//In the string comentario item, manually add the apostrophes.

let sql = array.map(item => `(${item.idservicio}, ${item.precio}, ${"'"}${item.comentario}${"'"}, ${item.descuento}, ${item.total}, ${item.estado})`)

//array with items.
console.log(sql);


const finalQuery = "INSERT INTO pos.detalle_orden (idservicio, precio, comentario, descuento,total,estado) VALUES " + sql
console.log(finalQuery)
console.log("query("+finalQuery+")")

you are close, using `` with ${} to put the values, you can create the desired string for each item on the array.

 const array = [{ topicId: 4, provider: 'ex', postId: 'tT-t1IfwuyI' }, { topicId: 4, provider: 'ex2', postId: '1382658099542084903' }, { topicId: 4, provider: 'ex3', postId: 'BdePFBBBUdI' }, { topicId: 4, provider: 'ex', postId: '-7UZr-wpL_Y' } ] let sql = array.map(item => `(${item.topicId}, ${item.provider}, ${item.postId})`) //array with items. console.log(sql); const finalQuery = "INSERT INTO table (topic_id, provider, post_id) VALUES " + sql console.log(finalQuery) console.log("query("+finalQuery+")")

Do you mean something like this?

 var arr = [ { topicId: 4, provider: 'ex', postId: 'tT-t1IfwuyI' }, { topicId: 4, provider: 'ex2', postId: '1382658099542084903' }, { topicId: 4, provider: 'ex3', postId: 'BdePFBBBUdI' }, { topicId: 4, provider: 'ex', postId: '-7UZr-wpL_Y' } ]; var sql = arr.map(item => ("(" + item.topicId + ", " + item.provider + ", " + item.postId + ")")) console.log(sql)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM