0% found this document useful (0 votes)
9 views

mysql2csvall

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

mysql2csvall

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#!

/usr/bin/env python
#[email protected]
#14-02-2014
#---------------

import sys
import os
import datetime
from ipserver import IPNYA
import csv
import MySQLdb

#set penamaan file (format Victoria_(Day-1).csv)


now=datetime.datetime.now()
now -= datetime.timedelta(days=1)
tTime=now.strftime("%Y%m%d%H")
FILENAME="/home/data/chiken_crush_" + tTime + ".csv"

#set konfigurasi database


#hostname="192.168.1.5"
dbuser="dinas"
dbpass="dinas123"
dbname="db_cc_jambi"

#set Query String


#SELECTQ="select pscd,trdt,trno,rvnamt,svchgamt,taxamt from iafjrndt where
active='1' and svchgamt>'0' and taxamt>'0' and date(trdt)=curdate() - interval 1
day order by pscd"
SELECTQ="select id_nota,no_faktur,tanggal,input_date,subtotal,diskon,pajakppn,total
from penjualan where id_jenis IN ('K','T','G') and date(input_date)between
date_sub(curdate(),interval 31 day) and curdate()"
# where date(tgl)>=curdate() - interval 1 day order by tgl asc"
#SELECTQ="select * from device"

#open connection to database


db = MySQLdb.connect(host=IPNYA, user=dbuser, passwd=dbpass, db=dbname, port=3306)

#prepare csv file


dump_writer = csv.writer(open(FILENAME,'w'), delimiter=',',quotechar="'")
cursor = db.cursor()
#execute query, fetching data row
cursor.execute(SELECTQ)
result = cursor.fetchall()

#get column names


field_names = [i[0] for i in cursor.description]
dump_writer.writerow(field_names)
#dump to csv
#data = [row[0] for row in cursor.fetchall()]
for record in result:
dump_writer.writerow(record)

#close connection
db.close()

You might also like