mysql - Flutter:与mysql服务器的连接不起作用
问题描述
我正在尝试将颤振与 mysql 数据库连接以进行 sql crud 操作,但它不起作用。这是我得到的错误
SocketException: OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 45896
这是我的 API 请求代码
import 'package:http/http.dart' as http;
import 'dart:async';
class Services{
static const ROOT = 'http://127.0.0.1/practice/service.php';
static const _CREATE_TABLE_ACTION = 'CREATE_TABLE';
static const _ADD_EMP_ACTION = 'ADD_EMP';
//method to create table employees
static Future<String> createTable() async{
try{
//add parameters to pass to the request
var map = Map<String, dynamic>();
map['action'] = _CREATE_TABLE_ACTION;
final response = await http.post(ROOT,body:map);
print(response.body);
//print('CREATE TABLE RESPONSE: ${response.body}');
return response.body;
}
catch (e){
print(e);
return "error";
}
}
//method to add an employee to database
static Future<String> addEmployee(String firstName, String lastName) async{
try{
//add parameters to pass to the request
var map = Map<String, dynamic>();
map['action'] = _ADD_EMP_ACTION;
map['first_name'] = firstName;
map['last_name'] = lastName;
final response = await http.post(ROOT,body:map);
print('Insert response: ${response.body}');
if(200 == response.statusCode){
return response.body;
}
else{
return "error";
}
}
catch (e){
print(e);
return "error";
}
}
}
这是调用创建表的代码的函数
_createTable(){
_showProgress("Creating Table");
Services.createTable().then((result){
if('success' == result){
//show a snackbar
_showSnackBar(context,result);
print("table created");
}
else{
print("table not created");
}
});
}
这是将员工添加到数据库的函数调用代码
_addEmployee(){
if(_firstNameController.text.isEmpty || _lastNameController.text.isEmpty){
print("Empty fields");
return;
}
_showProgress("Adding Employee..");
Services.addEmployee(_firstNameController.text, _lastNameController.text).then((result){
if('success' == result){
//
print("success");
}
else{
print("error");
}
_clearValues();
});
}
这是一个 php 文件,其中包含对从 api 收到的请求的所有响应并执行请求的 crud 操作
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$table = "employee";
//we will get actions from the app to do operations in the database...
$action = $_POST['action'];
$conn = mysqli_connect($servername,$username,$password,$dbname);
if(!$conn){
//die("Connection Failed: ");
}
if(!mysqli_select_db($conn,"practice")){
//echo "db not selected";
}
if("CREATE_TABLE" == $action){
$sql = "CREATE TABLE IF NOT EXISTS ".$table."(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL
)";
if(mysqli_query($conn,$sql)){
//echo "success";
}
else{
//echo "failed";
}
mysqli_close($conn);
return;
}
//add an employee
if("ADD_EMP" == $action){
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$sql = "INSERT INTO $table (first_name, last_name) VALUES ('$first_name','$last_name')";
$result = mysqli_query($conn,$sql);
//echo "success";
mysqli_close($conn);
}
?>
解决方案
static const ROOT = 'http://127.0.0.1/practice/service.php';
127.0.0.1 是模拟器或设备本身的 ip,它是环回地址。实际ip应该不同
推荐阅读
- ios - 尝试在我的 iPhone 上运行应用程序时,Xcode 错误达到最大应用程序限制
- winapi - winapi 如何在透明窗口背景上绘制不透明文本?
- android - 如何为 Android 设备获得最准确的高度
- python - 在所有值都相同的熊猫中删除行
- ssl - 使用 Tasmota 的 Shelly 1PM 上的 MQTT 错误 BR_ERR_BAD_VERSION
- c# - 从文本文件中读取字母数字数据
- r - dplyr在r中的多列分组?
- python - 使用 Python 比较 postgres 表中两列中的字符串
- c - 为什么 getnameinfo() 检索重复的地址?
- authentication - 向第三方证明 OAuth 身份验证