mirror of
https://github.com/darksaid98/overextended.github.io.git
synced 2026-12-02 06:28:54 +00:00
update oxmysql usage and "getting started" tips
This commit is contained in:
1 parent
80cfe6b7a2
commit
1363afcd2a
2 files changed
+44
-27
No files matched your search
+21
-23
@@ -1,29 +1,27 @@
|
||||
---
|
||||
title: Usage
|
||||
---
|
||||
# Usage
|
||||
|
||||
## Placeholders
|
||||
|
||||
Safely format queries by using placeholders in the query, and sending arguments in an array or object.
|
||||
Named placeholders are not recommended, and unsupported when using MySQL.prepare.
|
||||
|
||||
```lua
|
||||
-- Lua
|
||||
MySQL.query('SELECT * from users WHERE identifier = ?', {identifier}, function(result)
|
||||
-- callback response
|
||||
-- same as MySQL.Async.fetchAll
|
||||
end)
|
||||
CreateThread(function()
|
||||
local result = MySQL.query.await('SELECT * from users WHERE identifier = ?', {identifier})
|
||||
-- await a promise to resolve
|
||||
-- same as MySQL.Sync.fetchAll
|
||||
end)
|
||||
MySQL.query('SELECT * FROM users WHERE identifier = ?', { 'license:abcdefg' })
|
||||
MySQL.query('SELECT * FROM users WHERE identifier = :identifier', { identifier = 'license:abcdefg' })
|
||||
```
|
||||
|
||||
```js
|
||||
// JS
|
||||
exports.oxmysql.query('SELECT * from users WHERE identifier = ?', [identifier], (result) => {
|
||||
// callback response
|
||||
})(async () => {
|
||||
const result = await exports.oxmysql.query_async('SELECT * from users WHERE identifier = ?', [identifier]);
|
||||
// await a promise to resolve
|
||||
})();
|
||||
exports.oxmysql.query_async('SELECT * from users WHERE identifier = ?', [identifier]).then((result) => {
|
||||
// utilise .then to resolve a promise like a callback
|
||||
});
|
||||
exports.oxmysql.query('SELECT * FROM users WHERE identifier = ?', { 'license:abcdefg' });
|
||||
exports.oxmysql.query('SELECT * FROM users WHERE identifier = :identifier', { identifier: 'license:abcdefg' });
|
||||
```
|
||||
|
||||
## Upsert
|
||||
|
||||
When uncertain if a row should be inserted into the database, or an existing row should be updated, queries should check for duplicate keys.
|
||||
|
||||
```lua
|
||||
MySQL.prepare('INSERT INTO ox_inventory (owner, name, data) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE data = VALUES(data)', { owner, dbId, inventory })
|
||||
```
|
||||
|
||||
This is preferred over checking the existence of a row, then inserting or updating depending on the result.
|
||||
Furthermore, unlike using 'REPLACE INTO', the row is not deleted and re-inserted.
|
||||
Reference in new issue
Block a user