9951 explained code solutions for 126 technologies


python-mysqlHow can I use Python and MySQL to create an example project on GitHub?


To create an example project on GitHub using Python and MySQL, you can follow the steps listed below:

  1. Install the necessary packages for Python and MySQL.
  2. Create a GitHub repository for the project.
  3. Connect Python to MySQL using the mysql.connector library.
  4. Create a MySQL database and tables with the necessary columns.
  5. Write a Python script to insert data into the database.

    import mysql.connector
    
    mydb = mysql.connector.connect(
     host="localhost",
     user="yourusername",
     passwd="yourpassword"
    )
    
    mycursor = mydb.cursor()
    
    sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
    val = ("John", "Highway 21")
    mycursor.execute(sql, val)
    
    mydb.commit()
  6. Write a Python script to query the database and print the results.

    import mysql.connector
    
    mydb = mysql.connector.connect(
     host="localhost",
     user="yourusername",
     passwd="yourpassword"
    )
    
    mycursor = mydb.cursor()
    
    mycursor.execute("SELECT * FROM customers")
    
    myresult = mycursor.fetchall()
    
    for x in myresult:
     print(x)

    Output:

    ('John', 'Highway 21')
  7. Push the code to the GitHub repository.

Helpful links

Edit this code on GitHub