I'm a beginner of "Python with SqlAlchemy". I try the following to create the table in "MySQL" Database.
My Code is :
import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy import MetaData, Column, Table, ForeignKey
from sqlalchemy import Integer, String
#DB Connection String
conn_str = "mysql+pymysql://root:*****@localhost/globalTracker?charset=utf8&use_unicode=0"
engine = create_engine(conn_str)
metadata = MetaData()
user = Table('users', metadata,
Column('user_id', Integer, primary_key=True),
Column('name', String(40)),
Column('age', Integer),
Column('password', String(80)),
mysql_engine='InnoDB'
)
user.create(engine, checkfirst=True)
I don't know the mistake on my code.
ERROR:
AttributeError Traceback (most recent call last)
/home/shankar/.local/lib/python3.5/site-packages/sqlalchemy/sql/elements.py in __getattr__(self, key)
731 try:
--> 732 return getattr(self.comparator, key)
733 except AttributeError:
AttributeError: 'Comparator' object has no attribute '_columns'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-46-d21d5be2c28c> in <module>()
16 Column('age', Integer,
17 Column('password', String(80)),
---> 18 mysql_engine='InnoDB'
19 ))
20 user.create(engine, checkfirst=True)
/home/shankar/.local/lib/python3.5/site-packages/sqlalchemy/sql/schema.py in __init__(self, *args, **kwargs)
1212 args.append(DefaultClause(self.server_onupdate,
1213 for_update=True))
-> 1214 self._init_items(*args)
1215
1216 util.set_creation_order(self)
/home/shankar/.local/lib/python3.5/site-packages/sqlalchemy/sql/schema.py in _init_items(self, *args)
81 for item in args:
82 if item is not None:
---> 83 item._set_parent_with_dispatch(self)
84
85 def get_children(self, **kwargs):
/home/shankar/.local/lib/python3.5/site-packages/sqlalchemy/sql/base.py in _set_parent_with_dispatch(self, parent)
431 def _set_parent_with_dispatch(self, parent):
432 self.dispatch.before_parent_attach(self, parent)
--> 433 self._set_parent(parent)
434 self.dispatch.after_parent_attach(self, parent)
435
/home/shankar/.local/lib/python3.5/site-packages/sqlalchemy/sql/schema.py in _set_parent(self, table)
1289 ))
1290
-> 1291 if self.key in table._columns:
1292 col = table._columns.get(self.key)
1293 if col is not self:
/home/shankar/.local/lib/python3.5/site-packages/sqlalchemy/sql/elements.py in __getattr__(self, key)
736 type(self).__name__,
737 type(self.comparator).__name__,
--> 738 key)
739 )
740
AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute '_columns'
Kindly notice my bug or give me a steps for creating table in MySQL Database. [From Scratch]
)
onage
. – univerio Aug 19 at 4:09