0
2

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.

PHP で Sqlite3 を扱う

Last updated at Posted at 2018-07-27

PHP7 で sqlite3 のファイルに、Read, Update を行います。

Ubuntu でドライバーのインストール

sudo apt install php7.4-sqlite3
cities.php
<?php
//	cities/cities.php?id=t0716
//
//						Jul/27/2018
// --------------------------------------------------------------------------
ini_set("display_errors", 1);
error_reporting(E_ALL);

echo '*** update.php start ***<br />';
echo '*** id = ' . $_GET['id'] . '<br />';


if(isset($_POST['population']))
	{
	$population=$_POST['population'];
	$dbcon= new PDO("sqlite:cities.db");
	echo '*** population = ' . $population . '<br />';

	if($dbcon !==null)
		{
		date_default_timezone_set('Asia/Tokyo');
		$line=date('Y/m/d-H:i');

		$command="UPDATE cities SET population=$population,date_mod='$line' WHERE id='" . $_GET['id'] . "';";

		echo $command . '<br />';

		try
			{
			$stmt = $dbcon-> prepare($command);
			$stmt -> execute();
			}catch (PDOException $e){
				print('Error:'.$e->getMessage());
				die();
			}

		$sql_c = 'select id,population from cities';
		foreach ($dbcon->query($sql_c) as $row)
			{
			echo $row['id'] . '&nbsp;&nbsp;' . 'population = ' .  $row['population'] . '<br />';
			}
		}
	}

	$dbcon=null;
// --------------------------------------------------------------------------
?>

<html>
<head>
<title>Cities</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</head>
<body>
<h2>Update</h2>
</h2>

<table width="800" border="1" cellpadding="3" cellspacing="0">
<tr bgcolor="#AAAAAA">
<th width="60"><p>id</p></th>
<th width="200"><p>name</p></th>
<th width="80"><p>population</p></th>
<th width="65"><p>date_mod</p></th>
<th width="150"><p>更新</p></th>
</tr>

<?php
$dbcon= new PDO("sqlite:cities.db");
$sql_a = "SELECT * FROM cities WHERE id='" . $_GET['id'] . "';";
foreach($dbcon->query($sql_a) as $row){
?>

<tr>
<td>
<?php
print($row['id']); 
?>
</p></td>
<td><?php print($row['name']); ?></p></td>
<td><?php print($row['population']); ?></p></td>
<td><?php print($row['date_mod']); ?></p></td>
<td>
<?php
}

$dbcon=null;
?>
<br>
<form method="post" action="cities.php?id=<?php print($_GET['id']); ?>"/>
<input type="double" name="population" size="6"><br><br>
<input type="submit" value="更新"/></p>
</form>
</td></tr>
</table>
<p />

Jul/27/2018<p />
</body>
</html>

cities.db の dump ファイルです。

cities.dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE cities (id varchar(10) NOT NULL PRIMARY KEY,name text,population int,date_mod text);
INSERT INTO cities VALUES('t0711','郡山',34516,'2002-8-9');
INSERT INTO cities VALUES('t0712','会津若松',84973,'2002-9-21');
INSERT INTO cities VALUES('t0713','白河',98214,'2002-6-7');
INSERT INTO cities VALUES('t0714','福島',9999,'2018/07/27-18:46');
INSERT INTO cities VALUES('t0715','喜多方',72145,'2002-7-24');
INSERT INTO cities VALUES('t0716','二本松',91235,'2018/07/27-18:37');
INSERT INTO cities VALUES('t0717','いわき',82754,'2002-4-9');
INSERT INTO cities VALUES('t0718','相馬',57213,'2002-5-14');
INSERT INTO cities VALUES('t0719','須賀川',21598,'2002-3-12');
COMMIT;

cities.db を作成するには、

sqlite3 cities.db < cities.dump

ブラウザーでテストするには、次のようにアクセスします。

http://localhost/tmp/cities/cities.php?id=t0711

php7_sqlite3_jun13.png

curl でテストするには、

URL="http://localhost/tmp/cities/cities.php?id=t0716"
curl $URL -d population=91235 > tmp01

ファイル属性をうまく設定しないと、update が出来ません。

$ ls -al cities.db
-rw-rw-rw- 1 uchida wheel 12288 Jul 27 18:53 cities.db

cities というフォルダの属性を、0777 にしておけばオーケーです。

$ ls -al | grep cities
drwxrwxrwx 2 uchida wheel  4096 Jul 27 18:59 cities

データベースの確認方法

$ sqlite3 cities.db
SQLite version 3.34.1 2021-01-20 14:10:07
Enter ".help" for usage hints.
sqlite> select * from cities;
t0711|郡山|34516|2002-8-9
t0712|会津若松|84973|2002-9-21
t0713|白河|98214|2002-6-7
t0714|福島|9999|2018/07/27-18:46
t0715|喜多方|72145|2002-7-24
t0716|二本松|91235|2018/07/27-18:37
t0717|いわき|82754|2002-4-9
t0718|相馬|57213|2002-5-14
t0719|須賀川|21598|2002-3-12
sqlite> .exit

次のバージョンで確認しました。

```text
$ php --version
PHP 7.4.16 (cli) (built: Mar 23 2021 16:15:03) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies
0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?