Hello guys, I am new to SQLAlchemy and I wanted to try an example using a MySQL database and PyDev as an IDE but when I run it, PyDev gives me the following error :
Traceback (most recent call last): > File "/usr/lib/python2.7/site.py", line 68, in <module> > import os > File "/usr/lib/python2.7/os.py", line 49, in <module> > import posixpath as path > File "/usr/lib/python2.7/posixpath.py", line 17, in <module> > import warnings > File "/usr/lib/python2.7/warnings.py", line 8, in <module> > import types > File > "/home/zakaria/Bureau/Python/SQLAlchemy-0.9.7/lib/sqlalchemy/types.py", > line 21, in <module> > from .sql.type_api import ( > ValueError: Attempted relative import in non-package > What did I do wrong? And how can I fix it? Please find attached the test file "Personne.py" and thanks for your help! -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
#!/usr/bin/python2.7 # -*-coding:Utf-8 -* ''' Created on 13 août 2014 @author: zakaria ''' from sqlalchemy import create_engine from sqlalchemy.ext.declarative.api import declarative_base from sqlalchemy.orm.session import sessionmaker from sqlalchemy import Column, Integer, String #Chaîne de connexion engine = create_engine('mysql://root:root@localhost:3306/python', echo=False) #Session Session = sessionmaker(bind=engine) session = Session() Base = declarative_base() class Personne(Base): #Nom de la table __tablename__ = 'Personne' #Attributs id = Column(Integer, primary_key=True) nom = Column(String(50)) prenom = Column(String(50)) #Constructeur def __init__(self, nom, prenom): self.nom = nom self.prenom = prenom #Méthodes def __repr__(self): return "<Personne(%s, %s)>" % (self.nom, self.prenom) #Fin Classe Personne #Ajout d'une seule entrée new_record = Personne("TOTO", "Zak") session.add(new_record) session.commit() #Ajout de plusieurs entrées list_of_records = [Personne("TOTO", "Med"), Personne("LALA", "Mia")] session.add_all(list_of_records) session.commit() #Requêtage records = session.query(Personne).filter_by(nom='TOTO') for person in records: print person print "or\n" all_records = session.query(Personne).all() for p1 in all_records: print p1