MySQL database, insert record into table
import
mysql.connector
db_connection
=
mysql.connector.connect(
host="localhost",
user="root",
passwd="topSecret",
database="joe"
)
my_database
=
db_connection.cursor()
sql_statement
=
"SELECT * FROM test"
my_database.execute(sql_statement)
output
=
my_database.fetchall()
for
x
in
output:
print(x)
MySQL database, insert record into table
import
mysql.connector
try:
connection
=
mysql.connector.connect(host='localhost',
database='joe',
user='root',
password='topSecret')
mySql_insert_query
=
"""INSERT
INTO test (idtest, company) VALUES (4, "GreenGrocer"); """
cursor
=
connection.cursor()
cursor.execute(mySql_insert_query)
connection.commit()
print(cursor.rowcount, "Record
inserted successfully into Laptop table")
cursor.close()
except
mysql.connector.Error
as
error:
print("Failed to
insert record into Laptop table
{}".format(error))
finally:
if
connection.is_connected():
connection.close()
print("MySQL connection is closed")