用sqlite3连接电子

时间:2016-06-12 19:51:55

标签: node.js electron

我想用sqlite3连接电子。我的package.json文件列在下面

{
  "name": "electrontest2",
  "version": "1.0.0",
  "description": "",
  "main": "db.js",
  "scripts": {
    "start": "electron ."
  },

"scripts": {
"start": "electron ."
},
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "electron-prebuilt": "^1.2.2"
  },
  "dependencies": {
    "jquery": "^2.1.4",
    "sqlite3": "^3.1.4"
  }
}

但是在进行npm启动时会抛出此错误

App threw an error during load
Error: Cannot find module '/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/binding/electron-v1.2-linux-x64/node_sqlite3.node'
    at Module._resolveFilename (module.js:438:15)
    at Function.Module._resolveFilename (/var/www/tools/node/project/electrontest2/node_modules/electron-prebuilt/dist/resources/electron.asar/common/reset-search-paths.js:47:12)
    at Function.Module._load (module.js:386:25)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/sqlite3.js:4:15)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
A JavaScript error occurred in the main process
Uncaught Exception:
Error: Cannot find module '/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/binding/electron-v1.2-linux-x64/node_sqlite3.node'
    at Module._resolveFilename (module.js:438:15)
    at Function.Module._resolveFilename (/var/www/tools/node/project/electrontest2/node_modules/electron-prebuilt/dist/resources/electron.asar/common/reset-search-paths.js:47:12)
    at Function.Module._load (module.js:386:25)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/sqlite3.js:4:15)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)

我想我收到了这个错误:

&#39; /var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/binding/node-v46-linux-x64/node_sqlite3.node'

而不是

&#39; /var/www/tools/node/project/electrontest2/node_modules/sqlite3/lib/binding/electron-v1.2-linux-x64/node_sqlite3.node'

请告诉我哪里弄错了。目前,在运行应用程序时,它会抛出上面列出的错误。

3 个答案:

答案 0 :(得分:2)

您确认错误是因为编译的SQLite模块默认为Node命名;因此电子无法找到它。使用node-gyp重建正确的名称,就像在answer above中一样;但是使用npm包electron-builder会更容易,这将为您提供更好的工作流程,并且可以使用多个平台和版本,而无需为node-gyp添加脚本。

请参阅this answer一个类似的问题,详细说明如何使其正常运行。

答案 1 :(得分:1)

尝试重建sqlite3包,如下所示:

Private m_cn As New SqlConnection
Private m_DA As SqlDataAdapter
Private m_CB As SqlCommandBuilder
Private m_DataTable As New DataTable
Private m_intRowPosition As Integer = 0

Private Sub InsertDatabaseItem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    m_cn.ConnectionString = "Data Source=My-PC\SQLSERVEREXPRESS;Initial Catalog=ConvienienceProducts;Integrated Security=True"

    m_cn.Open()
    m_DA = New SqlDataAdapter("Select * From ProductIndex", m_cn)
    m_CB = New SqlCommandBuilder(m_DA)
End Sub

Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
    Dim cmd As New SqlCommand(("INSERT INTO ProductIndex VALUES(" &
                              txtID.Text & "," &
                              txtName.Text & "," &
                              txtPrice.Text & "," &
                              txtDesc.Text & ")"), m_cn)

    cmd.ExecuteNonQuery()

    MsgBox("Success....", MsgBoxStyle.Information, "SUCCESS")

    Me.Hide()

    txtID.Clear()
    txtName.Clear()
    txtPrice.Clear()
    txtDesc.Clear()

    m_cn.Close()
    m_cn.Dispose()
End Sub

Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
    Me.Hide()
End Sub

检查你的电子版是什么

cd node_modules/sqlite3
npm run prepublish
node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.2-linux-x64
node-gyp rebuild --target=0.37.2 --arch=x64 --target_platform=linux --dist-url=https://atom.io/download/atom-shell --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.2-linux-x64

将--target值替换为电子包的vesion。

答案 2 :(得分:1)

我尝试使用最新的Node版本(6.2.2)运行它,如果您使用的是旧版本,例如。 4.2然后你将获得一个旧的捆绑而不是你的电子需要的v48:

npm install sqlite3 --build-from-source

只需将node_modules / sqlite3 / lib / binding中的文件夹从node-v48-linux-x64重命名为electron-v1.2-linux-x64。

它开始抱怨要求回调它们应该是可选的,但我只是像这样添加它们并且它起作用了:

var stmt = db.prepare(
  "INSERT INTO discount VALUES (?, ?, ?)",
  [1,2,3],
  () => true //Expected callback that is supposed to be optional
);

我只需要在我的电子应用程序中使用Sqlite用于简单目的,并且在我的实现中没有任何其他问题。