2

几天来,我试图将我的电子应用程序与 sqlite 集成。我已经使用以下脚本构建了 sqlite:

"rebuild": "electron-rebuild -f -w sqlite3"

我能够连接到数据库,创建 db 文件,但来自 javascript,如下所示:

 var sqlite3 = require('sqlite3').verbose();
            var db = new sqlite3.Database('./database2.sqllite3');

            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();

但由于我使用的是组件方法,我想创建单独的打字稿服务以提供所有数据库功能并使其可以从 TS 组件中访问。所以我只是试图将此代码移动到 TS 但它不起作用。网络上的所有示例都是关于在 js 文件或组件中使用此行,但我认为在旧版本的 Angular 中,因为我收到以下错误:

ERROR in src/app/database.service.ts(19,18): error TS2304: Cannot find name 'require'.

我能够从 ts 脚本调用专用的 js 文件,但效果相同。看起来很轻,使用 require 为时已晚?有没有人设法使它工作?

这是我的配置

"dependencies": {
    "@angular/animations": "^5.0.1",
    "@angular/common": "^5.0.0",
    "@angular/compiler": "^5.0.0",
    "@angular/core": "^5.0.0",
    "@angular/forms": "^5.0.0",
    "@angular/http": "^5.0.0",
    "@angular/platform-browser": "^5.0.0",
    "@angular/platform-browser-dynamic": "^5.0.0",
    "@angular/router": "^5.0.0",
    "@ngui/datetime-picker": "^0.16.2",
    "@types/lodash": "^4.14.85",
    "angular-svg-round-progressbar": "^1.2.0",
    "bootstrap": "^3.3.7",
    "commonjs": "latest",
    "core-js": "^2.4.1",
    "electron": "^1.8.1",
    "rxjs": "^5.5.2",
    "sqlite3": "^3.1.13",
    "xlsx": "^0.11.8",
    "zone.js": "^0.8.14"
  },
  "devDependencies": {
    "@angular/cli": "1.5.0",
    "@angular/compiler-cli": "^5.0.0",
    "@angular/language-service": "^5.0.0",
    "@types/jasmine": "~2.6.3",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~8.0.50",
    "codelyzer": "~4.0.1",
    "electron-rebuild": "^1.6.0",
    "jasmine-core": "^2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.2.0",
    "ts-node": "~3.3.0",
    "tslint": "~5.8.0",
    "typescript": "~2.6.1"
  }

问候简

4

0 回答 0