Program to insert record into a table with Mysql.

Here, we have a basic program example to insert data in the table using Mysql in Python and Java language.

Code to insert data into table in Python language

import mysql.connector
con = mysql.connector.connect(host="localhost",user="root",passwd="root", database="coder")
cur = con.cursor()
 
#accept user input to store
bookname = input("Enter Book Name")
author = input("Enter Author")
 
#execute insert query to store the data
cur.execute("insert into Book values('{}','{}')".format(bookname,author))
print("Record Inserted")
con.commit()    #commit means save the record
con.close()

Code to insert data into table in Java language

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class InsertDb {
   public static void main(String[] args) {
      Connection con = null;
      Statement st = null;
      try {
         con = DriverManager.getConnection("jdbc :mysql ://localhost :3306/TestDb" + "useSSL=false", "root", "vsit@123");
         st = con.createStatement();
         String accessDatabase = "insert into DemoTable(Id,Name,CountryName,Age)" + " values(12,'rahul','IND',28) ";
         int result = st.executeUpdate(accessDatabase);
         if (result > 0) {
            System.out.println("Record Inserted! Check your table now!");
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}