Javascript 想要一个关于 Node.js 的 SQLite3 教程和一个代码示例解释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11040897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:11:01  来源:igfitidea点击:

A tutorial on SQLite3 for Node.js and a code example explanation wanted

javascriptdatabasenode.jssqlite

提问by corazza

I am a bit confused with SQLite at the moment, as this is the first time I'm ever using a database. I got sqlite3 from here: https://github.com/developmentseed/node-sqlite3.

我现在对 SQLite 有点困惑,因为这是我第一次使用数据库。我从这里得到了 sqlite3:https: //github.com/developmentseed/node-sqlite3

I'm looking at that example there, some things I do understand, while others I do not. Most of those database commands that are wrapped in .run(), .prepare()and the like give me a hard time.

我正在看那个例子,有些事情我确实理解,而另一些则我不理解。大多数包含在.run(),.prepare()等中的数据库命令都让我感到困难。

This is the example:

这是示例:

var usersDB = new sqlite3.Database("databases/users.db");

  usersDB.serialize(function() {
  usersDB.run("CREATE TABLE lorem (info TEXT)");

  var stmt = usersDB.prepare("INSERT INTO lorem VALUES (?)");
  for (var i = 0; i < 10; i++) {
      stmt.run("Ipsum " + i);
  }
  stmt.finalize();

  usersDB.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
      console.log(row.id + ": " + row.info);
  });
});

usersDB.close();

Also, how do I store simple things such as usernames, passwords (do I have to hash them myself?) and emails in the SQLite database on Node.js?

另外,我如何在 Node.js 上的 SQLite 数据库中存储简单的东西,例如用户名、密码(我必须自己对它们进行哈希处理吗?)和电子邮件?

采纳答案by optikfluffel

回答by corazza

There are two distinct things to learn: sqlite the database program, and node-sqlite3 the nodejs module that provides access to the sqlite db services. Your database questions will be best answered by learning about sqlite, the database program first. I would recommend getting and installing sqlite from: http://www.sqlite.org/. The site has good documentation that will help you learn to store user names and passwords. You can create tables from the command line, add data and get as sense of what is going on. After that if you understand the concepts of node.js then node-sqlite3 will make much more sense to you. Otherwise, spend some time with the node.js site.

有两个不同的东西需要学习:sqlite 数据库程序和 node-sqlite3 nodejs 模块,它提供对 sqlite db 服务的访问。你的数据库问题最好先学习sqlite,数据库程序。我建议从http://www.sqlite.org/获取和安装 sqlite 。该站点有很好的文档,可以帮助您学习存储用户名和密码。您可以从命令行创建表、添加数据并了解正在发生的事情。之后,如果您了解 node.js 的概念,那么 node-sqlite3 对您来说会更有意义。否则,花一些时间在 node.js 站点上。