mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
db.pg: add parameter syntax to docs (#19003)
This commit is contained in:
parent
fd81bae361
commit
a609d6c9d1
@ -51,3 +51,14 @@ Read this section to learn how to install and connect to PostgreSQL
|
|||||||
[*Windows*](https://www.postgresqltutorial.com/install-postgresql);
|
[*Windows*](https://www.postgresqltutorial.com/install-postgresql);
|
||||||
[*Linux*](https://www.postgresqltutorial.com/postgresql-getting-started/install-postgresql-linux);
|
[*Linux*](https://www.postgresqltutorial.com/postgresql-getting-started/install-postgresql-linux);
|
||||||
[*macOS*](https://www.postgresqltutorial.com/postgresql-getting-started/install-postgresql-macos).
|
[*macOS*](https://www.postgresqltutorial.com/postgresql-getting-started/install-postgresql-macos).
|
||||||
|
|
||||||
|
## Using Parameterized Queries
|
||||||
|
|
||||||
|
Parameterized queries (exec_param, etc.) in V require the use of the following syntax: ($n).
|
||||||
|
|
||||||
|
The number following the $ specifies which parameter from the argument array to use.
|
||||||
|
|
||||||
|
```v ignore
|
||||||
|
db.exec_param_many('INSERT INTO users (username, password) VALUES ($1, $2)', ['tom', 'securePassword']) or { panic(err) }
|
||||||
|
db.exec_param('SELECT * FROM users WHERE username = ($1) limit 1', 'tom') or { panic(err) }
|
||||||
|
```
|
||||||
|
@ -251,7 +251,7 @@ pub fn (db DB) exec_one(query string) !Row {
|
|||||||
return row
|
return row
|
||||||
}
|
}
|
||||||
|
|
||||||
// exec_param_many executes a query with the provided parameters
|
// exec_param_many executes a query with the parameters provided as ($1), ($2), ($n)
|
||||||
pub fn (db DB) exec_param_many(query string, params []string) ![]Row {
|
pub fn (db DB) exec_param_many(query string, params []string) ![]Row {
|
||||||
unsafe {
|
unsafe {
|
||||||
mut param_vals := []&char{len: params.len}
|
mut param_vals := []&char{len: params.len}
|
||||||
@ -265,12 +265,12 @@ pub fn (db DB) exec_param_many(query string, params []string) ![]Row {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// exec_param2 executes a query with 1 parameter, and returns either an error on failure, or the full result set on success
|
// exec_param2 executes a query with 1 parameter ($1), and returns either an error on failure, or the full result set on success
|
||||||
pub fn (db DB) exec_param(query string, param string) ![]Row {
|
pub fn (db DB) exec_param(query string, param string) ![]Row {
|
||||||
return db.exec_param_many(query, [param])
|
return db.exec_param_many(query, [param])
|
||||||
}
|
}
|
||||||
|
|
||||||
// exec_param2 executes a query with 2 parameters, and returns either an error on failure, or the full result set on success
|
// exec_param2 executes a query with 2 parameters ($1) and ($2), and returns either an error on failure, or the full result set on success
|
||||||
pub fn (db DB) exec_param2(query string, param string, param2 string) ![]Row {
|
pub fn (db DB) exec_param2(query string, param string, param2 string) ![]Row {
|
||||||
return db.exec_param_many(query, [param, param2])
|
return db.exec_param_many(query, [param, param2])
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user