Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ESP8266(NodeMCU)无法创建sqlite3数据库(使用lua)_Sqlite_Lua_Esp8266_Nodemcu - Fatal编程技术网

ESP8266(NodeMCU)无法创建sqlite3数据库(使用lua)

ESP8266(NodeMCU)无法创建sqlite3数据库(使用lua),sqlite,lua,esp8266,nodemcu,Sqlite,Lua,Esp8266,Nodemcu,我想在我的ESP8266节点微处理器上使用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 bu

我想在我的ESP8266节点微处理器上使用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)
我使用的代码(主要部分)是: (完整的代码在本帖后面)

日志是:

> -- drop_sntptab()
> existenzsicherung_sntptab()
E:M 4272
CREATE TABLE IF NOT EXISTS 'abgleich' ('lsync' number,'ssync' number)
Status 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()
  -- Datenbankdatei allokieren
  db = sqlite3.open(database)
end


function close_db()
  status = db:close() 
  print("Status DB-schliessen: ", status)
end


function delete_sntp_database()
  -- Datenbankdatei wird geloescht
  file.remove(database)
end


function drop_sntptab()
  --- Tabelle loeschen
  sql = "DROP TABLE '"..tabelle.."';"
  status = db:exec(sql)
  print(sql)
  print("Status DROP "..tabelle..": ",status)
end


function existenzsicherung_sntptab()
  -- Wenn Tabelle nicht exisiert, ohne Daten anlegen
  sql = "CREATE TABLE IF NOT EXISTS '"..tabelle.."' ('"..lokal.."' number,'"..sntp.."' number)"
  status = db:exec(sql)
  print(sql)
  print("Status Anlage '"..tabelle.."': ",status)

  -- Anzahl der Datenaetze ermitteln
  sql = "SELECT count(*) FROM '"..tabelle.."'"
  print(sql)
  for tmp in db:urows(sql) 
    -- Anzahl Datensätze
  do 
    anzahl = tmp
  end
  print('Anzahl Datensaetze: ',anzahl)
  -- Wenn Anzahl der Datensätze 0 ist, wird eine Datenzeile eingefügt
  if anzahl == 0
  then
    sql = "INSERT INTO '"..tabelle.."' ('"..lokal.."', '"..sntp.."') values ("..l0..","..s0..")"
    status = db:exec(sql)
    print(sql)
    print("Status Eingabe Feldwerte: ",status)
  end
  -- Ausgabe des Tabelleninhalts
  -- urows ermöglicht die Zeilenweise Ausgabe des Select - Ergebnis
  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("Ergebniszeile(n): ",v1,v2) 
    lastlokal = v1
    lastsntp = v2
  end
end

-- delete_sntp_database()
open_db()
-- drop_sntptab()
existenzsicherung_sntptab()
-- ermittel_lastlokal()
close_db()
有人能看出我的错误吗

事先非常感谢

问候
KleInLat

如果不存在,则创建
恰好不是为该端口剥离的许多SQLite功能之一?->您好,谢谢您的评论。这似乎不是问题所在。我删除了数据库文件,并在没有IF EXISTS子句的情况下重新启动。它仍然不起作用。那么,我想你应该开始把你的代码精简到一个最小的、完整的、可验证的例子(尽可能短的代码来证明错误)。谢谢你,你是对的。因为似乎没有一个明确而简单的方法来发现错误,所以您指出了正确的方法。我会做第二天的。
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()
  -- Datenbankdatei allokieren
  db = sqlite3.open(database)
end


function close_db()
  status = db:close() 
  print("Status DB-schliessen: ", status)
end


function delete_sntp_database()
  -- Datenbankdatei wird geloescht
  file.remove(database)
end


function drop_sntptab()
  --- Tabelle loeschen
  sql = "DROP TABLE '"..tabelle.."';"
  status = db:exec(sql)
  print(sql)
  print("Status DROP "..tabelle..": ",status)
end


function existenzsicherung_sntptab()
  -- Wenn Tabelle nicht exisiert, ohne Daten anlegen
  sql = "CREATE TABLE IF NOT EXISTS '"..tabelle.."' ('"..lokal.."' number,'"..sntp.."' number)"
  status = db:exec(sql)
  print(sql)
  print("Status Anlage '"..tabelle.."': ",status)

  -- Anzahl der Datenaetze ermitteln
  sql = "SELECT count(*) FROM '"..tabelle.."'"
  print(sql)
  for tmp in db:urows(sql) 
    -- Anzahl Datensätze
  do 
    anzahl = tmp
  end
  print('Anzahl Datensaetze: ',anzahl)
  -- Wenn Anzahl der Datensätze 0 ist, wird eine Datenzeile eingefügt
  if anzahl == 0
  then
    sql = "INSERT INTO '"..tabelle.."' ('"..lokal.."', '"..sntp.."') values ("..l0..","..s0..")"
    status = db:exec(sql)
    print(sql)
    print("Status Eingabe Feldwerte: ",status)
  end
  -- Ausgabe des Tabelleninhalts
  -- urows ermöglicht die Zeilenweise Ausgabe des Select - Ergebnis
  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("Ergebniszeile(n): ",v1,v2) 
    lastlokal = v1
    lastsntp = v2
  end
end

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