In all the codes below replace userid, password and database_name with your MySQL login and database details.
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:password@localhost/database_name")
We are using student table. Use the code to create your sample student table.
Creating table ( We used escape char \ for line continuation )
try:
q="CREATE TABLE IF NOT EXISTS `student` (\
`id` int(2) NOT NULL AUTO_INCREMENT,\
`name` varchar(50) CHARACTER SET utf8 NOT NULL DEFAULT '',\
`class` varchar(10) CHARACTER SET utf8 NOT NULL DEFAULT '',\
`mark` int(3) NOT NULL DEFAULT '0',\
`sex` varchar(6) CHARACTER SET utf8 NOT NULL DEFAULT 'male',\
UNIQUE KEY `id` (`id`)\
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;"
my_conn.execute(q)
except SQLAlchemyError as e:
error = str(e.__dict__['orig'])
print(error)
Table is created, we can check the structure of the table by using DESCRIBE.
r_set=my_conn.execute("DESCRIBE student");
for row in r_set:
print(row)