Topics   All   Mac OS X (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server (Not)   Client   Old   Guides
New in version: 5.4   6.0   6.1   6.2   6.3   6.4   6.5   7.0   7.1   7.2    Statistic  

SQL.NewConnection

Initialize a new SAConnection object and returns Connection Reference.

Component: SQL
Version: 2.6
Mac OS X: Yes
Windows: Yes
Server: Yes
FileMaker Cloud: Yes

MBS( "SQL.NewConnection" { ; ID } )

Parameters

Parameter Description Example value
ID Optional
The ID to use instead of default one.
Must be unique. If none is provided, the plugin will create one.

Result

An error or a connection reference number.

Description

Initialize a new SAConnection object and returns Connection Reference.

The Connection represents a database connection. You must have a connection before you can operate on the database - furthermore the connection needs to be connected. It is important to note that connection objects are not connected during construction - it is up to the user to explicitly call Connect method for the object after it is created and before it is used. Once connected, a connection object is normally shared by the other objects that require a connection (e.g. commands).

Use SQL.FreeConnection to release connection after you finished.

read also:
http://www.sqlapi.com/OnLineDoc/Connection.html

SQL connection reference numbers are starting at 23000 and counting up for each new connection.

Examples

Create new connection

$Connection = MBS("SQL.NewConnection")

Connect to MySQL and run a query:

#Start a new connection
Set Variable [$Connection; Value:MBS("SQL.NewConnection")]
#Tell plugin where MySQL library is (put it where you like)
Set Variable [$result; Value:MBS("SQL.SetConnectionOption"; $Connection; "MYSQL.LIBS"; "/Users/cs/Desktop/libmysqlclient.dylib")]
#Connect to a mysql database:
Set Variable [$result; Value:MBS("SQL.Connect"; $Connection; "192.168.11.51@Server_Config"; "user"; "password"; "MySQL")]
If [$result ≠ "OK"]
    #Connection failed
    Show Custom Dialog ["Error: " & $result]
    Set Variable [$result; Value:MBS("SQL.FreeConnection"; $Connection)]
    Halt Script
Else
    #Create a query:
    Set Variable [$Command; Value:MBS("SQL.NewCommand"; $Connection; "SELECT * FROM Server_Config where ServerName=:Name")]
    #If you use parameters, you can fill them here
    Set Variable [$r; Value:MBS("SQL.SetParamAsText"; $Command; "Name"; "MacMini")]
    #Execute it
    Set Variable [$result; Value:MBS("SQL.Execute"; $Command)]
    If [$result ≠ "OK"]
        Set Field [MySQL Query::Result; $result]
        Show Custom Dialog ["Error: " & $result]
    Else
        Set Variable [$lines; Value:""]
        Set Variable [$fieldcount; Value:MBS("SQL.FieldCount"; $command)]
        Loop
            #call FetchNext to get the next record
            Set Variable [$result; Value:MBS("SQL.FetchNext"; $Command)]
            Exit Loop If [$result ≠ 1]
            Set Variable [$line; Value:""]
            Set Variable [$i; Value:1]
            Loop
                #We query field names and values to show them later
                Set Variable [$v; Value:MBS("SQL.GetFieldAsText"; $command; $i)]
                Set Variable [$n; Value:MBS("SQL.GetFieldName"; $command; $i)]
                Set Variable [$line; Value:$line & $n & ": " & $v & ¶]
                Set Variable [$i; Value:$i+1]
                Exit Loop If [$i > $fieldCount]
            End Loop
            Set Variable [$lines; Value:$lines & ($line & ¶)]
        End Loop
        Set Variable [$lines; Value:$lines & ¶]
        Show Custom Dialog ["Result from Query:"; $lines]
    End If
    #Cleanup
    Set Variable [$result2; Value:MBS("SQL.FreeCommand"; $Command)]
End If
Set Variable [$result2; Value:MBS("SQL.FreeConnection"; $Connection)]

See also


SQL.NewCommand   -   SQL.Open

Feedback: Report problem or ask question.




Links
MBS REAL studio Plugins - Pfarrgemeinde Ministranten Nickenich