sqlite HTML,CSS,JS和数据库-我如何连接所有4个?

uqcuzwp8  于 2022-12-29  发布在  SQLite
关注(0)|答案(2)|浏览(135)

我有麻烦连接任何数据库到我的HTML CSS JS项目。我读了这么多东西,我只是迷路了。什么是最简单的方法来连接任何数据库,将与HTML和JS都工作。
我试过SQLite,但它不起作用,我该怎么解决这个问题呢?

23c0lvtd

23c0lvtd1#

你可以试试这个
https://pouchdb.com/

var db = new PouchDB('dbname');

db.put({
  _id: 'dave@gmail.com',
  name: 'David',
  age: 69
});

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

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

或者,它也可以用于firebase和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);
1dkrff03

1dkrff032#

要从JavaScript连接到SQLite数据库,需要使用PHP或Node.js等服务器端语言作为中介。
可以使用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();
?>

要从Node.js连接到SQLite数据库,可使用sqlite3模块
x一个一个一个一个x一个一个二个x
用于向服务器发送AJAX请求并处理响应的JavaScript代码:

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();
}

相关问题