python
import MySQLdb
conn = MySQLdb.connect(host="localhost", user="username", passwd="password", db="database")
# ...
conn.close()
python
import MySQLdb
conn = MySQLdb.connect(host="localhost", user="username", passwd="password", db="database")
cursor = conn.cursor()
cursor.execute("INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2')")
conn.commit()
cursor.execute("SELECT * FROM table_name")
results = cursor.fetchall()
for row in results:
print(row)
cursor.close()
conn.close()
python
import MySQLdb
try:
conn = MySQLdb.connect(host="localhost", user="username", passwd="password", db="database")
# ...
conn.close()
except MySQLdb.Error as e:
print(f"Error connecting to the database: {e}")