Home > Net >  HTML, CSS, JS & DataBase - how can i connect all 4?
HTML, CSS, JS & DataBase - how can i connect all 4?

Time:12-29

I'm having troubles with connecting any database into my HTML CSS JS project. I read and tired so many things, I'm just lost. What is the easiest way to connect any database that would work with HTML and JS both.

I tried SQLite, and it won't work, how can I solve this? Thanks in advance, Omer.

CodePudding user response:

You can try this one

https://pouchdb.com/

var db = new PouchDB('dbname');

db.put({
  _id: '[email protected]',
  name: 'David',
  age: 69
});

db.changes().on('change', function() {
  console.log('Ch-Ch-Changes');
});

db.replicate.to('http://example.com/mydb');

Or it can be useful for firebase and firebase SDK

https://firebase.google.com/docs/storage/web/start#web-version-9

import { initializeApp } from "firebase/app";
import { getStorage } from "firebase/storage";

// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
  storageBucket: ''
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);


// Initialize Cloud Storage and get a reference to the service
const storage = getStorage(app);

CodePudding user response:

To connect to a SQLite database from JavaScript, you will need to use a server-side language like PHP or Node.js as an intermediary.

You can connect databases using php sqlite

<?php
// Connect to the database
$db = new SQLite3("mydatabase.db");

// Perform a SELECT query
$result = $db->query("SELECT * FROM users");

// Print the results as a JSON array
$rows = array();
while ($row = $result->fetchArray()) {
    $rows[] = $row;
}
echo json_encode($rows);

// Close the connection
$db->close();
?>

JavaScript code to send an AJAX request to the server and handle the response:

function fetchUsers() {
    // Send an AJAX request to the server
    const xhr = new XMLHttpRequest();
    xhr.open("GET", "get_users.php");
    xhr.onload = function() {
        if (xhr.status === 200) {
            // Update the page with the results
            let users = JSON.parse(xhr.responseText);
            for (let i = 0; i < users.length; i  ) {
                let user = users[i];
                let name = user["name"];
                let email = user["email"];
                // ...
            }
        } else {
            console.error(xhr.statusText);
        }
    };
    xhr.onerror = function(error) {
        console.error(error);
    };
    xhr.send();
}
```
  • Related