NAOでデータを保存したいなーと思っていたら、意外とあっさり出来ました。
環境
OS X El Capitan 10.11.3
Choregraphe 2.1.3.3
SQLite version 3.8.10.2
Python Script
ボックスはデフォルトのままでOKです。
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
def onLoad(self):
#put initialization code here
pass
def onUnload(self):
#put clean-up code here
pass
def onInput_onStart(self):
import sqlite3
appId = self.packageUid()
qi.path.setWritablePath("/Users/{ユーザ名}/Desktop/dev/nao/data_files") #任意の場所を指定
dbpath = qi.path.userWritableDataPath(appId, "sample.db")
self.logger.info(dbpath)
conn = sqlite3.connect(dbpath)
c = conn.cursor()
c.execute('''CREATE TABLE shop (id INTEGER PRIMARY KEY, name TEXT , price INTEGER)''')
c.execute("INSERT INTO shop (name, price) VALUES ('hogehoge',100)")
conn.commit()
conn.close()
self.onStopped()
def onInput_onStop(self):
self.onUnload() #it is recommended to reuse the clean-up as the box is stopped
確認
$ pwd
/Users/{ユーザ名}/Desktop/dev/nao/data_files/data/.lastUploadedChoregrapheBehavior
$ sqlite3 sample.db
SQLite version 3.8.10.2 2015-05-20 18:17:19
Enter ".help" for usage hints.
sqlite> .table
shop
sqlite> select * from shop;
1|hogehoge|100
登録できました。
※sqliteでエラーになるときは、.lastUploadedChoregrapheBehavior
をchmod 777
にするといけました。
その他
CSVパッケージ、JSONパッケージ、ElementTreeパッケージも使えるみたいです。