mysql - Python - Import CSV file and save it into Database SQLAlchemy -
i have prblem importing csv-file database... im using sqlalchemy in python , wanted open csv-file show in qtablewidget maybe change values , after write db (new table).
def setintable(self): colcnt = len(self.title) rowcnt = len(self.data) self.tabel_model = qtgui.qtablewidget(rowcnt, colcnt) vheader = qtgui.qheaderview(qtcore.qt.orientation.vertical) self.tabel_model.setverticalheader(vheader) hheader = qtgui.qheaderview(qtcore.qt.orientation.horizontal) self.tabel_model.sethorizontalheader(hheader) self.tabel_model.sethorizontalheaderlabels(self.title) in range(rowcnt): j in range(len(self.data[0])): item = qtgui.qtablewidgetitem(str(self.data[i][j])) self.tabel_model.setitem(i, j, item) self.tabel_model.horizontalheader().sectiondoubleclicked.connect(self.changehorizontalheader) self.setcentralwidget(self.tabel_model)
get csv-data
def getdata(filepath): open(filepath, 'r') csvfile: sample = csvfile.read(1024) dialect = csv.sniffer().sniff(sample, [';',',','|']) csvfile.seek(0) reader = csv.reader(csvfile,dialect=dialect) header = next(reader) lines = [] line in reader: lines.append(line) return lines
reading , showing csv-file data in qtablewidget working .. dont know how save mysql database
for easier way load csv database table, check out 'odo' python project - https://pypi.python.org/pypi/odo/0.3.2
--
to use table via sql alchemy 1 approach use session , call "update":
myrow = mytable( column_a = 'foo', column_b = 'bar') myrow.column_c = 1 + 2 mysession.update(myrow)
Comments
Post a Comment