Help us understand the problem. What is going on with this article?

NAO(Pepper)でsqlite3を使う

More than 3 years have passed since last update.

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でエラーになるときは、.lastUploadedChoregrapheBehaviorchmod 777にするといけました。

その他

CSVパッケージ、JSONパッケージ、ElementTreeパッケージも使えるみたいです。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away