6

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...

3
  • 1
    Is it possible that you've initially executed without the "email" column added? If so, you might have to do a clear data so the data base can be created again. The procedure to create only happens on the first execution and it can get updates if the database version has changed, which goes to the onUpgrade method. Commented Nov 29, 2013 at 21:55
  • 1
    It's obvious that there is no column called email in your database table. Verify your database schema and column names before you execute any query. Commented Nov 29, 2013 at 21:58
  • so, i downloaded database and looked that the email was created like "emailTEXT Type:Nothing" ... But no, how i call onUpgrade method...? Cause I have created it but I execute this and looks the same error... Commented Nov 29, 2013 at 22:24

9 Answers 9

26

You are missing a space in your CREATE TABLE statement:

NOMBRE_CORREO + "TEXT NOT NULL);");

should be

NOMBRE_CORREO + " TEXT NOT NULL);");
Sign up to request clarification or add additional context in comments.

1 Comment

In my case I missed a comma, that took me two hours, silly mistake happen, I think thats why android Room ORM exist.
17

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

Comments

5

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.

2 Comments

You're a life savior. Thanks. I actually thought so too but somehow I thought that my table was empty! (I know- idiot me lol)..
happy to help .
1

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

Comments

1

Uninstall the app, and again install it, because it can be solve by using two approaches, one by using

alter method and increment version

or

creating the table again by reinstalling the app

Comments

0

As @joeabala has mentioned above, much time this problem is caused by syntax like you forgot the space or commas, but if you checked there is no problem with it but youstill get the problem, maybe you need to uninstall the app on the virtual device and restart it which may help you

Comments

0

Uninstall the app, because a version is installed in the phone that does not have this table and therefore can not find this table

or

to raise database version and it will update the database with the newly created tables.

Comments

0

Check if you have any syntax errors. If no syntax errors found just uninstall and reinstall the app. After which everything should work fine.

P.S. My thoughts: This uninstalling and reinstalling is really annoying. Developers spent so much time finding the bug but at the end the solution turns out to be uninstall and reinstall. This also happens if you add permissions(e.g. Internet Access permission) to your app. This database thing is second such thing i m finding. Android Team at Google should do something about this.

Comments

0

You can just Update version like this: private static final int VERSION = 4; OR make sure that you have create correct table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.