Queries and Connections with JavaScript via C++

Introduction

Managing database connections and executing queries is essential for applications that need to interact with structured data, particularly when working with Excel files and databases. Aspose.Cells for JavaScript via C++ provides comprehensive features to perform these tasks efficiently.

Connecting to a Database

To connect to a database, you can use the connection string relevant to your database type. Below is an example of how to establish a connection using JavaScript with C++ integration.

<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells Example - SQLite Connect Conversion</title>
    </head>
    <body>
        <h1>SQLite Connect Example (Browser Simulation)</h1>
        <input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
        <br/><br/>
        <input type="text" id="dbPath" placeholder="path_to_your_database.db" style="width:300px;" />
        <button id="runExample">Run Example</button>
        <a id="downloadLink" style="display: none;">Download Result</a>
        <div id="result"></div>
    </body>

    <script src="aspose.cells.js.min.js"></script>
    <script type="text/javascript">
        const { Workbook, SaveFormat, Worksheet, Cell } = AsposeCells;
        
        AsposeCells.onReady({
            license: "/lic/aspose.cells.enc",
            fontPath: "/fonts/",
            fontList: [
                "arial.ttf",
                "NotoSansSC-Regular.ttf"
            ]
        }).then(() => {
            console.log("Aspose.Cells initialized");
        });

        // Simulated createConnection for browser environment
        // (JavaScript `require('node-sqlite')` cannot be used in browser)
        function createConnection(databasePath) {
            return {
                databasePath,
                connect(callback) {
                    // Simulate async connection attempt
                    setTimeout(() => {
                        if (!databasePath || databasePath.toLowerCase().includes('error') || databasePath.toLowerCase().includes('invalid')) {
                            const err = new Error('Failed to connect to database: ' + databasePath);
                            callback(err);
                        } else {
                            callback(null);
                        }
                    }, 300);
                }
            };
        }

        document.getElementById('runExample').addEventListener('click', async () => {
            const dbPathInput = document.getElementById('dbPath').value || 'path_to_your_database.db';

            // Convert JavaScript pattern:
            // const { createConnection } = require('node-sqlite');
            // const db = createConnection('path_to_your_database.db');
            //
            // db.connect(err => { ... });
            //
            // to browser-compatible simulation:

            const db = createConnection(dbPathInput);

            db.connect(err => {
                if (err) {
                    console.error('Error connecting to the database:', err);
                    document.getElementById('result').innerHTML = `<p style="color: red;">Error connecting to the database: ${err.message}</p>`;
                    return;
                }
                console.log('Connected to the database successfully!');
                document.getElementById('result').innerHTML = '<p style="color: green;">Connected to the database successfully!</p>';
            });
        });
    </script>
</html>

Executing a Query

Once connected, you can execute queries against the database. Here is an example of how to execute a simple SQL query to retrieve data.