ESP8266 (NodeMCU) 创建sqlite3数据库(使用Lua)不可行

我想在我的ESP8266 NodeMCU微处理器上使用sqlite3数据库。 固件是:

NodeMCU custom build by frightanic.com
branch: master
commit: 67027c0d05f7e8d1b97104e05a3715f6ebc8d07f
SSL: false
modules: bit,bme280,cron,dht,file,gpio,i2c,net,node,rfswitch,rtctime,sntp,sqlite3,tmr,uart,wifi
build created on 2018-04-16 16:06
powered by Lua 5.1.4 on SDK 2.2.1(cfd48f3)

我使用的(主要部分)代码是: (完整代码稍后在此帖子中)

-- 如果表不存在,则创建没有数据
sql = "CREATE TABLE IF NOT EXISTS '"..tabelle.."' ('"..lokal.."' number,'"..sntp.."' number)"
status = db:exec(sql)
print(sql)
print("Anlage '"..tabelle.."' 状态: ",status)

日志是:

> -- drop_sntptab()
> existenzsicherung_sntptab()
E:M 4272
CREATE TABLE IF NOT EXISTS 'abgleich' ('lsync' number,'ssync' number)
Anlage 'abgleich' 状态:   7
SELECT count(*) FROM 'abgleich'
no such table: abgleich

您可以看到,尽管SQL语句似乎是正确的,但表没有被创建。 打开命令是:

db = sqlite3.open(database)

以下是完整代码:

database = 'sntp.db'
tabelle = 'abgleich'
lokal = 'lsync'
sntp= 'ssync'
l0 = 1514764800  -- Epoche-Time 01.01.2018 00:00:00
s0 = 1514764800  -- Epoche-Time 01.01.2018 00:00:00

function open_db()
  -- 分配数据库文件
  db = sqlite3.open(database)
end

function close_db()
  status = db:close()
  print("关闭DB状态:", status)
end

function delete_sntp_database()
  -- 删除数据库文件
  file.remove(database)
end

function drop_sntptab()
  --- 删除表
  sql = "DROP TABLE '"..tabelle.."';"
  status = db:exec(sql)
  print(sql)
  print("DROP ",tabelle," 状态: ",status)
end

function existenzsicherung_sntptab()
  -- 如果表不存在,则创建没有数据
  sql = "CREATE TABLE IF NOT EXISTS '"..tabelle.."' ('"..lokal.."' number,'"..sntp.."' number)"
  status = db:exec(sql)
  print(sql)
  print("Anlage '"..tabelle.."' 状态: ",status)

  -- 获取数据集数量
  sql = "SELECT count(*) FROM '"..tabelle.."'"
  print(sql)
  for tmp in db:urows(sql)
    -- 数据集数量
  do
    anzahl = tmp
  end
  print('数据集数量:',anzahl)
  -- 如果数据集数量为0,则插入数据行
  if anzahl == 0
  then
    sql = "INSERT INTO '"..tabelle.."' ('"..lokal.."', '"..sntp.."') values ("..l0..","..s0..")"
    status = db:exec(sql)
    print(sql)
    print("输入字段值的状态:",status)
  end
  -- 输出表内容
  -- urows允许逐行输出选择结果
  sql = "SELECT * FROM '"..tabelle.."'"
  for lokal,sntp in db:urows(sql)
  do
    print(lokal,sntp)
  end
end

function ermittel_lastlokal()
  sql = "SELECT * FROM '"..tabelle.."' WHERE _ROWID_ = 1;"
  for v1,v2 in db:urows(sql)
  do
    print("结果行数:",v1,v2)
    lastlokal = v1
    lastsntp = v2
  end
end

-- delete_sntp_database()
open_db()
-- drop_sntptab()
existenzsicherung_sntptab()
-- ermittel_lastlokal()
close_db()

有人看到我的错误吗?

非常感谢

敬礼 Kleinlaut

点赞