JavaScript ve C++ ile Sorgular ve Bağlantılar

Giriş

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.

Veritabanına Bağlanmak

Veritabanına bağlanmak için, veritabanı türünüze uygun bağlantı dizesini kullanabilirsiniz. Aşağıda, C++ entegrasyonu ile JavaScript kullanarak bağlantı kurmanın bir örneği verilmiştir.

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

Sorgu Çalıştırmak

Bağlantı kurulduktan sonra, veritabanına karşı sorgular çalıştırabilirsiniz. İşte basit bir SQL sorgusu kullanarak veri almak için bir örnek.