mysql/androidstudio,table 上的列只接受 int,而不是 varchar
mysql/androidstudio, column on table only accepts int, instead of varchar
我试图在 table 中插入行,但是“部门”列只接受整数。
我正在使用这个 Tutorial 作为我的指南。
如果我将第 4 列(部门)的类型更改为 int 并在其上插入 int,一切正常。但我需要字符串。我已经将上述列的类型更改为 VARCHAR,但在 table 上始终显示“0”,即使我插入一个字符串也是如此。
我的Table
这是我的 table 结构
API.php
<?php
require_once '../includes/DbOperation.php';
function isTheseParametersAvailable($params){
$available = true;
$missingparams = "";
foreach($params as $param){
if(!isset($_POST[$param]) || strlen($_POST[$param])<=0){
$available = false;
$missingparams = $missingparams . ", " . $param;
}
}
if(!$available){
$response = array();
$response['error'] = true;
$response['message'] = 'Parameters ' . substr($missingparams, 1, strlen($missingparams)) . ' missing';
echo json_encode($response);
die();
}
}
$response = array();
if(isset($_GET['apicall'])){
switch($_GET['apicall']){
//the CREATE operation
//if the api call value is 'createhero'
//we will create a record in the database
case 'createemployee':
//first check the parameters required for this request are available or not
isTheseParametersAvailable(array('firstname','lastname','department','image'));
//creating a new dboperation object
$db = new DbOperation();
//creating a new record in the database
$result = $db->createEmployee(
$_POST['firstname'],
$_POST['lastname'],
$_POST['department'],
$_POST['image']
);
//if the record is created adding success to response
if($result){
//record is created means there is no error
$response['error'] = false;
//in message we have a success message
$response['message'] = 'Employee addedd successfully';
//and we are getting all the heroes from the database in the response
// $response['employees'] = $db->getHeroes();
}else{
//if record is not added that means there is an error
$response['error'] = true;
//and we have the error message
$response['message'] = 'Some error occurred please try again';
}
break;
Dboperation.php
class DbOperation{
private $con;
function __construct()
{
//Getting the DbConnect.php file
require_once dirname(__FILE__) . '/DbConnect.php';
//Creating a DbConnect object to connect to the database
$db = new DbConnect();
//Initializing our connection link of this class
//by calling the method connect of DbConnect class
$this->con = $db->connect();
}
/*
* The create operation
* When this method is called a new record is created in the database
*/
function createEmployee($firstname, $lastname, $department, $image){
$stmt = $this->con->prepare("INSERT INTO tbl_users (firstname, lastname, department, image) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssis", $firstname, $lastname, $department, $image);
if($stmt->execute())
return true;
return false;
}
Android代码
private void createEmployee1() {
String firstname = EtFirstname.getText().toString().trim();
String lastname = EtLastname.getText().toString().trim();
String department = EtDepartment.getText().toString().trim();
String image = EtImage.getText().toString().trim();
HashMap<String,String> params = new HashMap<>();
params.put("firstname", firstname);
params.put("lastname", lastname);
params.put("department", department);
params.put("image", image);
PerformNetworkRequest request = new PerformNetworkRequest(Api.URL_CREATE_EMPLOYEE, params, CODE_POST_REQUEST);
request.execute();
}
private class PerformNetworkRequest extends AsyncTask<Void, Void , String> {
String url;
HashMap<String, String> params;
int requestCode;
PerformNetworkRequest(String url, HashMap<String, String> params, int requestCode) {
this.url = url;
this.params = params;
this.requestCode = requestCode;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressBar.setVisibility(View.GONE);
try {
JSONObject object = new JSONObject(s);
if (!object.getBoolean("error")) {
Toast.makeText(getApplicationContext(), object.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids) {
RequestHandler requestHandler = new RequestHandler();
if (requestCode == CODE_POST_REQUEST)
return requestHandler.sendPostRequest(url, params);
if (requestCode == CODE_GET_REQUEST)
return requestHandler.sendGetRequest(url);
return null;
}
}
Api.java
public class Api {
private static final String ROOT_URL = "http://XXX.XXX.X.XX:8080/DTR/v1/Api.php?apicall=";
public static final String URL_CREATE_EMPLOYEE = ROOT_URL + "createemployee";
public static final String URL_READ_EMPLOYEE = ROOT_URL + "getemployees";
public static final String URL_UPDATE_EMPLOYEE = ROOT_URL + "updateemployee";
public static final String URL_DELETE_EMPLOYEE = ROOT_URL + "deleteemployee&id=";
}
$stmt->bind_param("ssis", $firstname, $lastname, $department, $image);
对于 ssis
中的 i
,您在此处指定的第三个插入值应该是一个整数,因此它被视为整数,包括将无效值转换为 0。
我试图在 table 中插入行,但是“部门”列只接受整数。 我正在使用这个 Tutorial 作为我的指南。
如果我将第 4 列(部门)的类型更改为 int 并在其上插入 int,一切正常。但我需要字符串。我已经将上述列的类型更改为 VARCHAR,但在 table 上始终显示“0”,即使我插入一个字符串也是如此。
我的Table
这是我的 table 结构
API.php
<?php
require_once '../includes/DbOperation.php';
function isTheseParametersAvailable($params){
$available = true;
$missingparams = "";
foreach($params as $param){
if(!isset($_POST[$param]) || strlen($_POST[$param])<=0){
$available = false;
$missingparams = $missingparams . ", " . $param;
}
}
if(!$available){
$response = array();
$response['error'] = true;
$response['message'] = 'Parameters ' . substr($missingparams, 1, strlen($missingparams)) . ' missing';
echo json_encode($response);
die();
}
}
$response = array();
if(isset($_GET['apicall'])){
switch($_GET['apicall']){
//the CREATE operation
//if the api call value is 'createhero'
//we will create a record in the database
case 'createemployee':
//first check the parameters required for this request are available or not
isTheseParametersAvailable(array('firstname','lastname','department','image'));
//creating a new dboperation object
$db = new DbOperation();
//creating a new record in the database
$result = $db->createEmployee(
$_POST['firstname'],
$_POST['lastname'],
$_POST['department'],
$_POST['image']
);
//if the record is created adding success to response
if($result){
//record is created means there is no error
$response['error'] = false;
//in message we have a success message
$response['message'] = 'Employee addedd successfully';
//and we are getting all the heroes from the database in the response
// $response['employees'] = $db->getHeroes();
}else{
//if record is not added that means there is an error
$response['error'] = true;
//and we have the error message
$response['message'] = 'Some error occurred please try again';
}
break;
Dboperation.php
class DbOperation{
private $con;
function __construct()
{
//Getting the DbConnect.php file
require_once dirname(__FILE__) . '/DbConnect.php';
//Creating a DbConnect object to connect to the database
$db = new DbConnect();
//Initializing our connection link of this class
//by calling the method connect of DbConnect class
$this->con = $db->connect();
}
/*
* The create operation
* When this method is called a new record is created in the database
*/
function createEmployee($firstname, $lastname, $department, $image){
$stmt = $this->con->prepare("INSERT INTO tbl_users (firstname, lastname, department, image) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssis", $firstname, $lastname, $department, $image);
if($stmt->execute())
return true;
return false;
}
Android代码
private void createEmployee1() {
String firstname = EtFirstname.getText().toString().trim();
String lastname = EtLastname.getText().toString().trim();
String department = EtDepartment.getText().toString().trim();
String image = EtImage.getText().toString().trim();
HashMap<String,String> params = new HashMap<>();
params.put("firstname", firstname);
params.put("lastname", lastname);
params.put("department", department);
params.put("image", image);
PerformNetworkRequest request = new PerformNetworkRequest(Api.URL_CREATE_EMPLOYEE, params, CODE_POST_REQUEST);
request.execute();
}
private class PerformNetworkRequest extends AsyncTask<Void, Void , String> {
String url;
HashMap<String, String> params;
int requestCode;
PerformNetworkRequest(String url, HashMap<String, String> params, int requestCode) {
this.url = url;
this.params = params;
this.requestCode = requestCode;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressBar.setVisibility(View.GONE);
try {
JSONObject object = new JSONObject(s);
if (!object.getBoolean("error")) {
Toast.makeText(getApplicationContext(), object.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids) {
RequestHandler requestHandler = new RequestHandler();
if (requestCode == CODE_POST_REQUEST)
return requestHandler.sendPostRequest(url, params);
if (requestCode == CODE_GET_REQUEST)
return requestHandler.sendGetRequest(url);
return null;
}
}
Api.java
public class Api {
private static final String ROOT_URL = "http://XXX.XXX.X.XX:8080/DTR/v1/Api.php?apicall=";
public static final String URL_CREATE_EMPLOYEE = ROOT_URL + "createemployee";
public static final String URL_READ_EMPLOYEE = ROOT_URL + "getemployees";
public static final String URL_UPDATE_EMPLOYEE = ROOT_URL + "updateemployee";
public static final String URL_DELETE_EMPLOYEE = ROOT_URL + "deleteemployee&id=";
}
$stmt->bind_param("ssis", $firstname, $lastname, $department, $image);
对于 ssis
中的 i
,您在此处指定的第三个插入值应该是一个整数,因此它被视为整数,包括将无效值转换为 0。