我正在尝试将SQLite数据库与javascript建立连接。我究竟做错了什么? 我在运行以下代码时遇到问题。当我尝试在节点上运行.js时,效果很好。当我尝试通过浏览器在HTML文件上运行它时,它失败了。这是我得到的错误:
Uncaught ReferenceError: require is not defined
at changeRequest.js:1
Uncaught ReferenceError: Cannot access 'sqlite3' before initialization
at test (changeRequest.js:8)
at HTMLButtonElement.onclick (changeRequest - Kopie.html:15)
test @ changeRequest.js:8
onclick @ changeRequest - Kopie.html:15
我将不胜感激。
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<p>Hier werden die Change Requests sichtbar sein</p>
<script src="changeRequest.js"></script>
<script src="require.js"></script>
<button onclick="test()" >test</button>
</body>
</html>
const sqlite3 = require('sqlite3').verbose();
function test ()
{
// open the database
let db = new sqlite3.Database('./changeRequestDB.db', sqlite3.OPEN_READWRITE, (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the chinook database.');
});
db.serialize(() => {
db.each(`SELECT id as id,
name as name
FROM test`, (err, row) => {
if (err) {
console.error(err.message);
}
console.log(row.id + "\t" + row.name);
});
});
db.close((err) => {
if (err) {
console.error(err.message);
}
console.log('Close the database connection.');
});
}
test();