RESTでMySQLを操作する

ESP32でMySQLを操作する


esp-idfにはこ ちらにhttpのサンプルコードが付属しています。
このサンプルを元に、MySQLをRESTで操作するソースをこちらで公開していま す。
menuconfigで以下の変数を指定します。


WEBサーバーのIPアドレスとポート番号は環境に合わせて変更してください。




## List ##
テーブル内の全件を表示します。
I (35386) JSON: path=
I (35386) JSON: url=http://192.168.10.43:8080/api.php/records/posts
I (36666) JSON: HTTP GET Status = 200, content_length = 134
I (37666) JSON: buffer=
{"records":[{"id":1,"user_id":1,"category_id":1,"content":"blog started"},{"id":2,"user_id":1,"category_id":2,"content":"It works!"}]}
I (37666) JSON: Deserialize.....
I (37666) JSON: [records]
I (37676) JSON: [id]
I (37676) JSON: int=1 double=1.000000
I (37676) JSON: [user_id]
I (37686) JSON: int=1 double=1.000000
I (37686) JSON: [category_id]
I (37686) JSON: int=1 double=1.000000
I (37696) JSON: [content]
I (37696) JSON: blog started
I (37706) JSON: [id]
I (37706) JSON: int=2 double=2.000000
I (37706) JSON: [user_id]
I (37716) JSON: int=1 double=1.000000
I (37716) JSON: [category_id]
I (37716) JSON: int=2 double=2.000000
I (37726) JSON: [content]

## Read ##
テーブル内の特定の1件を表示します。
I (70386) JSON: path=1
I (70386) JSON: url=http://192.168.10.43:8080/api.php/records/posts/1
I (70416) JSON: HTTP GET Status = 200, content_length = 61
I (71416) JSON: buffer=
{"id":1,"user_id":1,"category_id":1,"content":"blog started"}
I (71416) JSON: Deserialize.....
I (71416) JSON: [id]
I (71416) JSON: int=1 double=1.000000
I (71416) JSON: [user_id]
I (71426) JSON: int=1 double=1.000000
I (71426) JSON: [category_id]
I (71436) JSON: int=1 double=1.000000
I (71436) JSON: [content]
I (71436) JSON: blog started

## Create ##
データを追加します。
I (95486) JSON: url=http://192.168.10.43:8080/api.php/records/posts
I (95656) JSON: HTTP GET Status = 200, content_length = 1
I (96656) JSON: buffer=
3
I (96656) JSON: new_id=3

## Update ##
データを更新します。
I (115586) JSON: path=3
I (115586) JSON: url=http://192.168.10.43:8080/api.php/records/posts/3
I (115616) JSON: HTTP GET Status = 200, content_length = 60
I (116616) JSON: buffer=
{"id":3,"user_id":1,"category_id":3,"content":"Hello World"}
I (116616) JSON: Deserialize.....
I (116616) JSON: [id]
I (116616) JSON: int=3 double=3.000000
I (116616) JSON: [user_id]
I (116626) JSON: int=1 double=1.000000
I (116626) JSON: [category_id]
I (116636) JSON: int=3 double=3.000000
I (116636) JSON: [content]
I (116636) JSON: Hello World
I (116646) JSON: path=3
I (116656) JSON: url=http://192.168.10.43:8080/api.php/records/posts/3
I (116806) JSON: HTTP GET Status = 200, content_length = 1
I (117806) JSON: buffer=
1
I (117806) JSON: path=3
I (117806) JSON: url=http://192.168.10.43:8080/api.php/records/posts/3
I (117836) JSON: HTTP GET Status = 200, content_length = 60
I (118836) JSON: buffer=
{"id":3,"user_id":1,"category_id":3,"content":"Hello Japan"}
I (118836) JSON: Deserialize.....
I (118836) JSON: [id]
I (118836) JSON: int=3 double=3.000000
I (118836) JSON: [user_id]
I (118846) JSON: int=1 double=1.000000
I (118846) JSON: [category_id]
I (118856) JSON: int=3 double=3.000000
I (118856) JSON: [content]
I (118856) JSON: Hello Japan

## Delete ##
データを削除します。
I (149386) JSON: path=3
I (149386) JSON: url=http://192.168.10.43:8080/api.php/records/posts/3
I (149436) JSON: HTTP GET Status = 200, content_length = 60
I (150436) JSON: buffer=
{"id":3,"user_id":1,"category_id":3,"content":"Hello Japan"}
I (150436) JSON: Deserialize.....
I (150436) JSON: [id]
I (150436) JSON: int=3 double=3.000000
I (150436) JSON: [user_id]
I (150446) JSON: int=1 double=1.000000
I (150446) JSON: [category_id]
I (150456) JSON: int=3 double=3.000000
I (150456) JSON: [content]
I (150456) JSON: Hello Japan
I (150466) JSON: path=3
I (150476) JSON: url=http://192.168.10.43:8080/api.php/records/posts/3
I (150646) JSON: HTTP GET Status = 200, content_length = 1
I (151646) JSON: buffer=
1
I (151646) JSON: path=3
I (151646) JSON: url=http://192.168.10.43:8080/api.php/records/posts/3
I (151686) JSON: HTTP GET Status = 404, content_length = 46
I (152686) JSON: buffer=
{"code":1003,"message":"Record '3' not found"}
I (152686) JSON: Deserialize.....
I (152686) JSON: [code]
I (152686) JSON: int=1003 double=1003.000000
I (152686) JSON: [message]
I (152696) JSON: Record '3' not found

続く...