android.database.sqlite.SQLiteException:near“20”:语法错误(代码1):,编译时:DELETE FROM ACCIDENT WHERE data = 19/04/2015 20:59

时间:2015-04-21 14:28:02

标签: android mysql sqlite

程序返回此错误:

android.database.sqlite.SQLiteException: near "20": syntax error (code 1): , while compiling: DELETE FROM ACCIDENT WHERE data = 19/04/2015 20:59

字段数据是包含以空格分隔的日期和时间的字符串。

错误是因为条件不能使用由空格分隔的字符串。 数据库中的字段数据包含" 19/04/2015 20:59"

这是代码

package com.ebre.disseny.ajudaenaccidents;

 import android.app.AlertDialog;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.database.sqlite.SQLiteDatabase;
 import android.os.Bundle;
 import android.os.Environment;
 import android.view.View;
 import android.widget.Button;
 import android.widget.CheckBox;
 import android.widget.EditText;

/**
* Created by Ventura on 15/04/2015.
*/
public class VeureDadesAccidentActivity extends ClassePare {

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_veure_dades_accident);
    this.idLayout = R.layout.activity_veure_dades_accident;
    this.idVista = R.id.VistaVeureDades;

    final Bundle bundle = this.getIntent().getExtras();
    this.setListenerDesplaçar();

    //Obtenim la referència als controls de la interfase
    final EditText textData = (EditText) findViewById(R.id.DataAccident);
    final EditText textLloc = (EditText) findViewById(R.id.LlocAccident);
    final EditText textDescripció = (EditText) findViewById(R.id.DescripcióAccident);
    final CheckBox checkContrari = (CheckBox) findViewById(R.id.checkbox);
    final EditText textNom = (EditText) findViewById(R.id.NomCognoms);
    final EditText textDNI = (EditText) findViewById(R.id.DNI);
    final EditText textAdreça = (EditText) findViewById(R.id.Adreça);
    final EditText textTelefon = (EditText) findViewById(R.id.Telèfon);
    final EditText textMatricula = (EditText) findViewById(R.id.MatriculaVehicle);
    final EditText textModel = (EditText) findViewById(R.id.ModelVehicle);
    final EditText textMarca = (EditText) findViewById(R.id.MarcaVehicle);
    final EditText textAsseguradora = (EditText) findViewById(R.id.Asseguradora);
    final EditText textPolissa = (EditText) findViewById(R.id.NumeroPolissa);
    final String check = bundle.getString("check");

    textData.setText(bundle.getString("data"));
    final String data = bundle.getString("data");
    textLloc.setText(bundle.getString("lloc"));
    textDescripció.setText(bundle.getString("descripcio"));

    if (check.equals("true")){
        checkContrari.setChecked(true);
        activar_camps();
    }else {
        checkContrari.setChecked(false);
        desactivar_camps();
    }

    textNom.setText(bundle.getString("nom"));
    textDNI.setText(bundle.getString("dni"));
    textAdreça.setText(bundle.getString("adreça"));
    textTelefon.setText(bundle.getString("telefon"));
    textMarca.setText(bundle.getString("marca"));
    textModel.setText(bundle.getString("model"));
    textMatricula.setText(bundle.getString("matricula"));
    textAsseguradora.setText(bundle.getString("asseguradora"));
    textPolissa.setText(bundle.getString("polissa"));       

     //Implementem el botó de fer veure les fotos de l'accident en cas que n'hi hagin
    final Button veureFotos = (Button) findViewById(R.id.BotoVeureFotos);
    veureFotos.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(), VeureFotos.class);
            intent.putExtras(bundle);
            v.getContext().startActivity(intent);
        //    this.overridePendingTransition(R.anim.left_in, R.anim.left_out);
        }
    });

    final Button eliminarAccident = (Button) findViewById(R.id.BotoEliminar);
    eliminarAccident.setOnClickListener(new View.OnClickListener() {
        public void onClick(final View v) {
            new AlertDialog.Builder(v.getContext())
                    .setTitle("Eliminar Accident?")
                    .setMessage("Estas segur que vols eliminar aquest accident?")
                    .setNegativeButton(android.R.string.no, null)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface arg0, int arg1) {
                            AccesSQLite accesSQLite = new  AccesSQLite(v.getContext(), "DBAjudaEnAccidents", null, 1);
                            final SQLiteDatabase baseDades = accesSQLite.getWritableDatabase();
//   StringBuilder suprimirDades = new StringBuilder();
//   suprimirDades.append("DELETE FROM ACCIDENT WHERE data = ");
//   suprimirDades.append(data);
//   baseDades.delete("ACCIDENT", "", null);
//   baseDades.delete("ASSEGURAT", "data" + "=" + data, null);
                            baseDades.execSQL("DELETE FROM ACCIDENT WHERE data = " + data.toString());
                            baseDades.execSQL("DELETE FROM ASSEGURAT WHERE data = 19/04/2015 20:59");
                            baseDades.close();
                            onBackPressed();
                        }
                    }).create().show();
        }
    });
 }

