【问题标题】:Webiopi (Raspberry pi) with sql or sqlite带有 sql 或 sqlite 的 Webiopi (Raspberry pi)
【发布时间】:2015-01-24 07:36:48
【问题描述】:

我在使用 webiopi 使数据库工作时遇到了一些问题。我已经导入 sqlite3 并更改文件夹权限,但是当我运行 webiopi 时,什么都没有创建。但是,f.write('This is a test\n') 之后的其他功能每个进程都正常工作并重复循环。希望你能帮助我吗?

def loop():
    db = sqlite3.connect('schedule.db')
    db.execute('DROP TABLE IF EXISTS schedule')
    db.execute('CREATE TABLE schedule (hour int, minute int, second int, status text)')
    db.execute('INSERT INTO schedule (hour,minute,second,status) VALUES (?,?,?,?)',(sche.get('hour'),sche.get('minute'),sche.get('second'),"yes"))
    db.commit()
    f = open('workfile', 'w')
    f.write('This is a test\n')
    loop1=1
    while ((now.minute >= minute_ON) and (now.minute < minute_OFF) and (loop1<stepping)):
        step()
        loop1=loop1+1

谢谢

【问题讨论】:

  • 什么意思没有被创造?文件 workfile 还是 schedule.db 文件?
  • 他们两个。我用来控制步进电机的 step() 工作正常,所以我真的很困惑为什么没有创建数据库甚至文本文件

标签: python mysql sqlite raspberry-pi webiopi


【解决方案1】:

对于数据库,你可以使用 cursor 和 conn 更好。请参阅doc 了解更多信息。

对于文件,你可以close()它,当你不使用它来写数据时。 下面的代码可能会有所帮助:

def loop():
    db = sqlite3.connect('schedule.db')
    cur = db.cursor() # create a cursor
    cur.execute('DROP TABLE IF EXISTS schedule')
    cur.execute('CREATE TABLE schedule (hour int, minute int, second int, status text)')
    cur.execute('INSERT INTO schedule (hour,minute,second,status) VALUES (?,?,?,?)',(sche.get('hour'),sche.get('minute'),sche.get('second'),"yes"))
    db.commit()

    cur.close() # close cursor
    db.close() # close connection to sqlite3

    f = open('workfile', 'w')
    f.write('This is a test\n')
    f.close() # close file to write data

    loop1=1
    while ((now.minute >= minute_ON) and (now.minute < minute_OFF) and (loop1<stepping)):
        step()
        loop1=loop1+1

【讨论】:

  • 谢谢! close() 数据库解决了我的整个问题。
猜你喜欢
  • 1970-01-01
  • 2016-11-22
  • 2018-08-20
  • 2021-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多