Skip to content

What is the correct syntax for including SQL values and rowsAsArray option in a query? #3094

Closed

Description

@millerbryan

I've looked at all the examples in the docs which use rowsAsArray and it isn't clear how to specify it in a query (rather than in the connection). Is this possible?

// simplified code
const sql = "select county_name from static_web_data.state_counties where state_name = ?";
const state = "TX"

        try {
          const [rows, fields] = await pool.query({sql, [state], rowsAsArray: true});
          if (rows) {
            return rows;
          }
        } catch (err) {
          console.log(err);
        }

Activity

  1. wellwelwel commented on Oct 3, 2024

    @wellwelwel
    Collaborator

    Hi, @millerbryan 🙋🏻‍♂️

    You can set it in both connection and query levels:

    const conn = await mysql.createConnection({
    host: 'localhost',
    database: 'test',
    user: 'root',
    rowsAsArray: true,
    });

    try {
    const [results, fields] = await conn.query({
    sql: 'SELECT 1 AS `foo`, 2 AS `foo`',
    rowsAsArray: true,
    });
    console.log(results); // in this query, results will be an array of arrays rather than an array of objects
    console.log(fields); // fields are unchanged
    } catch (err) {
    console.log(err);
    }

    You can see the docs#array-results section for more details.

  2. Repository owner locked and limited conversation to collaborators on Oct 3, 2024
  3. converted this issue into a discussion #3095 on Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions