We can obtain MySQL data through the following methods: Use the MySQL command line client to connect to the database and use the SELECT statement to retrieve the data. Connect and query using Python's MySQL Connector/Python library. Connect and execute queries using Java's JDBC.
How to obtain the data in the MySQL database
There are many ways to obtain the data in the MySQL database, as follows Are some of the most commonly used methods:
1. Use the MySQL command line client
<code>mysql -u username -p password -D database_name</code>
username
is your MySQL username and password
is you password, database_name
is the name of the database you want to connect to. SELECT
statement to retrieve data: <code>SELECT column_name(s) FROM table_name WHERE condition;</code>
2. Use Python
Library:
<code class="python">import mysql.connector # 建立数据库连接 connection = mysql.connector.connect( host="localhost", user="username", password="password", database="database_name" ) # 获取游标 cursor = connection.cursor() # 执行查询 cursor.execute("SELECT * FROM table_name") # 获取查询结果 results = cursor.fetchall() # 遍历结果 for row in results: print(row) # 关闭游标和连接 cursor.close() connection.close()</code>
3. Using Java
:
<code class="java">import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class GetMySQLData { public static void main(String[] args) { // 建立数据库连接 Connection connection = DriverManager.getConnection( "jdbc:mysql://localhost/database_name", "username", "password"); // 创建声明 Statement statement = connection.createStatement(); // 执行查询 ResultSet resultSet = statement.executeQuery("SELECT * FROM table_name"); // 遍历结果 while (resultSet.next()) { System.out.println(resultSet.getString("column_name")); } // 关闭声明和连接 statement.close(); connection.close(); } }</code>
The above is the detailed content of How to get data from mysql database. For more information, please follow other related articles on the PHP Chinese website!