python - Webiopi (Raspberry pi) 与 sql 或 sqlite

标签 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 - Webiopi (Raspberry pi) 与 sql 或 sqlite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28123371/

相关文章:

php - 如何在 mysql 中存储 html_entites 作为它们的表示而不是它们的值

java - 如何使用java检查sqlite中是否存在与数据库的连接?

mysql - 如何根据另一个表中的列和原始表中的列对表中的值进行排序?

java - Google Foobar power_hungry

python 数组在列表中索引列表

python - 将 gtk.DrawingArea 保存到文件

python - 如何识别字符串中的非字母

php - 适用于大型和可扩展应用程序的数据库表结构

php - 从数据库中填充缺失的日期和空数据并在一个数组中获得结果-Laravel

android - 无法使用 Sqlite AssetHelper 库打开预安装的数据库