mercredi 14 janvier 2015

How to feed excel sheet data to an SQLite Database row by row using xlrd?

1.#!/usr/bin/python


2.# -- coding: utf-8 --


3."""


4.This file is part of KMyMoney, A Personal Finance Manager for KDE


5.Copyright (C) 2014 Christian Dávid


6.

7.This program is free software; you can redistribute it and/or


8.modify it under the terms of the GNU General Public License


9.as published by the Free Software Foundation; either version 2


10.of the License, or (at your option) any later version.


11.

12.This program is distributed in the hope that it will be useful,


13.but WITHOUT ANY WARRANTY; without even the implied warranty of


14.MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the


15.GNU General Public License for more details.


16.

17.You should have received a copy of the GNU General Public License


18.along with this program. If not, see http://ift.tt/ytFmsU.


19."""


20.

21."""


22.Uses the " BIC-lijstvanNederlandsebankenvoorSEPA " of the dutch Betaalvereniging Nederland bank to create a bic lookup table for KMyMoney .


23.

24.@author: Christian David


25."""


26.# importing important packages


27.import sqlite3


28.import argparse


29.import xlrd


30.

31.def createTable():




  1. """ Create table structure




  2. """




  3. cursor = db.cursor()




  4. cursor.execute("DROP TABLE IF EXISTS institutions")




  5. cursor.execute(




  6. "CREATE TABLE institutions ("




  7. " country CHAR(2) DEFAULT 'NL' CONSTRAINT dutchCountryCode NOT NULL CHECK(country == 'NL'),"




  8. " bankcode CHAR(4) NOT NULL PRIMARY KEY CHECK(length(bankcode) = 4),"




  9. " bic CHAR(11),"




  10. " name VARCHAR(60)"




  11. " )"




  12. )




  13. db.commit()




45.# Have Some Doubt In Line 39 . If No Error , Please remove This Comment :)


46.def processFile(fileName):




  1. """ Fills the database with institutions saved in fileName




  2. """




  3. rowsInserted = 0




  4. cursor = db.cursor()




  5. cursor.execute("BEGIN")




  6. def submitInstitute(bankCode, bankName, bic):




  7. try:




  8. cursor.execute("INSERT INTO institutions (bankCode, bic, name) VALUES(?,?,?)", (bankCode, bic, bankName))




  9. except sqlite3.Error, e:




  10. print "Sorry , Error: {0} while inserting {1} ({2})".format(e.args[0], bankCode, bic)




  11. institutesFile = xlrd.open(fileName, "r", encoding=args.encoding)




  12. for institute in institutesFile:




  13. submitInstitute(institute[0:8], institute[9:67].strip(), institute[39:50])




  14. rowsInserted += 1




  15. db.commit();




  16. return rowsInserted


    70.


71.if name == 'main':




  1. parser = argparse.ArgumentParser(description="We Create an SQLite database for KMyMoney with information about IBAN and BICs based on an 'Excel 2007 sheet' from the Dutch Betaalvereniging Nederland bank."




  2. " You can go and download the source (.xlsx) file at http://ift.tt/1yevWEG "




  3. )




  4. parser.add_argument(dest='file', help='File to load')




  5. parser.add_argument('-o', '--output', default="dutchbankdata.nl.db", help='SQLite database to open/generate')




  6. args = parser.parse_args()




  7. print "Read data from \"{0}\"".format(args.file)




  8. db = sqlite3.connect(args.output)




  9. createTable()




  10. institutions = processFile( args.file )




  11. print "Inserted {0} institutions into database \"{1}\"".format(institutions, args.output)




  12. cursor = db.cursor();




  13. cursor.execute("ANALYZE institutions");




  14. cursor.execute("CREATE INDEX bic_index ON institutions (bic)");




  15. cursor.execute("REINDEX");




  16. cursor.execute("VACUUM");




  17. db.commit();




  18. db.close()




Aucun commentaire:

Enregistrer un commentaire