简体   繁体   English

安卓。 SQLite 异常:没有这样的列 _id

[英]Android. SQLite Exception: no such column _id

Im having some troubles trying to get the info stored in my database and showing it in a ListView.我在尝试获取存储在我的数据库中的信息并将其显示在 ListView 中时遇到了一些麻烦。

This is the ListActivity where i want to show the rows:这是我要显示行的 ListActivity:

public class Prueba extends ListActivity{

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.i_prueba);

        DataBaseAccess db = new DataBaseAccess(this);
        db.open();

        Cursor result = db.getAllContact();
        startManagingCursor(result);


        // the desired columns to be bound
        String[] columns = new String[] {"_id", "nombre", "telefono", "mail"};
        // the XML defined views which the data will be bound to
        int[] to = new int[] { R.id.textid, R.id.textNombre, R.id.textTelefono, R.id.textMail };

        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.i_listadb, result, columns, to);


        this.setListAdapter(mAdapter);

    }

The DB class.数据库类。 Here is the method getAllContact where i get the error:这是我收到错误的方法 getAllContact:

public class DataBaseAccess {公共类数据库访问{

public static final String NOMBRE = "nombre";
public static final String TELEFONO = "telefono";
public static final String MAIL = "mail";
public static final String ID = "_id";


    public DataBaseAccess(Context context) {
        this.androidContext = context;
//DataBaseHelper is another class where the Database is created. In the "onCreate" //method of that class is where i make the CREATE TABLE and add some values to that //table.
        dbHelper = new DataBaseHelper(androidContext);  
    }



    public void open() throws SQLException {
        db = dbHelper.getWritableDatabase();
        }



[...MORE METHODS...]

    public Cursor getAllContact(){
        String[] columnas = new String[] {ID, NOMBRE, TELEFONO, MAIL};
        Cursor mCursor = dbHelper.query("t_contactos", columnas, null, null, null, null, null);
    if (mCursor != null){
        mCursor.moveToFirst();
    }
    return mCursor;
}

} }

And here is the error messages from the LogCat:这是来自 LogCat 的错误消息:

E/AndroidRuntime(  636): Uncaught handler: thread main exiting due to uncaught exception

E/AndroidRuntime(  636): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pfc.android/com.pfc.android.Prueba}: android.database.sqlite.SQLiteException: no such column: _id: , while compiling: SELECT _id, nombre, telefono, mail FROM t_contactos

E/AndroidRuntime(  636):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)

E/AndroidRuntime(  636):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)

E/AndroidRuntime(  636):    at android.app.ActivityThread.access$2100(ActivityThread.java:116)

E/AndroidRuntime(  636):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)

E/AndroidRuntime(  636):    at android.os.Handler.dispatchMessage(Handler.java:99)

E/AndroidRuntime(  636):    at android.os.Looper.loop(Looper.java:123)

E/AndroidRuntime(  636):    at android.app.ActivityThread.main(ActivityThread.java:4203)

E/AndroidRuntime(  636):    at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime(  636):    at java.lang.reflect.Method.invoke(Method.java:521)

E/AndroidRuntime(  636):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)

E/AndroidRuntime(  636):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)

E/AndroidRuntime(  636):    at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime(  636): Caused by: android.database.sqlite.SQLiteException: no such column: _id: , while compiling: SELECT _id, nombre, telefono, mail FROM t_contactos

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteProgram.native_compile(Native Method)

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:110)

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49)

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1118)

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1006)

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:964)

E/AndroidRuntime(  636):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1041)

E/AndroidRuntime(  636):    at com.pfc.android.DataBaseAccess.getAllContact(DataBaseAccess.java:97)

E/AndroidRuntime(  636):    at com.pfc.android.Prueba.onCreate(Prueba.java:21)

E/AndroidRuntime(  636):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)

E/AndroidRuntime(  636):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)

E/AndroidRuntime(  636):    ... 11 more

