Mysql Interface error

Installation und Anwendung von Datenbankschnittstellen wie SQLite, PostgreSQL, MariaDB/MySQL, der DB-API 2.0 und sonstigen Datenbanksystemen.
Antworten
doca82
User
Beiträge: 48
Registriert: Mittwoch 16. September 2009, 19:39
Wohnort: Berlin

Hallo,

Ich bin noch ziemlich am Anfang mit meinen Python- und MySql-Kenntnissen und ich weiß mal wieder nicht weiter (Sorry für die schlechte Syntax und die Wiederholungen;P). Ich versuche Daten aus einer MySql Datenbank auszulesen und diese in eine neue MySql DB einzufügen. Aber das Ding will nicht, wie ich will....nämlich dass er das ohne Interface Error macht.... vielleicht hat jemand von Euch eine Idee was da los ist...

Gruß

Code: Alles auswählen


import MySQLdb

class GetBills(object):
    
    def __init__(self, date, from_date, until_date):
        if date != "":
            conn = MySQLdb.connect (host = "localhost",
                            user = "user",
                            passwd = "passwort",
                            db = "test")
            cursor = conn.cursor()
            cursor.execute("SELECT c.`account_id`, c.`name`,l.`package`,\
            c.`street_name`, c.`street_nr`, c.`postal_code`,c.`city`, c.`country_code`, p.`listing_id`,\
            p.`company_name`, l.`listed_since_l`,pg.`id` FROM customers c,\
            listings l, previews p, packages pg WHERE l.`account_id`=c.`account_id`AND l.`id`=p.`listing_id` \
            AND DATE(listed_since_l)= DATE(%s) ORDER BY c.`name`", (date,))
            bills = cursor.fetchall()
            cursor.close ()
            conn.close ()
            self.Bills = bills            
        if from_date and until_date != "":
            conn = MySQLdb.connect (host = "localhost",
                            user = "user",
                            passwd = "passwort",
                            db = "test")
            cursor = conn.cursor()
            cursor.execute("SELECT c.`account_id`, c.`name`,l.`package`,\
            c.`street_name`, c.`street_nr`, c.`postal_code`,c.`city`, c.`country_code`, p.`listing_id`,\
            p.`company_name`, l.`listed_since_l`,pg.`id` FROM customers c,\
            listings l, previews p, packages pg WHERE l.`account_id`=c.`account_id`AND\
            l.`id`=p.`listing_id` AND DATE(listed_since_l) BETWEEN DATE(%s)\
            AND DATE(%s) ORDER BY c.`name`", (from_date, until_date,))
            bills = cursor.fetchall()
            cursor.close ()
            conn.close ()
            self.Bills = bills
        
    def into_lokal_db(self):
        conn = MySQLdb.connect (host = "localhost",
                           user = "user",
                            passwd = "passwort",
                            db = "test")
  
        for data in self.Bills:
            cursor = conn.cursor()
            cursor.execute("INSERT INTO customer (id_customer_lk, name_lk, street_name_lk,\
            street_nr_lk,postal_code_lk, city_lk, country_code_lk)VALUES (%s,%s,%s,%s,%s,%s,%s)"\
            ,(data[0],data[1],data[3],data[4],data[5],data[6],data[7]))
            conn.commit()
            conn.close ()
                

a= GetBills("","2008-08-13","2009-10-27")
a.into_lokal_db()
Die Fehlermeldungen lauten:

Traceback (most recent call last):
File "C:blabla", line 69, in <module>
a.into_lokal_db()
File "C:blabla", line 53, in into_lokal_db
cursor.execute("INSERT INTO customer (id_customer_lk, name_lk, street_name_lk, street_nr_lk,postal_code_lk, city_lk, country_code_lk)VALUES (%s,%s,%s,%s,%s,%s,%s)",(data[0],data[1],data[3],data[4],data[5],data[6],data[7]))
File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 147, in execute
charset = db.character_set_name()
_mysql_exceptions.InterfaceError: (0, '')
Benutzeravatar
jens
Python-Forum Veteran
Beiträge: 8502
Registriert: Dienstag 10. August 2004, 09:40
Wohnort: duisburg
Kontaktdaten:

Da fehlt ein Leerzeichen: ...country_code_lk)VALUES (%s...

Mehr Spass macht es, wenn du ein ORM nutzt.

GitHub | Open HUB | Xing | Linked in
Bitcoins to: 1JEgSQepxGjdprNedC9tXQWLpS424AL8cd
doca82
User
Beiträge: 48
Registriert: Mittwoch 16. September 2009, 19:39
Wohnort: Berlin

Vielen Dank für den Hinweis und die Mühe das durchzulesen ;)

Habe jetzt ein Leerzeichen eingefügt, wie Du meintest

Code: Alles auswählen

 for data in self.Bills:
            cursor = conn.cursor()
            cursor.execute("INSERT INTO customer (id_customer_lk, name_lk, street_name_lk,\
            street_nr_lk,postal_code_lk, city_lk, country_code_lk) VALUES (%s,%s,%s,%s,%s,%s,%s)"\
            ,(data[0],data[1],data[3],data[4],data[5],data[6],data[7]))
            conn.commit()
Den Interface error gibt es jedoch weiterhin....

Aber vielleicht wäre es sowieso cleverer Deinen Tip mit dem ORM weiterzuverfolgen....sonst werde ich noch verrückt....
Kannst du da ein Tool empfehlen oder gibt es da bei Python schon was mitgeliefert?

Danke und noch einen schönen Tag
Benutzeravatar
jens
Python-Forum Veteran
Beiträge: 8502
Registriert: Dienstag 10. August 2004, 09:40
Wohnort: duisburg
Kontaktdaten:

Da ich eine Web App mit django ( http://docs.djangoproject.com ) mache, nutzte ich das ORM von django.

Im allgemeinen wird viel von http://www.sqlalchemy.org/ gehalten.

GitHub | Open HUB | Xing | Linked in
Bitcoins to: 1JEgSQepxGjdprNedC9tXQWLpS424AL8cd
doca82
User
Beiträge: 48
Registriert: Mittwoch 16. September 2009, 19:39
Wohnort: Berlin

sqlchemesty scheint eine gute wahl zu sein...mal gucken, ob ich mich da einarbeiten kann.

Danke Gruß
Antworten