JJC's 테크니컬 다이어리

Python SQLite 코드 샘플-select 본문

SQLite

Python SQLite 코드 샘플-select

털털한JJC 2009. 10. 29. 16:15
#!/opt/bin/python
# -*- coding: cp949 -*-
import sys
sys.path.append('/opt/lib/python2.4/site-packages')
from pysqlite2 import dbapi2 as sqlite
from datetime import date
import csv, os, string,cgi

print "Content-Type: text/html"
print
# Create a connection to the database file "mydb":
con = sqlite.connect("mydb")
cur = con.cursor()

# we can also implement a custom text_factory ...
# here we implement one that will ignore Unicode characters that cannot be
# decoded from UTF-8
#con.text_factory = lambda x: unicode(x, "utf-8", "ignore")

# by default, rows are returned as Unicode
# but we can make pysqlite always return bytestrings ...
con.text_factory = str
#con.text_factory = pysqlite2.dbapi2.OptimizedUnicode
now=date.today()
# 3 months ago
m3ago=date(now.year,now.month-3,1)
#fm="2007-06-14"
fm=m3ago.strftime("%Y.%m.%d")
#to="2007-06-15"
to=now.strftime("%Y.%m.%d")
print "최근3개월사용정보"
print "<br>"
print "<br>"
print "시작일: "+fm
print "~"
print "오늘  : "+to
print "<br>"
print "기간내 접수 기준 금액입니다."
print "<br><br>"
# Execute the SELECT statement:
cur.execute("select cardno,sum(price) from credituse where (usedate between ? and ?) and accept='접수' group by cardno",(fm,to))

# Retrieve all rows as a sequence and print that sequence:
for row in cur:
    for col in row:
        print col
    print "<br>"
print "<br>"

m2ago=date(now.year,now.month-2,1)
nextm=date(now.year,now.month+1,1)
fm2=m2ago.strftime("%Y.%m.%d")
to2=nextm.strftime("%Y.%m.%d")

print "익월 첫날 예상"
print "<br>"
print "<br>"
print "시작일: "+fm2
print "~"
print "담달첫날  : "+to2
print "<br>"
print "기간내 접수 기준 금액입니다."
print "<br><br>"

# Execute the SELECT statement:                                                                                
cur.execute("select cardno,sum(price) from credituse where (usedate between ? and ?) and accept='접수' group by cardno",(fm2,to2))

# Retrieve all rows as a sequence and print that sequence:                                                       
for row in cur:
    for col in row:
        print col
    print "<br>"
print "<br>"


print "--참고--<br>"
print "<a href=mywayinfo.html target=_blank>"
print "V344:MY WAY</a><br>"
print "V353:OUTBACK<br>"
print "<a href='http://www.hanabank.com/online/contents/card/product/prod_02/prod_0206/prod_02063/index.jsp' target=_blank>"
print "V876:AUTO"
print "</a>"
print "<br>"
    
print "<br>상세내역<br>"
cur.execute("select * from credituse where usedate between ? and ? order by usedate",(fm,to))
for row in cur:
    for col in row:
        print col
    print "<br>"

print "<br>"
print "<a href=index.html>처음으로가기</a>"