python - 使用sql或sqlite的Webiopi(Raspberry pi)

标签 python mysql sqlite raspberry-pi webiopi

使数据库与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


谢谢

最佳答案

对于数据库,可以使用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

关于python - 使用sql或sqlite的Webiopi(Raspberry pi),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28123371/

相关文章:

mysql - 根据mysql中另一个表中的新条目在一个表中添加新列

mysql - 使用 restful API 在 nodejs 上实现 breeze

android - 更新时清除数据

sql - 将数据导入sqlite

Objective-C sqlite 在 LIKE 子句中添加查询参数

python - 如何加入/合并数据集?

python - Python路径困惑:可以导入目录但不能导入文件吗?

mysql - 如何计算具有空白值或 0 值的列数

python - CSV 读取错误 : new-line character seen in unquoted field

python - 在文档字符串中返回__init__的类型