Запросы и соединения с помощью JavaScript через C++
Contents
[
Hide
]
Введение
Управление соединениями с базой данных и выполнение запросов является важным для приложений, которым требуется взаимодействовать с структурированными данными, особенно при работе с файлами Excel и базами данных. Aspose.Cells for JavaScript через C++ предоставляет комплексные возможности для эффективного выполнения этих задач.
Подключение к базе данных
Для подключения к базе данных вы можете использовать строку подключения, соответствующую вашему типу базы данных. Ниже приведён пример установления соединения с помощью JavaScript с интеграцией C++.
<!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>
Выполнение запроса
После подключения вы можете выполнять запросы к базе данных. Вот пример, как выполнить простой SQL-запрос для получения данных.