android.database.sqlite。SQLiteException:表X没有列命名为Y:在编译时:插入。

时间:2021-01-04 23:02:39

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

这是主要活动,有一个按钮用于创建“Perfil”(用户)[btCrearPerfil]…和他们一起看listView [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 :

这是Perfil的创建者,由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 ...

这是SQLite创建者的数据库…

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

需要帮助请. .谢谢……

5 个解决方案

#1


19  

You are missing a space in your CREATE TABLE statement:

您在CREATE TABLE语句中缺少一个空格:

NOMBRE_CORREO + "TEXT NOT NULL);");

should be

应该是

NOMBRE_CORREO + " TEXT NOT NULL);");

#2


13  

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

请查看下面的链接以获得更多的信息:Android:表没有名为“变量名”MySql数据库错误的列。

#3


3  

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

您可以指定这样的版本:private static final int version = 4;

#4


1  

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.

这个问题主要发生在您在数据库中进行更改时,但不删除以前的数据库。卸载应用程序,然后运行它。

#5


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

随着@joeabala上面所提到的,太多的时间这个问题是由于语法如你忘记了空间或逗号,但如果你检查没有问题但是youstill得到这个问题,也许你需要卸载应用程序在虚拟设备并重启它可能帮助你

#1


19  

You are missing a space in your CREATE TABLE statement:

您在CREATE TABLE语句中缺少一个空格:

NOMBRE_CORREO + "TEXT NOT NULL);");

should be

应该是

NOMBRE_CORREO + " TEXT NOT NULL);");

#2


13  

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

请查看下面的链接以获得更多的信息:Android:表没有名为“变量名”MySql数据库错误的列。

#3


3  

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

您可以指定这样的版本:private static final int version = 4;

#4


1  

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.

这个问题主要发生在您在数据库中进行更改时,但不删除以前的数据库。卸载应用程序,然后运行它。

#5


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

随着@joeabala上面所提到的,太多的时间这个问题是由于语法如你忘记了空间或逗号,但如果你检查没有问题但是youstill得到这个问题,也许你需要卸载应用程序在虚拟设备并重启它可能帮助你