'Connect to SQL Server in Robot framework | Maven Dependency

I'm trying to connect to SQL server in robot framework, so I've added the following code in pom.xml file:

    <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>8.4.1.jre14</version>
</dependency>

and I have the following robot file:

    *** Settings ***
    Documentation  Testing connection to the database!
    Library     SeleniumLibrary
    Library     DatabaseLibrary
    Library     OperatingSystem  
    #I'm not sure if we need OperatingSystem Library!!
    
    
    Suite Setup        Connect To Database      pymssql      ${DBName}    ${DBUser}     ${DBPass}    ${DBHost} 
    
   # Suite Setup        Connect To Database Using Custom Params   pymssql            ${DBName}    ${DBUser}     ${DBPass}    ${DBHost} 
    
    
    
    *** Variables ***
    ${DBName}         DatabaseName
    ${DBUser}         Username
    ${DBPass}         Password
    ${DBHost}         xx.xx.xx.xx
    
    
    
    *** Test Cases ***
    Testing Connect to SQL Server
        ${queryTest}=    Execute SQL String     select * from Users where Id=1
        log to console   ${queryTest} 

I'm getting the following error:

NoSectionError: No section: 'default'

I'm spending days trying to connect to the database and no luck!



Solution 1:[1]

You are likely missing ./resources/db.cfg file as explained in the official documentation for Connect To Database keyword.

Optionally, you can specify a dbConfigFile wherein it will load the default property values for dbapiModuleName, dbName dbUsername and dbPassword (note: specifying dbapiModuleName, dbName dbUsername or dbPassword directly will override the properties of the same key in dbConfigFile). If no dbConfigFile is specified, it defaults to ./resources/db.cfg.

You didn't specify any other, so it expect the default one in the location I mentioned.

A similar problem has been answered here on SO: Error: No section: 'default' in Robot Framework using DatabaseLibrary


EDIT:

Because it still doesn't work (see comments), I add this part.

Connect To Database looks like this in the code: https://github.com/franz-see/Robotframework-Database-Library/blob/master/src/DatabaseLibrary/connection_manager.py#L37, the beginning of the function looks like this (comment left out):

def connect_to_database(self, dbapiModuleName=None, dbName=None, dbUsername=None, dbPassword=None, dbHost=None, dbPort=None, dbCharset=None, dbConfigFile="./resources/db.cfg"):       
        config = ConfigParser.ConfigParser()
        config.read([dbConfigFile])

        dbapiModuleName = dbapiModuleName or config.get('default', 'dbapiModuleName')
        dbName = dbName or config.get('default', 'dbName')
        dbUsername = dbUsername or config.get('default', 'dbUsername')
        dbPassword = dbPassword if dbPassword is not None else config.get('default', 'dbPassword')
        dbHost = dbHost or config.get('default', 'dbHost') or 'localhost'
        dbPort = int(dbPort or config.get('default', 'dbPort'))

You get this error:

NoSectionError: No section: 'default'

which happens on line 81: https://github.com/franz-see/Robotframework-Database-Library/blob/master/src/DatabaseLibrary/connection_manager.py#L81

and that's because dbapiModuleName is None (by default and you don't assign any other value to it), so the code goes to or config.get('default', 'dbapiModuleName') on the same line. And that's where you're missing the ./resources/db.cfg file from which it's trying to get a value for this variable.

So you either have to provide all values, or have the file there.

It really is all mentioned in the doc for the keyword: https://franz-see.github.io/Robotframework-Database-Library/api/1.2.2/DatabaseLibrary.html#Connect%20To%20Database The first sentence reads:

Loads the DB API 2.0 module given dbapiModuleName then uses it to connect to the database using dbName, dbUsername, and dbPassword.

Additional information on what value to provide for dbapiModuleName could be found here: How to use database library with Robot Framework on temporary sql server But it can't be None as it it now with your config.

Solution 2:[2]

I made a simple changes on my code, and it's worked!!

Suite Setup    Connect To Database Using Custom Params    pymssql    '${DBHost}','${DBUser}','${DBPass}','${DBName}'

Please notice it should be: "Host" > "Username" > "Password" > "Database Name"

the parameters arrangement is matter!

Note: I didn't need to use the config file!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 A.Han