我必须从sqlite表中检索所有行,然后通过PHP Webservice将它们插入mysql,但仅插入了第一行。 如何在Android中的Mysql表中同时插入多行?
代码以检索sqlite数据
public String getName(){
SQLiteDatabase db=getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT NOMBRE FROM BEBIDAS",null);
if(cursor.moveToFirst()){
return cursor.getString(0);
}
return "";
}
public int getPrecio(){
SQLiteDatabase db=getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT PRECIO FROM BEBIDAS",null);
if(cursor.moveToFirst()){
return cursor.getInt(0);
}
return 0;
}
public int getCantidad(){
SQLiteDatabase db=getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT CANTIDAD FROM BEBIDAS",null);
if(cursor.moveToFirst()){
return cursor.getInt(0);
}
return 0;
}
Volley请求将详细信息传递给Web服务
private void insertData(String URL){
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response.isEmpty()){
Intent confirm = new Intent(Pagos_Lugares.this, elegirPrefer.class);
startActivity(confirm);
}else{
Toast.makeText(Pagos_Lugares.this,"Error",Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String,String> getParams() throws AuthFailureError {
CafeteriaDB cafe= new CafeteriaDB(getApplicationContext());
Map<String,String> parametros = new HashMap<String,String>();
parametros.put("nombre",cafe.getName());
parametros.put("precio",String.valueOf(cafe.getPrecio()));
parametros.put("cantidad",String.valueOf(cafe.getCantidad()));
return parametros;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
网络服务-php代码在下面给出
<?php
include 'conexion.php';
$nombre=$_POST['nombre'];
$precio=$_POST['precio'];
$cantidad=$_POST['cantidad'];
$consulta="INSERT INTO ordenes_bebidas VALUES('".$nombre."','".$precio."','".$cantidad."')";
mysqli_query($conexion,$consulta) or die (mysqli_error());
mysqli_close($conexion);
?>
谢谢