Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are missing a space in your CREATE TABLE statement:

NOMBRE_CORREO + "TEXT NOT NULL);");

should be

NOMBRE_CORREO + " TEXT NOT NULL);");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...