我刚学过nodeJs& sqlite3的。
我正在尝试用简单的DB构建简单的nodeJs服务器。 我正在将用户添加到数据库中(我没有收到任何错误),但是当我尝试从表中打印值时,我的结果是空的。
我无法理解我做错了什么?
// Import Required Module
var express = require('express')
var sqlite3 = require('sqlite3').verbose();
var promise = require('promise');
var app = express()
var bodyParser = require('body-parser')
// Create instances
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Get
app.get('/', function (req, res) {
res.sendFile('main.html', {root: __dirname })
})
// Init
app.get('/init', function (req, res) {
initDataBase(req, res)
})
// Post - Login
app.post("/login", function(req, res) {
var userName = req.body.userName;
var pass = req.body.pass;
console.log('userName: ' + userName);
console.log('pass: ' + pass);
if(req.body.userName && req.body.pass)
{
if ((userName == 'a') && (pass == 'b'))
{
console.log('YES')
res.send('YES')
}
}
else
{
console.log('NO')
res.send('NO')
}
});
/*
/addUserScreen
*/
app.get("/addUserScreen", function(req, res) {
console.log('Sending: addUserScreen.html')
res.sendFile('addUserScreen.html', {root: __dirname })
});
/*
View
*/
app.get("/view", function(req, res) {
var db = new sqlite3.Database('usersDataBase');
db.all("SELECT * FROM usersTable", function(err,rows){
if (err)
{
console.log('Error');
}
else
{
console.log('Ok');
rows.forEach(function (row) {
console.log('---');
console.log('User: ' + row.userName + ": " + row.password);
});
}
});
db.close();
// get to main screen
console.log('Sending main.html')
res.sendFile('main.html', {root: __dirname })
});
function onSelectFromUsersTableSuccess(row) {
console.log(row.userName + ": " + row.password);
}
function onSelectFromUsersTableError(err) {
console.log('error: ' + err)
}
// Post - Add User
app.post("/addUser", function(req, res) {
var userName = req.body.userName;
var passw = req.body.passw;
if(req.body.userName && req.body.passw)
{
console.log('Adding userName: ' + userName + ' password: ' + passw);
var db = new sqlite3.Database('usersDataBase');
var stmt = db.prepare("INSERT INTO usersTable VALUES (null, ?, ?)", userName, passw, onAddNewUsernameFinished);
stmt.finalize();
db.close();
}
console.log('Sending main.html')
res.sendFile('main.html', {root: __dirname })
});
function onAddNewUsernameFinished(err) {
if (null == err)
{
console.log('Finished adding new user name')
}
else
{
console.log('[onAddNewUsernameFinished] error: ' + err)
}
}
/*
*/
var initDataBase = function (req, res) {
console.log('Init DB')
// create DB
var db = new sqlite3.Database('usersDataBase');
// create table
db.run("CREATE TABLE IF NOT EXISTS usersTable (id INTEGER PRIMARY KEY AUTOINCREMENT , userName TEXT, password TEXT)", onCreateDatabaseError);
db.close();
console.log('DB Created Finished')
}
function onCreateDatabaseError(err) {
if (null != err)
{
console.log('[onCreateDatabaseError] error: ' + err)
}
}
// Listner
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
答案 0 :(得分:1)
您实际上还没有在数据库中添加用户。
为了执行准备好的查询语句,您需要使用.run
方法。
您的代码应如下所示:
var stmt = db.prepare("INSERT INTO usersTable VALUES (null, ?, ?)");
stmt.run( userName, passw, onAddNewUsernameFinished );
stmt.finalize();
中阅读更多内容