0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

プロジェクト型演習 #1 (raspberrypi DB操作)

Last updated at Posted at 2021-06-01

準備

phpインストール

pi@raspberry:~ $ sudo apt install php php php-cli php-curl
pi@raspberry:~ $ sudo a2enmod userdir
pi@raspberry:~ $ sudo systemctl restart apache2

phpファイル作成

pi@raspberry:~ $ cd /var/www/html
pi@raspberry:/var/www/html $ touch index.php
pi@raspberry:/var/www/html $ nano index.php
index.php
<?php
  phpinfo();
?>

phpの確認

pi@raspberry:/var/www/html $ cd ~

MariaDBインストール

pi@raspberry:~ $ sudo apt-get install mariadb-server-10.0

DBへログインしユーザの作成

pi@raspberry:~ $ sudo mysql -u root
Enter password: raspberry
MariaDB[(none)]> CREATE USER 'pi'@'localhost' IDENTIFIED BY 'raspberry';
MariaDB[(none)]> grant all privileges on sample_db.* to 'pi'@'localhost'
    -> with grant option;
MariaDB[(none)]> exit
Bye

ユーザでログイン

pi@raspberry:~ $ sudo mysql -upi -praspberry

DBを作成

MariaDB[(none)]> create database sample_db;

作成したDBへ入る

MariaDB[(none)]> use sample_db
Database changed

テーブルの作成

MariaDB [sample_db]> create table sample_tb(num int,name varchar(50));
Query OK, 0 rows affected (0.24 sec)

テーブルへデータの挿入

MariaDB [sample_db]> insert into sample_tb values(1,'太郎');
Query OK, 1 row affected (0.06 sec)
MariaDB [sample_db]> insert into sample_tb values(2,'花子');
Query OK, 1 row affected (0.07 sec)
MariaDB[sample_db]> exit
Bye

Pythonで操作する

インストール

pi@raspberry:~ $ pip install mysql-connector-python
pi@raspberry:~ $ pip install mysqlclient

操作するプログラム作成

pi@raspberry:~ $ touch db_connect.py
pi@raspberry:~ $ nano db_connect.py
db_connect.py
import mysql.connector as mydb
import sys

def mysqlConnector(_host, _port, _user, _passwd, _dbname):
  try:
    resconn = mydb.connect(
      host=_host,
      port=_port,
      user=_user,
      password=_passwd,
      database=_dbname
    )
  except Exception as e:
    print('[DB Connection Error]', e)
    sys.exit(1)
  
  # 接続が切れた場合に自動的に再接続する
  resconn.ping(reconnect=True)

  return resconn


user = 'pi'
password = 'raspberry'
host = 'localhost'
db = 'sample_db'

conn = mysqlConnector(host, 3306, user, password, db)
cur = conn.cursor()
sql = 'SELECT * FROM sample_tb;'
cur.execute(sql)

rows = cur.fetchall()

for row in rows:
 print(row)

cur.close
conn.close
  

実行

pi@raspberry:~ $ python db_connect.py
(1, '太郎')
(2, '花子')

PHPからDB操作

PDOインストール

pi@raspberry:~ $ sudo apt-get -y install php-mysql
pi@raspberry:~ $ sudo service apache2 restart

ファイルの編集

pi@raspberry:~ $ cd /var/www/html
pi@raspberry:/var/www/html $ nano index.php
index.php
<?php
$hostdbname = 'mysql:host=localhost;dbname=sample_db;charset=UTF8';
$username = "pi";
$password = "raspberry";
$pdo = new PDO($hostdbname, $username, $password);
$pdo -> setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

$sql = "SELECT * FROM sample_tb";
$stmt = $pdo -> query($sql);
foreach($stmt as $row){
  echo $row["num"];
  echo $row["name"];
}
?>
pi@raspberry:/var/www/html $ cd ~

実行

phpの確認

  • php.iniの位置 /etc/php/7.3/cli/php.ini

今後

  1. cを動かしてemボードからの通信検知
  2. cからShellScriptを呼び出す
  3. shellからpythonを呼び出して実行

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?