|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: latin-1 -*- |
| 3 | +# |
| 4 | +# test2.py - Test mit MySQL |
| 5 | +# |
| 6 | +# Copyright 2013 Mechtilde Stehmann <mechtilde@stephan> |
| 7 | +# |
| 8 | +# This program is free software; you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU General Public License as published by |
| 10 | +# the Free Software Foundation; either version 3 of the License, or |
| 11 | +# any later version. |
| 12 | +# |
| 13 | +# This program is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License |
| 19 | +# along with this program; if not, write to the Free Software |
| 20 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 21 | +# MA 02110-1301, USA. |
| 22 | + |
| 23 | +import os, sys |
| 24 | + |
| 25 | +from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, select |
| 26 | + |
| 27 | +from sqlalchemy.dialects.mysql import pymysql |
| 28 | + |
| 29 | +class test(object): |
| 30 | + def __init__(self): |
| 31 | + pass |
| 32 | + |
| 33 | + def connect_mysql(self,User,Password,Host,Database): |
| 34 | + filename = 'mysql+mysqldb://' + User + ':' + Password + '@' + Host + '/' + Database |
| 35 | + self.engine = create_engine(filename + '?charset=latin1') |
| 36 | + self.metadata = MetaData() |
| 37 | + self.metadata.bind = self.engine |
| 38 | + |
| 39 | + def create_tbl_adr(self): |
| 40 | + self.tab = Table('Adresse2', self.metadata, |
| 41 | + Column('id', Integer, primary_key = True), |
| 42 | + Column('Name', String(30)), |
| 43 | + Column('Vorname', String(30)), |
| 44 | + Column('StrasseNr', String(40)), |
| 45 | + Column('PLZ', String(10)), |
| 46 | + Column('Ort', String(10)), |
| 47 | + Column('Land', String(30))) |
| 48 | + self.metadata.create_all() |
| 49 | + |
| 50 | + def insert_adress(self, Name, Vorname, StrasseNr, PLZ, Ort, Land): |
| 51 | + conn = self.engine.connect() |
| 52 | + conn.execute(Table('Adresse2',self.metadata, autoload=True).insert(), [ |
| 53 | + {'Name': Name, 'Vorname': Vorname, 'StrasseNr': StrasseNr, 'PLZ': PLZ, 'Ort': Ort, 'Land': Land}]) |
| 54 | + |
| 55 | + def get_Table(self, tablename): |
| 56 | + tabc = Table(tablename, self.metadata, autoload=True).c |
| 57 | + if tablename == 'Adresse2': |
| 58 | + query = select([tabc.Name, tabc.Vorname, tabc.StrasseNr, tabc.PLZ, tabc.Ort, tabc.Land]) |
| 59 | + return self.engine.execute(query) |
| 60 | + else: |
| 61 | + print("Tabelle existiert nicht") |
| 62 | + |
| 63 | +def main(): |
| 64 | + root = test() |
| 65 | + root.connect_mysql('test','test','localhost','test') |
| 66 | + root.create_tbl_adr() |
| 67 | + root.insert_adress('Schmidt','Hans', 'Löhnstr.', '12345', 'Stadt', 'Land') |
| 68 | + daten = root.get_Table('Adresse2') |
| 69 | + |
| 70 | + for element in daten: |
| 71 | + print(element['Name']) |
| 72 | + |
| 73 | + return 0 |
| 74 | + |
| 75 | +if __name__ == '__main__': |
| 76 | + main() |
0 commit comments