public void activar_camps(){
    EditText et = (EditText) findViewById(R.id.NomCognoms);
    et.setVisibility(View.VISIBLE);
    et = (EditText) findViewById(R.id.DNI);
    et.setVisibility(View.VISIBLE);
    et = (EditText) findViewById(R.id.Adreça);
    et.setVisibility(View.VISIBLE);
    et = (EditText) findViewById(R.id.Telèfon);
    et.setVisibility(View.VISIBLE);
    et = (EditText) findViewById(R.id.MarcaVehicle);
    et.setVisibility(View.VISIBLE);
    et = (EditText) findViewById(R.id.ModelVehicle);
    et.setVisibility(View.VISIBLE);
    et = (EditText) findViewById(R.id.MatriculaVehicle);
    et.setVisibility(View.VISIBLE);
    et = (EditText) findViewById(R.id.Asseguradora);
    et.setVisibility(View.VISIBLE);
    et = (EditText) findViewById(R.id.NumeroPolissa);
    et.setVisibility(View.VISIBLE);

}

public void desactivar_camps(){
    EditText et = (EditText) findViewById(R.id.NomCognoms);
    et.setVisibility(View.GONE);
    et = (EditText) findViewById(R.id.DNI);
    et.setVisibility(View.GONE);
    et = (EditText) findViewById(R.id.Adreça);
    et.setVisibility(View.GONE);
    et = (EditText) findViewById(R.id.Telèfon);
    et.setVisibility(View.GONE);
    et = (EditText) findViewById(R.id.MarcaVehicle);
    et.setVisibility(View.GONE);
    et = (EditText) findViewById(R.id.ModelVehicle);
    et.setVisibility(View.GONE);
    et = (EditText) findViewById(R.id.MatriculaVehicle);
    et.setVisibility(View.GONE);
    et = (EditText) findViewById(R.id.Asseguradora);
    et.setVisibility(View.GONE);
    et = (EditText) findViewById(R.id.NumeroPolissa);
    et.setVisibility(View.GONE);

}

}

1 个答案:

答案 0 :(得分:1)

SQL语法错误:

baseDades.execSQL("DELETE FROM ASSEGURAT WHERE data = 19/04/2015 20:59");

你没有使用约会,你正在使用数学除法的结果和一些奇怪的未知数字。查询正在执行

... WHERE data = 0.002357... 20:59
                   ^^^---valid number
                             ^^^^^---unknown garbage

尝试

baseDades.execSQL("DELETE FROM ASSEGURAT WHERE data = '19/04/2015 20:59'");
                                                      ^----------------^

代替。请注意额外的' - 引号。

鉴于您有这个错误,表明您很容易受到sql injection attacks的攻击。<​​/ p>