rssで取得したデータをmysqlに保存

a

$dbh = new PDO で接続、stmt->executeでsql文を実行
$stmt = $dbh->prepare(“insert into rss (title,link,site_title,site_link,date) values(?,?,?,?,?)”);
$stmt->execute(array($title,$link,$site_title,$site_link,$date));

reference
FETCH_ASSOC:Fetch a result row as an associative array

create table rss(
	id int not null auto_increment primary key,
	title varchar(255),
	link varchar(255),
	site_title varchar(64),
	site_link varchar(64),
	date datetime
);
<?php

try {
  $dbh = new PDO('mysql:host=localhost;dbname=test','dbuser','xxxx');
} catch(PDOException $e){
  var_dump($e->getMessage());
  exit;
}
$num = 5;//RSS取得件数

$rssUrl=array(
  'http://x6xo.hatenablog.com/rss',//サイトURL
);

//magpierss
require_once('./magpierss-master/rss_fetch.inc');
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');//encode
define('MAGPIE_CACHE_AGE','30');//cache

foreach($rssUrl as $no => $rss_url){
  if($rss_url != ''){
    //urlからRSSを取得
    $rss = @fetch_rss($rss_url);
      if($rss != NULL){
        for ($i=0; $i<count($rss->items); $i++){
          $rss->items[$i]["site_title"] = $rss->channel["title"];
          $rss->items[$i]["site_link"] = $rss->channel["link"];
        }
        // itemsを格納
        $rssItemsArray[] = $rss->items;
      }
  }
}

