/* 注1:node为异步,不能按顺序创建表、插入数据,可能会提示表不存在。 注2: */ var sqlite3 = require('sqlite3').verbose();
var db; db = new sqlite3.Database("db.db", function(err) { if (err) throw err; });
console.log("db: ", db) db.run(`create table IF NOT EXISTS user (id INT,name VARCHAR,password VARCHAR)`, function( err ) { if (err) throw err; console.log("Create Table Success!"); });
// Run Insert Data db.run(`insert into user values (666,"admin","admin")`, function(err) { if (err) throw err; console.log("Insert Data Success!"); });
db.close(function(err) { if (err) throw err; });
注意,由于 nodejs 是异步的,所以可能会提示 user 表不存在,多次执行即可。本文仅演示,不做实践指导。
此处仅描述环境的搭建。需求:在 arm 平台实现 nodejs 容器,内含 koa、sqlite3。 在 x86 运行 arm 容器。
1 2 3 4 5
docker pull arm32v7/node:10-slim
docker run -itd --rm -v $PWD:/home/node -p 3000:3000 --name nodejsbuild arm32v7/node:10-slim sh docker exec -it nodejsbuild sh
安装:
1
npm install sqlite3
出错:
1 2 3 4 5 6 7
npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! sqlite3@4.2.0 install: `node-pre-gyp install --fallback-to-build` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the sqlite3@4.2.0 install script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
分析:
1 2 3 4 5 6 7 8 9 10
node-pre-gyp WARN Using needle for node-pre-gyp https download node-pre-gyp WARN Tried to download(403): https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v4.2.0/node-v64-linux-arm.tar.gz node-pre-gyp WARN Pre-built binaries not found for sqlite3@4.2.0 and node@10.20.1 (node-v64 ABI, glibc) (falling back to source compile with node-gyp) // !! arm 平台没有预编译,需要从源码安装 gyp ERR! find Python // !! 未安装python gyp ERR! find Python Python is not set from command line or npm configuration gyp ERR! find Python Python is not set from environment variable PYTHON
gyp ERR! build error gyp ERR! stack Error: not found: make // !! 未安装编译所需工具