pythonでfoobarのalternativeを作る 6 190513 version 2

2019/05/14 09:38 by yamasyuh68
  :追加された部分   :削除された部分
(差分が大きい場合、文字単位では表示しません)
自分のPCの音楽ファイルをタグでデータベース化 6
# 190513 の開発日記

pythonつかいづらい┐('д')┌
sqlite winexeとした
簡単、いわ~( ´∀`)
素だとselect文ですぐ結果表示されるはずなにpythonだとexecuteしたあでfetchで取り出てprintしなくちゃイケな
で、sqlite のwin用exe落とした
コンソールだけど簡単で、いいわ~( ´∀`)
exeが三つ入ってるがとりあえず本体だけ解凍してdbのフォルダに置いたらすぐ使えた
当たり前だけど解説サイトの通りですぐ結果がわかる

# コード
二段階方式の一段階目だけのコード書いてみた
新規時も更新時も使えるように

```
import sqlite3 , glob ,os ,time

# 更新の実験

conn = sqlite3.connect(r'e:\Programs\python\_研究\database\database.db')
c = conn.cursor()
sel='select  mtime from musics where fullpath = ?'
fo=r'i:\Music\jazz\**'
count={'all':0 , 'file':0 , 'same':0 , 'diff':0 ,'none':0 , 'new':0}
start = time.time()

for p in glob.iglob(fo , recursive=True):
    count['all'] += 1
    if os.path.isfile(p):
        count['file'] += 1
        try:
            c.execute( "INSERT INTO musics (fullpath) VALUES (?)" , (p, ))
            count['new'] += 1
        except:
            c.execute('select  mtime from musics where fullpath = ?',(p, ))
            res=c.fetchone()
            nmtime=os.path.getmtime(p)
            if res :
                if res[0]==nmtime :
                    count['same'] += 1
                else :
                    count['diff'] += 1
                    print(p)
                    c.execute('update  musics set mtime = ? where fullpath = ? ',( None, p ))



elapsed_time = time.time() - start
print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")
print(count)

conn.commit()
conn.close()
``````

- 重複チェックをtry文でエラー出させる方式にしたがこういうのは推奨されないのだろうか??最初にデータの存在有無をチェックしても良いんだけど
新規時はこの方が良いよな
更新時はほとんどがエラーになる
- 更新時について、このtry方式だと、
エラーではじかれる、mtimeを取得して比較する、同じならパス、違ったらmtimeをNULLにして
- 
try文は強引だろうか??
      

190513 の開発日記

pythonつかいづらい┐('д')┌
素だとselect文ですぐ結果表示されるはずなのにpythonだとexecuteしたあとでfetchで取り出してprintしなくちゃイケない

で、sqlite のwin用exe落とした
コンソールだけど簡単で、いいわ~( ´∀`)
exeが三つ入ってるがとりあえず本体だけ解凍してdbのフォルダに置いたらすぐ使えた
当たり前だけど解説サイトの通りですぐ結果がわかる

コード

二段階方式の一段階目だけのコード書いてみた
新規時も更新時も使えるように

import sqlite3 , glob ,os ,time

# 更新の実験

conn = sqlite3.connect(r'e:\Programs\python\_研究\database\database.db')
c = conn.cursor()
sel='select  mtime from musics where fullpath = ?'
fo=r'i:\Music\jazz\**'
count={'all':0 , 'file':0 , 'same':0 , 'diff':0 ,'none':0 , 'new':0}
start = time.time()

for p in glob.iglob(fo , recursive=True):
    count['all'] += 1
    if os.path.isfile(p):
        count['file'] += 1
        try:
            c.execute( "INSERT INTO musics (fullpath) VALUES (?)" , (p, ))
            count['new'] += 1
        except:
            c.execute('select  mtime from musics where fullpath = ?',(p, ))
            res=c.fetchone()
            nmtime=os.path.getmtime(p)
            if res :
                if res[0]==nmtime :
                    count['same'] += 1
                else :
                    count['diff'] += 1
                    print(p)
                    c.execute('update  musics set mtime = ? where fullpath = ? ',( None, p ))



elapsed_time = time.time() - start
print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")
print(count)

conn.commit()
conn.close()
  • 重複チェックをtry文でエラー出させる方式にしたがこういうのは推奨されないのだろうか??最初にデータの存在有無をチェックしても良いんだけど
    新規時はこの方が良いよな
    更新時はほとんどがエラーになる
  • 更新時について、このtry方式だと、
    エラーではじかれる、mtimeを取得して比較する、同じならパス、違ったらmtimeをNULLにして

try文は強引だろうか??