插入脚本返回成功但没有插入数据库
Insert script returning success but nothing inserted to database
我有一种方法可以将值插入外部数据库。我有一个团队成员名称数组,它与团队成员团队 ID(这是 team_members table 中的外键)一起循环并一个一个发送到数据库。
下面是我简单的数据库设计:
CREATE TABLE team (
team_id int(11) NOT NULL AUTO_INCREMENT,
unique_id varchar(23) NOT NULL,
teamName varchar(50) NOT NULL,
PRIMARY KEY (team_id)
);
CREATE TABLE team_member (
team_member_id int(11) NOT NULL AUTO_INCREMENT,
unique_id varchar(23) NOT NULL,
fullName varchar(50) NOT NULL,
team_id int (11) NOT NULL,
PRIMARY KEY (team_member_id),
FOREIGN KEY (`team_id`)
REFERENCES `scratchcard`.`team` (`team_id`)
);
以下是我用来实现此目的的方法:
private void addTeamMember(final List teamMemberArray,final String team_id) {
//helps with debugging regarding post requests
Gson gson = new GsonBuilder()
.setLenient()
.create();
//Retrofit is a REST Client for Android and Java by Square.
//It makes it relatively easy to retrieve and upload JSON (or other structured data) via a REST based webservice
Retrofit retrofit = new Retrofit.Builder()
//directing to the localhost which is defined in the Constants Class as BASE_URL
.baseUrl(Constants.BASE_URL)
//Add converter factory for serialization and deserialization of objects.
//Gson passed as a parameter to help with debug
.addConverterFactory(GsonConverterFactory.create(gson))
//Create the Retrofit instance using the configured values.
.build();
//The Retrofit class generates an implementation of the RequestInterface interface.
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
for (Object x : teamMemberArray) {
//create new Team object
TeamMember teamMember = new TeamMember();
//setter
teamMember.setFullName(String.valueOf(x));
teamMember.setTeam_id(team_id);
Toast.makeText(getActivity(), teamMember.getFullName(),
Toast.LENGTH_LONG).show();
//create new server object
final ServerRequest request = new ServerRequest();
//make a request to set the operation to Team_Member
request.setOperation(Constants.Team_Member);
//set values entered for the new teamMember to be sent to the server
request.setTeamMember(teamMember);
Call<ServerResponse> response = requestInterface.operation(request);
/**
* Enqueue is used to Asynchronously send the request and notify callback of its response or if an error occurred
* talking to the server, creating the request, or processing the response.
*/
response.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {
ServerResponse resp = response.body();
/*Snackbars provide lightweight feedback about an operation. They show a brief message at the
bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other
elements on screen and only one can be displayed at a time.
*/
Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
if (resp.getResult().equals(Constants.SUCCESS)) {
SharedPreferences.Editor editor = pref.edit();
Log.d("TEST VALUE", "getTeamMemberName() = " + response.body().getTeamMember().getFullName() );
Log.d("TEST VALUE", "getTeamMemberUniqueID() = " + response.body().getTeamMember().getUnique_id());
Log.d("TEST VALUE", "getTeamMemberTeamID() = " + response.body().getTeamMember().getTeamID());
editor.putString(Constants.FULL_NAME, resp.getTeamMember().getFullName());
editor.putString(Constants.UNIQUE_ID, resp.getTeamMember().getUnique_id());
editor.putString(Constants.TEAM_ID, resp.getTeamMember().getTeamID());
editor.apply();
goToQuestions();
}
progress.setVisibility(View.INVISIBLE);
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
progress.setVisibility(View.INVISIBLE);
Log.d(Constants.TAG, "failed" + t);
Snackbar.make(getView(), t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();
}
});
}
}
因为我一次一个地插入这些值,所以我有以下简单的插入脚本(使用 PDO)
public function insertTeamMember($fullName, $team_id){
$unique_id = uniqid('', true);
$sql = 'INSERT INTO team_member SET unique_id =:unique_id,fullName =:fullName, team_id =:team_id';
$query = $this ->conn ->prepare($sql);
$query->execute(array('unique_id' => $unique_id, ':fullName' => $fullName, ':team_id' => $team_id));
$data = $query -> fetchObject();
if ($query) {
$teamMember["fullName"] = $fullName;
$teamMember["unique_id"] = $unique_id;
$teamMember["team_id"] = $team_id;
return $teamMember;
} else {
return false;
}
}
我使用 response.body 的日志输出确认这些值正确地到达了我的插入脚本。
08-11 11:07:49.750 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberName() = 1
08-11 11:07:49.750 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberUniqueID() = 598d90061b7de5.56364485
08-11 11:07:49.750 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberTeamID() = 598d8ffd0b9969.94188022
08-11 11:07:49.754 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberName() = 2
08-11 11:07:49.754 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberUniqueID() = 598d90061d4347.93277262
08-11 11:07:49.754 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberTeamID() = 598d8ffd0b9969.94188022
08-11 11:07:49.759 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberName() = 3
08-11 11:07:49.759 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberUniqueID() = 598d90061d8bb2.78397401
08-11 11:07:49.759 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberTeamID() = 598d8ffd0b9969.94188022
我的回调确认数据插入成功(我正在使用 MYSQL),如我的日志输出所示,但数据实际上并未插入我的数据库。
我相信我的插入脚本一定有问题,但我不确定解决方案是什么。我曾尝试使用 Postman 来帮助我,但这并没有指出为什么我被告知成功但实际上没有让我的数据插入的方向。
你的插入查询是错误的。它应该看起来像
$sql = 'INSERT INTO team_member (`unique_id`, `fullName`, `team_id`) VALUES (:unique_id, :fullName, :team_id)';
删除下一行。它用于 "SELECT" 获取记录。您 运行 插入查询。
$data = $query -> fetchObject();
在同一位置添加下一行并获取最后插入的 ID。
$insert_id = $this ->conn->lastInsertId();//find last inserted id.
if($insert_id > 0){
$teamMember["fullName"] = $fullName;
$teamMember["unique_id"] = $unique_id;
$teamMember["team_id"] = $team_id;
return $teamMember;
}
我有一种方法可以将值插入外部数据库。我有一个团队成员名称数组,它与团队成员团队 ID(这是 team_members table 中的外键)一起循环并一个一个发送到数据库。
下面是我简单的数据库设计:
CREATE TABLE team (
team_id int(11) NOT NULL AUTO_INCREMENT,
unique_id varchar(23) NOT NULL,
teamName varchar(50) NOT NULL,
PRIMARY KEY (team_id)
);
CREATE TABLE team_member (
team_member_id int(11) NOT NULL AUTO_INCREMENT,
unique_id varchar(23) NOT NULL,
fullName varchar(50) NOT NULL,
team_id int (11) NOT NULL,
PRIMARY KEY (team_member_id),
FOREIGN KEY (`team_id`)
REFERENCES `scratchcard`.`team` (`team_id`)
);
以下是我用来实现此目的的方法:
private void addTeamMember(final List teamMemberArray,final String team_id) {
//helps with debugging regarding post requests
Gson gson = new GsonBuilder()
.setLenient()
.create();
//Retrofit is a REST Client for Android and Java by Square.
//It makes it relatively easy to retrieve and upload JSON (or other structured data) via a REST based webservice
Retrofit retrofit = new Retrofit.Builder()
//directing to the localhost which is defined in the Constants Class as BASE_URL
.baseUrl(Constants.BASE_URL)
//Add converter factory for serialization and deserialization of objects.
//Gson passed as a parameter to help with debug
.addConverterFactory(GsonConverterFactory.create(gson))
//Create the Retrofit instance using the configured values.
.build();
//The Retrofit class generates an implementation of the RequestInterface interface.
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
for (Object x : teamMemberArray) {
//create new Team object
TeamMember teamMember = new TeamMember();
//setter
teamMember.setFullName(String.valueOf(x));
teamMember.setTeam_id(team_id);
Toast.makeText(getActivity(), teamMember.getFullName(),
Toast.LENGTH_LONG).show();
//create new server object
final ServerRequest request = new ServerRequest();
//make a request to set the operation to Team_Member
request.setOperation(Constants.Team_Member);
//set values entered for the new teamMember to be sent to the server
request.setTeamMember(teamMember);
Call<ServerResponse> response = requestInterface.operation(request);
/**
* Enqueue is used to Asynchronously send the request and notify callback of its response or if an error occurred
* talking to the server, creating the request, or processing the response.
*/
response.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {
ServerResponse resp = response.body();
/*Snackbars provide lightweight feedback about an operation. They show a brief message at the
bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other
elements on screen and only one can be displayed at a time.
*/
Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
if (resp.getResult().equals(Constants.SUCCESS)) {
SharedPreferences.Editor editor = pref.edit();
Log.d("TEST VALUE", "getTeamMemberName() = " + response.body().getTeamMember().getFullName() );
Log.d("TEST VALUE", "getTeamMemberUniqueID() = " + response.body().getTeamMember().getUnique_id());
Log.d("TEST VALUE", "getTeamMemberTeamID() = " + response.body().getTeamMember().getTeamID());
editor.putString(Constants.FULL_NAME, resp.getTeamMember().getFullName());
editor.putString(Constants.UNIQUE_ID, resp.getTeamMember().getUnique_id());
editor.putString(Constants.TEAM_ID, resp.getTeamMember().getTeamID());
editor.apply();
goToQuestions();
}
progress.setVisibility(View.INVISIBLE);
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
progress.setVisibility(View.INVISIBLE);
Log.d(Constants.TAG, "failed" + t);
Snackbar.make(getView(), t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();
}
});
}
}
因为我一次一个地插入这些值,所以我有以下简单的插入脚本(使用 PDO)
public function insertTeamMember($fullName, $team_id){
$unique_id = uniqid('', true);
$sql = 'INSERT INTO team_member SET unique_id =:unique_id,fullName =:fullName, team_id =:team_id';
$query = $this ->conn ->prepare($sql);
$query->execute(array('unique_id' => $unique_id, ':fullName' => $fullName, ':team_id' => $team_id));
$data = $query -> fetchObject();
if ($query) {
$teamMember["fullName"] = $fullName;
$teamMember["unique_id"] = $unique_id;
$teamMember["team_id"] = $team_id;
return $teamMember;
} else {
return false;
}
}
我使用 response.body 的日志输出确认这些值正确地到达了我的插入脚本。
08-11 11:07:49.750 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberName() = 1
08-11 11:07:49.750 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberUniqueID() = 598d90061b7de5.56364485
08-11 11:07:49.750 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberTeamID() = 598d8ffd0b9969.94188022
08-11 11:07:49.754 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberName() = 2
08-11 11:07:49.754 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberUniqueID() = 598d90061d4347.93277262
08-11 11:07:49.754 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberTeamID() = 598d8ffd0b9969.94188022
08-11 11:07:49.759 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberName() = 3
08-11 11:07:49.759 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberUniqueID() = 598d90061d8bb2.78397401
08-11 11:07:49.759 3884-3884/com.test.practise D/TEST VALUE: getTeamMemberTeamID() = 598d8ffd0b9969.94188022
我的回调确认数据插入成功(我正在使用 MYSQL),如我的日志输出所示,但数据实际上并未插入我的数据库。
我相信我的插入脚本一定有问题,但我不确定解决方案是什么。我曾尝试使用 Postman 来帮助我,但这并没有指出为什么我被告知成功但实际上没有让我的数据插入的方向。
你的插入查询是错误的。它应该看起来像
$sql = 'INSERT INTO team_member (`unique_id`, `fullName`, `team_id`) VALUES (:unique_id, :fullName, :team_id)';
删除下一行。它用于 "SELECT" 获取记录。您 运行 插入查询。
$data = $query -> fetchObject();
在同一位置添加下一行并获取最后插入的 ID。
$insert_id = $this ->conn->lastInsertId();//find last inserted id.
if($insert_id > 0){
$teamMember["fullName"] = $fullName;
$teamMember["unique_id"] = $unique_id;
$teamMember["team_id"] = $team_id;
return $teamMember;
}