|
1 | 1 | --- |
2 | | -last_modified: 2025-03-10 |
| 2 | +last_modified: 2026-05-14 |
3 | 3 | title: "How to use MySQL2 with Deno" |
4 | 4 | description: "Step-by-step guide to using MySQL2 with Deno. Learn how to set up database connections, execute queries, handle transactions, and build data-driven applications using MySQL's Node.js driver." |
5 | 5 | url: /examples/mysql2_tutorial/ |
@@ -71,49 +71,61 @@ We now have all the data ready to start querying. |
71 | 71 |
|
72 | 72 | ## Querying MySQL |
73 | 73 |
|
74 | | -We can use the same connection.query() method to write our queries. First we try |
75 | | -and get all the data in our `dinosaurs` table: |
| 74 | +We can use the same `connection.query()` method to read data back. The |
| 75 | +`mysql2/promise` driver resolves to a `[rows, fields]` tuple, so destructure to |
| 76 | +pull out the rows directly: |
76 | 77 |
|
77 | 78 | ```tsx |
78 | | -const results = await connection.query("SELECT * FROM `dinosaurs`"); |
79 | | -console.log(results); |
| 79 | +const [rows] = await connection.query("SELECT * FROM `dinosaurs`"); |
| 80 | +console.log(rows); |
80 | 81 | ``` |
81 | 82 |
|
82 | | -The result from this query is all the data in our database: |
| 83 | +This prints every row in the table: |
83 | 84 |
|
84 | 85 | ```tsx |
85 | 86 | [ |
86 | | - [ |
87 | | - { |
88 | | - id: 1, |
89 | | - name: "Aardonyx", |
90 | | - description: "An early stage in the evolution of sauropods." |
91 | | - }, |
92 | | - { |
93 | | - id: 2, |
94 | | - name: "Abelisaurus", |
95 | | - description: `Abel's lizard" has been reconstructed from a single skull.` |
96 | | - }, |
97 | | - { id: 3, name: "Deno", description: "The fastest dinosaur that ever lived." } |
98 | | - ], |
| 87 | + { |
| 88 | + id: 1, |
| 89 | + name: "Aardonyx", |
| 90 | + description: "An early stage in the evolution of sauropods.", |
| 91 | + }, |
| 92 | + { |
| 93 | + id: 2, |
| 94 | + name: "Abelisaurus", |
| 95 | + description: "Abels lizard has been reconstructed from a single skull.", |
| 96 | + }, |
| 97 | + { id: 3, name: "Deno", description: "The fastest dinosaur that ever lived." }, |
| 98 | +]; |
99 | 99 | ``` |
100 | 100 |
|
101 | | -If we want to just get a single element from the database, we can change our |
102 | | -query: |
| 101 | +### Parameterized queries |
| 102 | + |
| 103 | +To filter by a value, do not paste it directly into the SQL string — that's how |
| 104 | +SQL injection bugs get shipped. Use `connection.execute()` with `?` |
| 105 | +placeholders. The driver prepares the statement on the server and binds the |
| 106 | +values separately, so anything you pass in the values array is treated strictly |
| 107 | +as data, never parsed as SQL: |
103 | 108 |
|
104 | 109 | ```tsx |
105 | | -const [results, fields] = await connection.query( |
106 | | - "SELECT * FROM `dinosaurs` WHERE `name` = 'Deno'", |
| 110 | +const name = "Deno"; // imagine this came from a user request |
| 111 | +const [rows] = await connection.execute( |
| 112 | + "SELECT * FROM `dinosaurs` WHERE `name` = ?", |
| 113 | + [name], |
107 | 114 | ); |
108 | | -console.log(results); |
| 115 | +console.log(rows); |
109 | 116 | ``` |
110 | 117 |
|
111 | | -Which gives us a single row result: |
| 118 | +Which gives us a single matching row: |
112 | 119 |
|
113 | 120 | ```tsx |
114 | 121 | [{ id: 3, name: "Deno", description: "The fastest dinosaur that ever lived." }]; |
115 | 122 | ``` |
116 | 123 |
|
| 124 | +`?` placeholders are positional, so the values must appear in the same order as |
| 125 | +the placeholders in the SQL. The same pattern works for `INSERT`, `UPDATE`, and |
| 126 | +`DELETE`; `mysql2` also caches each prepared statement, so repeated calls with |
| 127 | +different values skip the parse step on subsequent runs. |
| 128 | + |
117 | 129 | Finally, we can close the connection: |
118 | 130 |
|
119 | 131 | ```tsx |
|
0 commit comments