android.database.sqlite.SQLiteException: 表 X 没有名为 Y: 的列,编译时: INSERT INTO
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20293812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android.database.sqlite.SQLiteException: table X has no column named Y: , while compiling: INSERT INTO
提问by juannmcc
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]...
这是 mainActivity,有一个按钮可以进入一个 Activity 以创建一个“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...
需要帮助请...谢谢...
回答by ianhanniballake
You are missing a space in your CREATE TABLE
statement:
您的CREATE TABLE
语句中缺少一个空格:
NOMBRE_CORREO + "TEXT NOT NULL);");
should be
应该
NOMBRE_CORREO + " TEXT NOT NULL);");
回答by joeabala
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:
如您所见,当我添加新列时,我忘记在输入 TEXT 后包含一个逗号。这意味着当执行此代码时,会出现语法错误,因为编译器会将最后一部分读取为:
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 数据库错误
回答by Shikha Gupta
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.
当您在数据库中进行更改但不删除以前的数据库时,通常会出现此问题。卸载该应用程序,然后运行它。
回答by Bilal H. Hungund
Uninstall the app, and again install it, because it can be solve by using two approaches, one by using
卸载app,重新安装,因为有两种方法可以解决,一种是用
alter method and increment version
改变方法和增量版本
or
或者
creating the table again by reinstalling the app
通过重新安装应用程序再次创建表
回答by Utopia
You can just specify the version like this:
private static final int VERSION = 4;
您可以像这样指定版本:
private static final int VERSION = 4;
回答by Z.Lancelot
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 上面提到的,很多时候这个问题是由你忘记空格或逗号的语法引起的,但如果你检查它没有问题但你仍然遇到问题,也许你需要在虚拟设备上卸载应用程序并重新启动它,这可能会对您有所帮助
回答by Sardorbek Muhammadjonov
Uninstall the app, because a version is installed in the phone that does not have this table and therefore can not find this table
卸载app,因为手机中安装了一个没有这张表的版本,所以找不到这张表
or
或者
to raise database versionand it will update the database with the newly created tables.
提高数据库版本,它将使用新创建的表更新数据库。
回答by Sameer Shelar
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.
PS 我的想法:这种卸载和重新安装真的很烦人。开发人员花了很多时间来查找错误,但最终解决方案是卸载并重新安装。如果您向应用程序添加权限(例如 Internet 访问权限),也会发生这种情况。这个数据库的东西是我发现的第二个这样的东西。谷歌的 Android 团队应该为此做些什么。