$concatArray = array();
if (is_array($rssItemsArray)) {
    for($i=0;$i<count($rssItemsArray);$i++){
    $concatArray = array_merge($concatArray,$rssItemsArray&#91;$i&#93;);//配列を統合する
  }

    foreach ($concatArray as $no => $values) {

        //RSSの種類によって日付を取得
        if($values['published']){$date = $values['published'];}
        elseif($values['created']){$date = $values['created'];}
        elseif($values['pubdate']){$date = $values['pubdate'];}
        elseif($values['dc']['date']){$date = $values['dc']['date'];}
        $date=date("Y-m-d H:i:s",strtotime($date));

        //Filter
        $nowtime = date("Y-m-d H:i:s",strtotime( "now" ));//現在時刻の取得
        if($date > $nowtime){//未来記事の排除
        }elseif(preg_match("/AD/", $values["title"])){//広告記事の排除
        }elseif(preg_match("/PR/", $values["title"])){
        }else{

            //値の定義
            $title=$values["title"];
            $link=$values["link"];
            $site_title=$values["site_title"];
            $site_link=$values["site_link"];

            //記事ごとに必要な項目を抽出
            $rssArray[]=array($date, $title, $link, $site_title, $site_link);
        }//
    }//

    //ソート
    function cmp($a, $b) {
        if ($a[0] == $b[0]) return 0;
        return ($a[0] > $b[0]) ? -1 : 1;
    }
    if($rssArray) { usort($rssArray, 'cmp'); }
    if(count($rssArray) > $num){$count=$num;}
    else{$count=count($rssArray);}

    for ($i=0; $i<$count; $i++) {
        $date=date("Y-m-d H:i:s",strtotime($rssArray&#91;$i&#93;&#91;0&#93;));
        $title=$rssArray&#91;$i&#93;&#91;1&#93;;
        $link=$rssArray&#91;$i&#93;&#91;2&#93;;
        $site_title=$rssArray&#91;$i&#93;&#91;3&#93;;
        $site_link=$rssArray&#91;$i&#93;&#91;4&#93;;
        $datelink = "<div>$date";
      $titlelink = "<a href='$link'>$title</a>";
      $site_titlelink = "<a href='$site_link'>[$site_title]</a></div>";
      echo "$datelink$titlelink$site_titlelink</div>";//(確認用)

      $stmt = $dbh->prepare("insert into rss (title,link,site_title,site_link,date) values(?,?,?,?,?)");
      $stmt->execute(array($title,$link,$site_title,$site_link,$date));
    }
}
?>

$stmt->fetchAll(PDO::FETCH_ASSOC) as $dataで、mysqlのデータを表示

<?php
// 接続
try {
  $dbh = new PDO('mysql:host=localhost;dbname=test','dbuser','xxxx');
} catch(PDOException $e){
  var_dump($e->getMessage());
  exit;
}
?>
<html>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>rss</title>
<body>
<?php

  date_default_timezone_set('Asia/Tokyo');

  $sql = "select * from rss order by date desc limit 10";
  echo '<table>';
  $stmt = $dbh->query($sql);
  foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $data){
    $date = date("m/d H:i",strtotime($data['date']));
    $title = ($data['title']);
    $link = ($data['link']);
    $site_link = ($data['site_link']);
    $site_title = ($data['site_title']);
    echo "<tr><td>$date</td><td><a href='$link'>$title</a></td><td><a href='$site_link'>[$site_title]</a></td></tr>";
  }

echo "</table>";

// 切断
$dbh = null;
?>

php rss

<?php
$num = 5;//RSS取得件数

$rssUrl=array(
  'http://x6xo.hatenablog.com/rss',//サイトURL
);

//magpierss
require_once('./magpierss-master/rss_fetch.inc');
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');//encode
define('MAGPIE_CACHE_AGE','30');//cache

foreach($rssUrl as $no => $rss_url){
  if($rss_url != ''){
    //urlからRSSを取得
    $rss = @fetch_rss($rss_url);
      if($rss != NULL){
        for ($i=0; $i<count($rss->items); $i++){
          $rss->items[$i]["site_title"] = $rss->channel["title"];
          $rss->items[$i]["site_link"] = $rss->channel["link"];
        }
        // itemsを格納
        $rssItemsArray[] = $rss->items;
      }
  }
}

$concatArray = array();
if (is_array($rssItemsArray)) {
    for($i=0;$i<count($rssItemsArray);$i++){
    $concatArray = array_merge($concatArray,$rssItemsArray&#91;$i&#93;);//配列を統合する
  }

    foreach ($concatArray as $no => $values) {

        //RSSの種類によって日付を取得
        if($values['published']){$date = $values['published'];}
        elseif($values['created']){$date = $values['created'];}
        elseif($values['pubdate']){$date = $values['pubdate'];}
        elseif($values['dc']['date']){$date = $values['dc']['date'];}
        $date=date("Y-m-d H:i:s",strtotime($date));

        //Filter
        $nowtime = date("Y-m-d H:i:s",strtotime( "now" ));//現在時刻の取得
        if($date > $nowtime){//未来記事の排除
        }elseif(preg_match("/AD/", $values["title"])){//広告記事の排除
        }elseif(preg_match("/PR/", $values["title"])){
        }else{

            //値の定義
            $title=$values["title"];
            $link=$values["link"];
            $site_title=$values["site_title"];
            $site_link=$values["site_link"];

            //記事ごとに必要な項目を抽出
            $rssArray[]=array($date, $title, $link, $site_title, $site_link);
        }//
    }//

    //ソート
    function cmp($a, $b) {
        if ($a[0] == $b[0]) return 0;
        return ($a[0] > $b[0]) ? -1 : 1;
    }
    if($rssArray) { usort($rssArray, 'cmp'); }
    if(count($rssArray) > $num){$count=$num;}
    else{$count=count($rssArray);}

    for ($i=0; $i<$count; $i++) {
        $date=date("Y-m-d H:i:s",strtotime($rssArray&#91;$i&#93;&#91;0&#93;));
        $title=$rssArray&#91;$i&#93;&#91;1&#93;;
        $link=$rssArray&#91;$i&#93;&#91;2&#93;;
        $site_title=$rssArray&#91;$i&#93;&#91;3&#93;;
        $site_link=$rssArray&#91;$i&#93;&#91;4&#93;;
        $datelink = "<div>$date";
      $titlelink = "<a href='$link'>$title</a>";
      $site_titlelink = "<a href='$site_link'>[$site_title]</a></div>";
      echo "$datelink$titlelink$site_titlelink</div>";//(確認用)
    }
}
?>

mysql

create table rss(
	id int not null auto_increment primary key,
	title varchar(255),
	link varchar(255),
	site_title varchar(64),
	site_link varchar(64),
	date datetime
);

Python DB-API

Python DB-API is a library method call sqlite and data.

writing DB-API

import sqlite3
conn = sqlite3.connect("Cookies")
cursor = conn.cursor()
cursor.execute(
    "select host_key from cookies limit 10")

results = cursor.fetchall()
print results
conn.close()

Watch how DB-API work below.

import sqlite3

# Fetch some student records from the database.
db = sqlite3.connect("students")
c = db.cursor()
query = "select name, id from students;"
c.execute(query)
rows = c.fetchall()

# First, what data structure did we get?
print "Row data:"
print rows

# And let's loop over it too:
print
print "Student names:"
for row in rows:
  print "  ", row[0]

db.close()

Result
Row data:
[(u’Jade Harley’, 441304), (u’Harry Evans-Verres’, 172342), (u’Taylor Hebert’, 654321), (u’Diane Paiwonski’, 773217), (u’Melpomene Murray’, 102030), (u’Robert Oliver Howard’, 124816), (u’Hoban Washburne’, 186753), (u’Trevor Bruttenholm’, 162636), (u’Jonathan Frisby’, 917151)]

Student names:
Jade Harley
Harry Evans-Verres
Taylor Hebert
Diane Paiwonski
Melpomene Murray
Robert Oliver Howard
Hoban Washburne
Trevor Bruttenholm
Jonathan Frisby

order by [ASC | DESC]

sql join on

QUERY = '''
select animals.name 
from animals join diet on animals.species = diet.species
where food = 'fish';
'''

having

QUERY = '''
select species, count(*) as num 
from animals group by species
having num = 1;
'''
QUERY = '''
select species, count(*) as num 
from animals group by species
having num = 1;
'''

QUERY = '''
select diet.food, count(*) as num
from animals join diet on animals.species = diet.species
group by food
having num = 1;
'''

python sql

QUERY = '''
select name, birthdate from animals where species = 'gorilla';
'''

+———+————+
| name | birthdate |
+=========+============+
| Max | 2001-04-23 |
| Dave | 1988-09-29 |
| Becky | 1979-07-04 |
| Liz | 1998-06-12 |
| George | 2011-01-09 |
| George | 1998-05-18 |
| Wendell | 1982-09-24 |
| Bjorn | 2000-03-07 |
| Kristen | 1990-04-25 |
+———+————+

select name, birthdate from animals where species = ‘gorilla’ and name = ‘Max’

否定

QUERY = '''
select name, birthdate from animals where species != 'gorilla' and name != 'Max';
'''

between文

QUERY = '''
select name from animals where species = 'llama' AND birthdate between '1995-01-01' AND '1998-12-31';
'''

query

QUERY = "select max(name) from animals;"

QUERY = "select * from animals limit 10;"

QUERY = "select * from animals where species = 'orangutan' order by birthdate;"

QUERY = "select name from animals where species = 'orangutan' order by birthdate desc;"

QUERY = "select name, birthdate from animals order by name limit 10 offset 20;"

QUERY = "select species, min(birthdate) from animals group by species;"

QUERY = '''
 select name, count(*) as num from animals
 group by name
 order by num desc
 limit 5;
 '''

Limit count Offset skip
limit is how many rows to return
offset is how far into the results to start

Order by columns DESC
columns is which columns to sort by, separated with commas
DESC is sort in reverse order(descending)

Group by columns
columns is which columns to use as groupings when aggregating

QUERY = "select count(*) as num, species from animals group by species order by num desc;"

insert

INSERT_QUERY = '''
insert into animals (name, species, birthdate) values ('yoshi', 'opposum', '2016-12-13');
'''

Ajaxを使ってMySQLの更新をHTML側で自動更新

$(“”).empty();で、一旦データを空にして、setTimeout、countupで自動更新します。

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <title>Ajax</title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
    // setTimeout関数で2秒ごとに取得
    var countup = function(){
    $("#content").empty();
    $(document).ready(function(){
      // /**
      // * Ajax通信メソッド
      // * @param type
      // * @param url
      // * @param dataType
      // ** /
      $.ajax({
      type: "POST",
      url: 'json.php',
      dataType: "json",

      success: function(data, dataType)
      {
        if(data == null) alert('データが0件でした');

        var $content = $('#content');
        for (var i = 0; i<data.length; i++)
        {
          $content.append("<li>" + data[i].name + "</li>");
        }
      },
      error: function(XMLHttpRequest, textStatus, errorThrown)
      {
        alert('Error : ' + errorThrown);
      }
    });
  });
    setTimeout(countup, 2000);
  }
  countup();
    </script>
  </head>
  <body>
    <h1>sample</h1>
    <ul id="content"></ul>
  </body>
</html>

phpでMySQLに接続する書き方

phpでMySQLに接続, sql文にてデータを挿入する書き方例
$stmt->executeにて実行しています。変数の$dbhはdbhandlerの略です。

  try {
    $dbh = new PDO('mysql:host='/DB_HOST.';dbname='.DB_NAME,DB_USER,DB_PASSWORD);
  } else (PDOException $e){
    echo $e->getMessage();
    exit;
  }

  $stmt = $dbh->prepare("select * from users where instagram_user_id=:user_id limit 1");
  $stmt->execute(array(":user_id"=>$json->user->id));
  $user = $stmt->fetch();

if (empty($user)){
    $stmt = $dbh->prepare("insert into users(instagram_user_id, instagram_user_name, instagram_profile_picture, instagram_access_token, created, modified) values (:user_id, :user_name, :profile_picture,  :access_token, now(), now());");
    $pramas = array(
      ":user_id" =>$json->user->id,
      ":user_name" =>$json->user->username,
      ":profile_picture" =>$json->user->profile_picture,
      ":access_token"=>$json->access_token
    );
    $stmt->execute($params);

データの取得

$stmt = $dbh->prepare("select * from users where id=:last_insert_id limit 1");
    $stmt->execute(array(":last_insert_id"=>$dbh->lastInsertId()));
    $user = $stmt->fetch();

sql commands vol.2

.headers on
.mode column
update users set score = 0, name ='* ' || name where score < 60;
delete from users where score = 0;
select * from users;
&#91;/sql&#93;

transaction
&#91;sql&#93;
begin transaction;
update users set score = score - 10 where name = 'sato';
update users set score = score + 10 where name = 'kimura';
commit;
select * from users;
rollback;
&#91;/sql&#93;

trigger
&#91;sql&#93;
.headers on
.mode column
create table messages (message);

create trigger new_winner update of score on users when new.score > 100
  begin
   insert into messages (message) values (
     'name: ' || new name ||
     ' ' || old. score ||
     ' ->' || new.score
   );
   end;

update users set score = score + 30;
select * from messages;

index

create index score_index on users(score);
create unique index name_index on users(name);

.indices users
.schema users

drop index if exists score_index;
drop index if exists name_index;

日時

select datetime('now', '+09:00:00');
select date('2016-12-01', '+3 months', 'start of month', '-1 day');

create table if not exists users (
  id integer primary key,
  name varchar(255) not null,
  score integer default 10 check(score >= 0),
  team text,
  created datetime default (datetime('now', '+09:00:00'))
);

複数テーブルの作成

drop table if exists posts;
create table if not exists posts (
  id integer primary key,
  title text,
  body text,
  created datetime default (datetime('now', '+09:00:00'))
);

insert into posts (id, title, body) values (1, 't1', 'b1');
insert into posts (id, title, body) values (2, 't2', 'b2');

drop table if exists comments;
create table if not exists comments (
    id integer primary key,
    post_id integer,
    comment text,
    created datetime default (datetime('now', '+09:00:00'))
  );
  insert into comments (id, post_id, comment) values (1, 1, 'c1');
  insert into comments (id, post_id, comment) values (2, 1, 'c2');

.headers on
.mode column

select * from posts;
select * from comments;

内部結合 inner join、外部結合 left outer join、交差結合 cross join

select * from posts inner join comments on posts.id = comments.post_id;
select * from posts join comments on posts.id = comments.post_id;
select posts.id, posts.title, comment from posts join comments on posts.id = comments.post_id;
select * from posts left outer join comments on posts.id = comments.post_id;
select * from posts cross join comments;

row id ※integer primary key -> row id

select rowid, * from users;

dump

.output users.dump.sql
.dump users

外部データの取り込み

drop table if exists users;
create table users (
  id integer primary key,
  name,
  score
);
insert into users (name, score) values ('a', 10);
insert into users (name, score) values ('b', 20);
insert into users (name, score) values ('c', 30);

create table temp(name, score);

.mode csv
.import users.csv temp

insert into users(name, score) select name, score from temp;
select * from users;

drop table temp;

.mode csv
.output users_out.csv
select * from users;