<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Dezash</id>
	<title>Multi Theft Auto: Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.multitheftauto.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Dezash"/>
	<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/wiki/Special:Contributions/Dezash"/>
	<updated>2026-04-24T00:46:31Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=DbConnect&amp;diff=65877</id>
		<title>DbConnect</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=DbConnect&amp;diff=65877"/>
		<updated>2020-04-18T22:55:49Z</updated>

		<summary type="html">&lt;p&gt;Dezash: Add authentication plugin information for MySQL 8&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;__NOTOC__&lt;br /&gt;
{{Server function}}&lt;br /&gt;
This function opens a connection to a database and returns an element that can be used with [[dbQuery]]. To disconnect use [[destroyElement]].&lt;br /&gt;
{{Note|Connecting and disconnecting many times can have a performance impact on the server. For optimal performance it is recommended that you use dbConnect only once when the resource starts, and share the connection element with the whole script.}}&lt;br /&gt;
{{Note|If you use MySQL 8 or newer, add this line to mysqld.cnf '''&amp;lt;nowiki&amp;gt;default-authentication-plugin=mysql_native_password&amp;lt;/nowiki&amp;gt;'''}}&lt;br /&gt;
==Syntax== &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
element dbConnect ( string databaseType, string host [, string username = &amp;quot;&amp;quot;, string password = &amp;quot;&amp;quot;, string options = &amp;quot;&amp;quot; ] )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
{{OOP||[[Connection]]}}&lt;br /&gt;
===Required Arguments===&lt;br /&gt;
*'''databaseType:''' The type of database. This can be either ''sqlite'' or ''mysql''&lt;br /&gt;
*'''host:''' The target to connect to. The format of this depends on the database type.&lt;br /&gt;
** For SQLite it is a [[filepath]] to a SQLite database file. If the filepath starts with &amp;quot;:/&amp;quot; then the server's global databases directory is used. The file will be created if it does not exist.&lt;br /&gt;
** For MySQL it is a list of key=value pairs separated by semicolons. Supported keys are:&lt;br /&gt;
*** '''dbname''': Name of the database to use e.g. ''dbname=test''&lt;br /&gt;
*** '''host''': Host address e.g. ''host=127.0.0.1''&lt;br /&gt;
*** '''port''': Host port e.g. ''port=1234'' (optional, defaults to standard MySQL port if not used)&lt;br /&gt;
*** '''unix_socket''': Unix socket or named pipe to use (optional)&lt;br /&gt;
***'''charset''': Communicate with the server using a character which is different from the default e.g. ''charset&amp;lt;nowiki&amp;gt;=&amp;lt;/nowiki&amp;gt;utf8'' (optional)&lt;br /&gt;
&lt;br /&gt;
===Optional Arguments===&lt;br /&gt;
*'''username:''' Usually required for MySQL, ignored by SQLite &lt;br /&gt;
*'''password:''' Usually required for MySQL, ignored by SQLite &lt;br /&gt;
*'''options :''' List of key=value pairs separated by semicolons. Supported keys are:&lt;br /&gt;
**'''share''' which can be set to 0 or 1. (Default value for SQLite is &amp;quot;share=1&amp;quot;, for MySQL is &amp;quot;share=0&amp;quot;). When set to 1, the connection is shared and will be used by other calls to dbConnect with the same host string. This is usually a good thing for SQLite connections, but not so good for MySQL unless care is taken.&lt;br /&gt;
**'''batch''' which can be set to 0 or 1. (Default is &amp;quot;batch=1&amp;quot;). When set to 1, queries called in the same frame are automatically batched together which can significantly speed up inserts/updates. The downside is you lose control of the feature that is used to achieve batching (For SQLite it is transactions, for MySQL it is autocommit mode). Therefore, if you use transactions, lock tables or control autocommit yourself, you may want to disable this feature.&lt;br /&gt;
**'''autoreconnect''' which can be set to 0 or 1. (Default value &amp;quot;autoreconnect=1&amp;quot;). When set to 1, dropped connections will automatically be reconnected. Note that session variables (incl. SET NAMES), user variables, table locks and temporary tables will be reset because of the reconnection. So if you use these fancy features, you will need to turn autoreconnect off and cope with dropped connections some other way.&lt;br /&gt;
**'''log''' which can be set to 0 or 1. (Default value &amp;quot;log&amp;lt;nowiki&amp;gt;=&amp;lt;/nowiki&amp;gt;1&amp;quot;). When set to 0, activity from this connection will not be recorded in the [[Server_Commands#debugdb|database debug log file]].&lt;br /&gt;
**'''tag''' (Default value &amp;quot;tag&amp;lt;nowiki&amp;gt;=&amp;lt;/nowiki&amp;gt;script&amp;quot;). A string which helps identify activity from this connection in the [[Server_Commands#debugdb|database debug log file]].&lt;br /&gt;
**'''suppress''' A comma separated list of error codes to ignore. (eg. &amp;quot;suppress&amp;lt;nowiki&amp;gt;=&amp;lt;/nowiki&amp;gt;1062,1169&amp;quot;).&lt;br /&gt;
**'''multi_statements''' Enable multiple statements (separated by a semi-colon) in one query. Use [[dbPrepareString]] when building a multiple statement query to reduce SQL injection risks.&lt;br /&gt;
&lt;br /&gt;
===Returns===&lt;br /&gt;
Returns a database connection element unless there are problems, in which case it return ''false''.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
This example opens a connection to a SQLite database file in the current resource&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
test_db = dbConnect( &amp;quot;sqlite&amp;quot;, &amp;quot;file.db&amp;quot; )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example opens a connection to a SQLite database file in another resource&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
test_db = dbConnect( &amp;quot;sqlite&amp;quot;, &amp;quot;:resname/file.db&amp;quot; )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example opens a connection to a SQLite database file in the global databases directory&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
test_db = dbConnect( &amp;quot;sqlite&amp;quot;, &amp;quot;:/file.db&amp;quot; )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example opens a connection to a SQLite database file in a sub directory of the global databases directory&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
test_db = dbConnect( &amp;quot;sqlite&amp;quot;, &amp;quot;:/example/sub/dir/file.db&amp;quot; )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example opens a connection to a MySQL database called 'frank' at server ip 1.2.3.4 using utf8 character set and allows the connection to be shared. Note that changing the database or other connection dependent settings affect all connections that are shared.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
test_db = dbConnect( &amp;quot;mysql&amp;quot;, &amp;quot;dbname=frank;host=1.2.3.4;charset=utf8&amp;quot;, &amp;quot;username&amp;quot;, &amp;quot;password&amp;quot;, &amp;quot;share=1&amp;quot; )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example opens a connection to a SQLite database is disallows sharing of the connection&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
test_db = dbConnect( &amp;quot;sqlite&amp;quot;, &amp;quot;file.db&amp;quot;, &amp;quot;&amp;quot;, &amp;quot;&amp;quot;, &amp;quot;share=0&amp;quot; )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example output debug message, if the connection with SQLite database was established or not&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
test_db = dbConnect( &amp;quot;sqlite&amp;quot;, &amp;quot;file.db&amp;quot; )&lt;br /&gt;
&lt;br /&gt;
if test_db then&lt;br /&gt;
    outputDebugString( &amp;quot;Connection with database was successfully established.&amp;quot; )&lt;br /&gt;
else&lt;br /&gt;
    outputDebugString( &amp;quot;Connection with database couldn't be established.&amp;quot; )&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The folowing example shows how you could approach a common resource for database operations with exported functions ('''query''' and '''execute'''):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function connect()&lt;br /&gt;
    DBConnection = dbConnect( &amp;quot;mysql&amp;quot;, &amp;quot;dbname=DBNAME;host=HOST;charset=utf8&amp;quot;, &amp;quot;USERNAME&amp;quot;, &amp;quot;PASSWORD&amp;quot; )&lt;br /&gt;
    if (not DBConnection) then&lt;br /&gt;
        outputDebugString(&amp;quot;Error: Failed to establish connection to the MySQL database server&amp;quot;)&lt;br /&gt;
    else&lt;br /&gt;
        outputDebugString(&amp;quot;Success: Connected to the MySQL database server&amp;quot;)&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
addEventHandler(&amp;quot;onResourceStart&amp;quot;,resourceRoot, connect)&lt;br /&gt;
 &lt;br /&gt;
function query(...)&lt;br /&gt;
    local queryHandle = dbQuery(DBConnection, ...)&lt;br /&gt;
    if (not queryHandle) then&lt;br /&gt;
        return nil&lt;br /&gt;
    end&lt;br /&gt;
    local rows = dbPoll(queryHandle, -1)&lt;br /&gt;
    return rows&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
function execute(...)&lt;br /&gt;
    local queryHandle = dbQuery(DBConnection, ...)&lt;br /&gt;
    local result, numRows = dbPoll(queryHandle, -1)&lt;br /&gt;
    return numRows&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function getDBConnection()&lt;br /&gt;
    return DBConnection&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Requirements==&lt;br /&gt;
{{Requirements|1.1.1-9.03328|n/a}}&lt;br /&gt;
&lt;br /&gt;
==Changelog==&lt;br /&gt;
{{ChangelogHeader}}&lt;br /&gt;
{{ChangelogItem|1.3.1-9.04817|Added options 'log', 'tag' and 'suppress'}}&lt;br /&gt;
{{ChangelogItem|1.3.5-9.06386|Added option 'charset'}}&lt;br /&gt;
{{ChangelogItem|1.5.2-9.07972|Added option 'multi_statements'}}&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
{{SQL_functions}}&lt;br /&gt;
&lt;br /&gt;
[[ru:dbConnect]]&lt;/div&gt;</summary>
		<author><name>Dezash</name></author>
	</entry>
</feed>