Loading data from a database in user code

To create a database worksheet from user code, here's what you need to do:

  • Define an ODBC connection string (if you don't know the syntax of an ODBC connection string for your database, ConnectionStrings is a great reference). This is a simple example for SQL Server:
connectionString = ("DRIVER={SQLServer};SERVER=myserver;" +
                    "DATABASE=mydb;UID=testuser;PWD=testpassword;")
  • Call workbook.AddWorksheetFromDB, which takes the name for the worksheet, the connection string, the query to produce the data, and whether the first row of the resulting worksheet should be a header row populated from the query's field names:
workbook.AddWorksheetFromDB("Transactions", connectionString,
                                "select * from transactions", True)

Comments