使用JavaScript连接数据库服务器的方法有多少?

时间:2012-04-30 10:36:16

标签: javascript jquery mysql oracle sqlite

我想知道有多少种方法可以使用JavaScript获取服务器数据库,例如使用mongo db,SQLite或任何其他方式连接服务器数据库

2 个答案:

答案 0 :(得分:3)

如果你在谈论客户端Javascript,那么 1 是没有办法的。

您必须向服务器上运行的脚本发送消息(可能使用AJAXWebSockets),然后服务器将连接到数据库。


1 实际上, WebDB用于访问HTML5规范中的本地SQLite引擎,但它已被弃用。

答案 1 :(得分:0)

感谢所有有价值的建议,我使用了基于浏览器支持的本地数据库,使用以下方式:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>SQL Storage</title>
<body>
    <br />
    <br />
    <div align="center">
        <input type="hidden" id="id" /> First name:<input type="text"
            id="firstName" /><br /> Last name:<input type="text" id="lastName" /><br />
        Phone: <input type="text" id="phone" /><br />
        <button onClick="resetForm()">Reset Form</button>
        <button onClick="updateRecord()">Update</button>
        <button onClick="insertRecord()">Insert</button>
        <button onClick="dropTable()">Drop Table</button>
        <div id="results"></div>
    </div>
</body>

<script>
    var results = document.getElementById('results');
    var id = document.getElementById('id');
    var firstName = document.getElementById('firstName');
    var lastName = document.getElementById('lastName');
    var phone = document.getElementById('phone');

    var createStatement = "CREATE TABLE IF NOT EXISTS Contacts (id INTEGER PRIMARY KEY AUTOINCREMENT, firstName TEXT, lastName TEXT, phone TEXT)";
    var selectAllStatement = "SELECT * FROM Contacts";
    var insertStatement = "INSERT INTO Contacts (firstName, lastName, phone) VALUES (?, ?, ?)";
    var updateStatement = "UPDATE Contacts SET firstName = ?, lastName = ?, phone = ? WHERE id = ?";
    var deleteStatement = "DELETE FROM Contacts WHERE id=?";
    var dropStatement = "DROP TABLE Contacts";
    try {
        var db = openDatabase("AddressBook", "1.0", "Address Book", 200000);
    } catch (e) {
        alert(e)
    }
    var dataset;
    createTable();

    function onError(tx, error) {
        alert(error.message);
    }

    function showRecords() {
        results.innerHTML = '';
        db.transaction(function(tx) {
            tx.executeSql(selectAllStatement, [], function(tx, result) {
                dataset = result.rows;
                for ( var i = 0, item = null; i < dataset.length; i++) {
                    item = dataset.item(i);
                    results.innerHTML += '<li>' + item['lastName'] + ' , '
                    item['firstName'] + ' <a href="#"     onclick="loadRecord('
                            + i + ')">edit</a>  '
                            + '<a href="#" onclick="deleteRecord(' + item['id']
                            + ')">delete</a></li>';
                }
            });
        });
    }

    function createTable() {
        db.transaction(function(tx) {
            tx.executeSql(createStatement, [], showRecords, onError);
        });
    }

    function insertRecord() {
        db.transaction(function(tx) {
            tx.executeSql(insertStatement, [ firstName.value, lastName.value,
                    phone.value ], loadAndReset, onError);
        });
    }

    function loadRecord(i) {
        var item = dataset.item(i);
        firstName.value = item['firstName'];
        lastName.value = item['lastName'];
        phone.value = item['phone'];
        id.value = item['id'];
    }

    function updateRecord() {
        db.transaction(function(tx) {
            tx.executeSql(updateStatement, [ firstName.value, lastName.value,
                    phone.value, id.value ], loadAndReset, onError);
        });
    }

    function deleteRecord(id) {
        db.transaction(function(tx) {
            tx.executeSql(deleteStatement, [ id ], showRecords, onError);
        });
        resetForm();
    }

    function dropTable() {
        db.transaction(function(tx) {
            tx.executeSql(dropStatement, [], showRecords, onError);
        });
        resetForm();
    }

    function loadAndReset() {
        resetForm();
        showRecords();
    }

    function resetForm() {
        firstName.value = '';
        lastName.value = '';
        phone.value = '';
        id.value = '';
    }
</script>
</html>