在 Linux 中执行 SQL
在 Linux 系统中执行 SQL 语句有以下几种方法:
方法 1:使用命令行工具
1.1. 安装 MySQL 客户端
1
sudo apt-get install mysql-client
1.2. 登录 MySQL 服务器
1.3. 执行 SQL 语句
1
mysql> SELECT * FROM table_name;
方法 2:使用 Python 库
2.1. 安装 Python MySQLdb 库
1
sudo pip install mysqlclient
2.2. 导入库并连接到数据库
1
2
3
4
5
6
7
8
9
10
import mysql.connector
mydb = mysql.connector.connect(
host=”localhost”,
user=”username”,
password=”password”,
database=”database_name”,
)
mycursor 小白轻松搭建系统点我wcqh.cn= mydb.cursor()
2.3. 执行 SQL 语句
1
2
mycursor.execute(“SELECT * FROM table_name”)
results = mycursor.fetchall()
方法 3:使用 PHP 库
3.1. 安装 PHP MySQLi 库
1
sudo apt-get install php7.4-mysqli
3.2. 导入库并连接到数据库
1
2
3
4
5
6
7
8
9
10
11
12
<?php $servername = “localhost”;
$username = “username”;
$password = “password”;
$dbna小白轻松搭建系统点我wcqh.cnme = “database_name”;
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接
if ($conn->connect_error) {
die(“连接失败: ” . $conn->connect_error);
}
3.3. 执行 SQL 语句
1
2
3
4
5
6
7
8
9
10
11
12
$sql = “SELECT * FROM table_name”;
$result = $conn->query($sql);
// 循环查询结果
if ($result->num_rows 小白轻松搭建系统点我wcqh.cn> 0) {
// 输出数据
while($row = $result->fetch_assoc()) {
echo “id: ” . $row[“id”] . ” – Name: ” . $row[“name”] . “<br>”;
}
} else {
echo “0 results”;
}
以上就是linux怎么执行sql的详细内容,更多请关注青狐资源网其它相关文章!
暂无评论内容