DbConnect: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ {{Server function}} {{New feature|3.0120|1.2| '''Available only in MTA SA 1.1.1 r3328 and onwards''' }} This function opens a connection to a database and returns an e...")
 
No edit summary
Line 12: Line 12:


===Required Arguments===
===Required Arguments===
*'''databaseType:''' The type of database. There is currently only one support type: ''sqlite''
*'''databaseType:''' The type of database. There is currently only one supported type: 'sqlite'
*'''host:''' The target to connect to. The format of this depends on the database type. For sqlite it is a [[filepath]] to a sqlite database file.
*'''host:''' The target to connect to. The format of this depends on the database type. For sqlite it is a [[filepath]] to a sqlite database file. If the filepath starts with ":/" then the server's global databases directory is used.


===Optional Arguments===
===Optional Arguments===
Line 21: Line 21:


===Returns===
===Returns===
Returns an element of type "db-connection" unless there are problems, in which case it return ''false''.
Returns a database connection element unless there are problems, in which case it return ''false''.


==Example==
==Example==

Revision as of 17:40, 26 October 2011

Available only in MTA SA 1.1.1 r3328 and onwards This function opens a connection to a database and returns an element that can be used with dbQuery

Syntax

element dbConnect ( string databaseType, string host [, string username, string password, string options ] )

Required Arguments

  • databaseType: The type of database. There is currently only one supported type: 'sqlite'
  • host: The target to connect to. The format of this depends on the database type. For sqlite it is a filepath to a sqlite database file. If the filepath starts with ":/" then the server's global databases directory is used.

Optional Arguments

  • username: Not required for sqlite
  • password: Not required for sqlite
  • options : List of key=value pairs separated by semicolons

Returns

Returns a database connection element unless there are problems, in which case it return false.

Example

This example opens a connection to a sqlite database file in the current resource

local test_db = dbConnect( "sqlite", "file.db" )

This example opens a connection to a sqlite database file in another resource

local test_db = dbConnect( "sqlite", ":resname/file.db" )

This example opens a connection to a sqlite database file in the global databases directory

local test_db = dbConnect( "sqlite", ":/file.db" )

This example opens a connection to a sqlite database file in a sub directroy of the global databases directory

local test_db = dbConnect( "sqlite", ":/example/sub/dir/file.db" )

Requirements

Minimum server version 1.1.1-9.03328
Minimum client version n/a

Note: Using this feature requires the resource to have the above minimum version declared in the meta.xml <min_mta_version> section. e.g. <min_mta_version server="1.1.1-9.03328" />

See Also