6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Raspberry Pi Zero WでNode.js+SQLite3を動かしてみた

Posted at

はじめに

Raspberry Pi Zero WでNode.jsが動くのは確認してたんですが、SQLite3って動くのかなということで、動作確認した手順のメモです。

ZeroWには執筆時点最新の、「RASPBIAN STRETCH LITE」をインストールしておきます。
Wifi(もしくはOTG)やSSHの有効化などは事前に行っておいてください。

nodebrewで最新のNode.jsをインストールする

LITE版にはNode.jsは入っていないので、nodebrewでLTSバージョンをインストールします。
まずはnodebrewから。

$ curl -L git.io/nodebrew | perl - setup

パスを通します。

$ echo "export PATH=\$HOME/.nodebrew/current/bin:\$PATH" >> ~/.bashrc

環境変数を適用します。

$ source ~/.bashrc

nodebrewが使えるようになっている事を確認します。

$ nodebrew -v
nodebrew 0.9.8

リモートのバージョンを確認します。
現時点でLTSバージョンはv8.9.4です。

$ nodebrew ls-remote
...
v8.9.1    v8.9.2    v8.9.3    v8.9.4    
...

バイナリモードでインストールします。

$ nodebrew install-binary v8.9.4
...
Installed successfully

カレントのバージョンをv8.9.4にセットします。

$ nodebrew use v8.9.4
use v8.9.4

nodeとnpmが使用できる事を確認します。

$ node -v
v8.9.4
$ npm -v
5.6.0

Node.jsのプロジェクトを作成する

適当にプロジェクトディレクトリを作成し、npm initでpackage.jsonを作っておきます。

$ mkdir Projects
$ cd Projects
$ mkdir sqlite-test
$ cd sqlite-test
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (sqlite-test) 
version: (1.0.0) 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /home/pi/Projects/sqlite-test/package.json:

{
  "name": "sqlite-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this ok? (yes) yes

node-sqlite3をインストールします。
package.jsonに追加するよう、--saveをつけておきます。

$ npm install sqlite3 --save

コンパイルが始まるようで、すごく時間がかかります・・・。

node-sqlite3公式のサンプルプログラムを書いてみます。

$ nano index.js
'use strict';

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database(':memory:');

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

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

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

db.close();

動作確認

動くか確認します。

$ node index.js
1: Ipsum 0
2: Ipsum 1
3: Ipsum 2
4: Ipsum 3
5: Ipsum 4
6: Ipsum 5
7: Ipsum 6
8: Ipsum 7
9: Ipsum 8
10: Ipsum 9

動きました!

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?