Abfragen und Verbindungen mit JavaScript via C++

Einführung

Die Verwaltung von Datenbankverbindungen und die Ausführung von Abfragen sind für Anwendungen, die mit strukturierten Daten interagieren, insbesondere bei der Arbeit mit Excel-Dateien und Datenbanken, unerlässlich. Aspose.Cells for JavaScript via C++ bietet umfassende Funktionen, um diese Aufgaben effizient durchzuführen.

Mit einer Datenbank verbinden

Um eine Datenbank zu verbinden, können Sie die für Ihren Datenbanktyp relevante Verbindungszeichenfolge verwenden. Nachfolgend ein Beispiel, wie man mit JavaScript und C++-Integration eine Verbindung herstellt.

<!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>

Ausführung einer Abfrage

Sobald die Verbindung hergestellt ist, können Sie Abfragen gegen die Datenbank ausführen. Hier ist ein Beispiel, wie man eine einfache SQL-Abfrage zum Abrufen von Daten ausführt.