So, can anybody help to get through this?那么,有人可以帮助解决这个问题吗? Thank you in advance !提前谢谢你!

例如,如果您使用字段“a”、“b”和“c”创建数据库并使用此数据库,那么如果您添加新字段(例如“d”),您需要在手机上重新创建数据库(只需删除它)。

You probably don't have a column _id in your database table.您的数据库表中可能没有 _id 列。 Pull the database and open it with SqliteViewer to check if it actually exists.拉取数据库,用SqliteViewer打开查看是否真的存在。 The database is usually under /data/data/com.yourapp.package/databases/数据库通常在/data/data/com.yourapp.package/databases/下

You can use: http://sqlitebrowser.sourceforge.net/ or http://www.navicat.com/en/products/navicat_sqlite/sqlite_overview.html (they also have a free lite version)您可以使用: http : //sqlitebrowser.sourceforge.net/http://www.navicat.com/en/products/navicat_sqlite/sqlite_overview.html (他们也有免费的精简版)

Try rowid instead of _id .尝试使用rowid而不是_id It works for me.它对我有用。

Right, you should create the database table before querying on it.是的,您应该在查询之前创建数据库表。

Here is an example:下面是一个例子:

 private static final String DATABASE_CREATE_DRIVERS =
        "create table " + DATABASE_TABLE_DRIVERS + "(" + KEY_ROWID +" integer primary key autoincrement, "
        + KEY_DRIVERID + " text not null,"
        + KEY_FIRST_NAME + " text not null,"
        + KEY_LAST_NAME + " text not null,"
        + KEY_SPEED_LIMIT + " int not null,"
        + KEY_TIMESTAMP + " int"
        + ");";

Then query on it here:然后在这里查询:

 public Cursor fetchAllDrivers(){
     Cursor mCursor =
         mDb.query(true, DATABASE_TABLE_DRIVERS, new String[] {
                 KEY_ROWID, 
                 KEY_DRIVERID,
                 KEY_FIRST_NAME,
                 KEY_LAST_NAME,
                 KEY_SPEED_LIMIT,
                 KEY_TIMESTAMP},"", null,null, null, null, null);
     if (mCursor != null) {
         mCursor.moveToFirst();
     }
     return mCursor;
 }

It also appears that you are missing a sixth argument as to what you are querying for.您似乎还缺少关于您要查询的内容的第六个参数。 See where I put "" in method parameter string.看看我把“”放在方法参数字符串中的什么地方。

This is can exists 2 possiblities:这是可以存在 2 种可能性:

  1. This problem can occurred from broken SQLite schema.这个问题可能是由损坏的 SQLite 架构引起的。

Your question is not about SQL Statement problem.您的问题与 SQL 语句问题无关。 but many developer can think SQL Statement problem about your question.但是许多开发人员可以将您的问题视为 SQL 语句问题。

This case can check no demaged data in your database file.这种情况可以检查您的数据库文件中没有损坏的数据。 Although you can not use select fields and sql where clause by field name.虽然您不能按字段名称使用选择字段和 sql where 子句。

As a result, you can not use database file in your android code.因此,您无法在 android 代码中使用数据库文件。

Exactly solution, I recommend recreate SQLite DB file, step by step.确切的解决方案,我建议逐步重新创建 SQLite DB 文件。

You must be backup before use SQLite modification tool.使用 SQLite 修改工具前必须进行备份。 (SQLite Manager, Browser, others db manage tools) (SQLite Manager, Browser, 其他数据库管理工具)

  1. This problem occurred from your persistent data.此问题是由您的持久数据引起的。

If you use the same file name for assets or raw data when run with modified data,如果在使用修改后的数据运行时对资产或原始数据使用相同的文件名,

you can try uninstall previous installed app for refresh.您可以尝试卸载以前安装的应用程序进行刷新。

i change my database name, all lower_caps.我更改了我的数据库名称,全部为lower_caps。 it worked for me.它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM