Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.database.sqlite.SQLiteException: table X has no column named Y: , while compiling: INSERT INTO

this is my error in console :

11-29 19:06:50.295: E/AndroidRuntime(333): android.database.sqlite.SQLiteException: table usuarios has no column named email: , while compiling: INSERT INTO usuarios(username, organizacion, email) VALUES(?, ?, ?);

This is the mainActivity, with a button that goes to an Activity for create a 'Perfil' (User)[btCrearPerfil] ... and one to see the listView with them [btEditarPerfil]...

public class MainActivity extends Activity implements OnClickListener {

    public static ArrayList<Perfil> lstPerfiles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lstPerfiles = new ArrayList<Perfil>();

        Button btCrearPerfil = (Button) findViewById(R.id.btCrearPerfil);
        btCrearPerfil.setOnClickListener(this);
        Button btEditarPerfil = (Button) findViewById(R.id.btEditarPerfil);
        btEditarPerfil.setOnClickListener(this);

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        Intent i;

        switch(v.getId()) {
            case R.id.btCrearPerfil:

                i = new Intent(MainActivity.this, CrearPerfil.class);
                startActivity(i);

                break;

            case R.id.btEditarPerfil:

                i = new Intent(MainActivity.this, ListaPerfiles.class);
                startActivity(i);

                break;

            default: break;
        }
    }

}

This is the creator of Perfil , entered by btCrearPerfil :

public class CrearPerfil extends Activity implements OnClickListener {

private Database datos;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crear_perfil);

    datos = new Database(this);

    Button btGuardarPerfil = (Button) findViewById(R.id.btGuardarPerfil);
    btGuardarPerfil.setOnClickListener(this);
    Button btCancelar = (Button) findViewById(R.id.btCancelarPerfil);
    btCancelar.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.crear_perfil, menu);
    return true;
}

@Override
public void onClick(View v) {

    Intent i;

    switch (v.getId()){

        case R.id.btGuardarPerfil:



            EditText eNombre = (EditText) findViewById(R.id.txtUsername);
            EditText eOrganizacion = (EditText) findViewById(R.id.txtOrganizacion);
            EditText eCorreo = (EditText) findViewById(R.id.txtCorreo);
            CheckBox eFavorito = (CheckBox) findViewById(R.id.cbFavorito);

            if ((eNombre.equals("")) || (eOrganizacion.equals("")) || (eCorreo.equals(""))){
                Toast.makeText(getApplicationContext(), "Rellena los campos", Toast.LENGTH_SHORT).show();
            } else {

                datos.nuevoPerfil(eNombre.getText().toString(), 
                        eOrganizacion.getText().toString(), eCorreo.getText().toString());


                Perfil p = new Perfil();
                p.setUsername(eNombre.getText().toString());
                p.setOrganizacion(eOrganizacion.getText().toString());
                p.setCorreo(eCorreo.getText().toString());
                p.setFavorito(eFavorito.isChecked());

                MainActivity.lstPerfiles.add(p);

                eNombre.setText("");
                eOrganizacion.setText("");
                eCorreo.setText("");

                Toast.makeText(getApplicationContext(), "Perfil guardado", Toast.LENGTH_SHORT).show();

                i = new Intent(CrearPerfil.this, MainActivity.class);
                startActivity(i);
            }

            break;

        case R.id.btCancelarPerfil:

            i = new Intent(CrearPerfil.this, MainActivity.class);
            startActivity(i);

            break;

        default: break;
    }
}

}

And this one, the database for SQLite creator ...

public class Database extends SQLiteOpenHelper {

    private static final String BBDD_NOMBRE = "baseDatos.db";
    private static String[] FROM_CURSOR = {_ID, NOMBRE_USUARIO, NOMBRE_ORGANIZACION, NOMBRE_CORREO };
    private static String ORDER_BY = NOMBRE_USUARIO + " DESC";

    public Database(Context contexto) {
        super(contexto, BBDD_NOMBRE, null, 1 );
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLA_USUARIOS + "("
                + _ID+" INTEGER PRIMARY KEY AUTOINCREMENT, " 
                + NOMBRE_USUARIO + " TEXT NOT NULL, " 
                + NOMBRE_ORGANIZACION + " TEXT NOT NULL, "
                + NOMBRE_CORREO + "TEXT NOT NULL);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int a, int b) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLA_USUARIOS);
        onCreate(db);
    }

    public void nuevoPerfil(String n, String o, String c){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues value = new ContentValues();
        value.put(NOMBRE_USUARIO, n);
        value.put(NOMBRE_ORGANIZACION, o);
        value.put(NOMBRE_CORREO, c);
        db.insertOrThrow(TABLA_USUARIOS, null, value);
    }

    public Cursor getPerfiles() {

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.query(TABLA_USUARIOS, FROM_CURSOR, null, null, null, null, ORDER_BY);

        return c;
    }

}

NEED HELP PLEASE .. THANKS...

like image 554
juannmcc Avatar asked Nov 29 '13 21:11

juannmcc


4 Answers

You are missing a space in your CREATE TABLE statement:

NOMBRE_CORREO + "TEXT NOT NULL);");

should be

NOMBRE_CORREO + " TEXT NOT NULL);");
like image 86
ianhanniballake Avatar answered Nov 11 '22 19:11

ianhanniballake


This problem is mainly caused by syntax. for example, previously my code was correct. here it is:

    String CREATE_PRODUCTS_TABLE = "CREATE TABLE " + TABLE_PRODUCTS + "("
                    + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_MESSAGEADDRESS
                    + " TEXT," + COLUMN_MESSAGEBODY + " TEXT " + ")";

The above code was running correct but i added another column and it started causing the mentioned error. below is the erroneous code:

    String CREATE_PRODUCTS_TABLE = "CREATE TABLE " + TABLE_PRODUCTS + "("
                    + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_MESSAGEADDRESS
                    + " TEXT," + COLUMN_MESSAGEBODY + " TEXT" + COLUMN_MESSAGETIME + " LONG" + ")";

As you can see, when i added the new column i forgot to include a comma after type TEXT. this means that when this code is executed, there will be syntax error as the compiler will read the last part as:

    COLUMN_MESSAGEBODY TEXTCOLUMN_MESSAGETIME LONG

as opposed to:

    COLUMN_MESSAGEBODY TEXT, COLUMN_MESSAGETIME LONG

Solution: ensure that your syntax is correct and you heed to spaces and commas.

kindly check the below link for more info: Android : Table has no column named "variable name" MySql Database error

like image 23
joeabala Avatar answered Nov 11 '22 18:11

joeabala


This problem occurs mostly when you make a change in the database but do not delete the previous database. Uninstall the app and then run it.

like image 20
Shikha Gupta Avatar answered Nov 11 '22 19:11

Shikha Gupta


You can just specify the version like this: private static final int VERSION = 4;

like image 31
Utopia Avatar answered Nov 11 '22 20:11

Utopia