";require "../templates/head_jq_bs4.php";echo "
";$img_path="..";require "templates/top_bs4.php"; echo "from sqlalchemy import create_engine,textmy_conn = create_engine("mysql+mysqldb://id:pw@localhost/my_db")my_conn = my_conn.connect() # connection object or connection stringWe are using student table. Use the code to create your sample student table. from sqlalchemy import create_engine,textmy_conn = create_engine("mysql+mysqldb://userid:password@localhost/database_name")my_conn = my_conn.connect() # connection object or connection stringid=my_conn.execute(text("INSERT INTO `database_name`.`student` (`name` ,`class` ,`mark` ,`sex`) \ VALUES ('King1', 'Five', '45', 'male')"))print("Row Added = ",id.rowcount)Output Row Added = 1In our student table we have used one auto incremented id column. We will collect this id after adding record to database. from sqlalchemy import create_engine,textmy_conn = create_engine("mysql+mysqldb://userid:password@localhost/database_name")my_conn = my_conn.connect() # connection object or connection stringid=my_conn.execute(text("INSERT INTO `database_name`.`student` (`name` ,`class` ,`mark` ,`sex`) \ VALUES ('King1', 'Five', '45', 'male')"))print("ID of Row Added = ",id.lastrowid)OutputID of Row Added = 46from sqlalchemy import create_engine, textmy_conn = create_engine("mysql+mysqldb://root:test@localhost/my_tutorial")my_conn = my_conn.connect()query = "INSERT INTO `student` (`name` ,`class` ,`mark` ,`gender`) \ VALUES (:name, :class, :mark, :gender)"my_data = { "name": 'King Li', "class": 'Four', "mark": 58, "gender": 'Female'}id = my_conn.execute(text(query), my_data) # insert dataprint("ID of Row Added = ", id.lastrowid)OutputID of Row Added = 45from sqlalchemy import create_engine, textmy_conn = create_engine("mysql+mysqldb://id:pw@localhost/my_db")my_conn = my_conn.connect()try: query = "INSERT INTO `student` (`name`, `class`, `mark`, `gender`) \ VALUES (:name, :class, :mark, :gender)" my_data = [ {"name": "King", "class": "Five", "mark": 45, "gender": "male"}, {"name": "Queen", "class": "Four", "mark": 44, "gender": "Female"}, {"name": "Jack", "class": "Three", "mark": 42, "gender": "male"} ] id = my_conn.execute(text(query), my_data) print("Rows Added = ", id.rowcount)except: print("Database error")Output Rows Added = 3 from sqlalchemy import create_engine,textfrom sqlalchemy.exc import SQLAlchemyErrormy_conn = create_engine("mysql+mysqldb://root:test@localhost/my_tutorial")my_conn=my_conn.connect()try: query="INSERT INTO `student` (`name` ,`class` ,`mark` ,`gender`) VALUES (:name, :class, :mark, :gender)" my_data = [ {"name": "King", "class": "Five", "mark": 45, "gender": "male"}, {"name": "Queen", "class": "Four", "mark": 44, "gender": "Female"}, {"name": "Jack", "class": "Three", "mark": 42, "gender": "male"} ] id=my_conn.execute(text(query),my_data) print("Rows Added = ",id.rowcount)except SQLAlchemyError as e: error = str(e.__dict__.get('orig', e)) # get error message print(error)
r_set=my_conn.execute(text("SHOW TABLES"));for row in r_set: print(row)r_set=my_conn.execute(text("SELECT * FROM student"));for row in r_set: print(row)