java.lang.IllegalArgumentException:列“_id”不存在

发布于 2024-11-03 21:28:00 字数 7106 浏览 4 评论 0原文

我正在尝试在真实设备上调试我的应用程序,但收到此错误:

错误/AndroidRuntime(981):原因是: java.lang.IllegalArgumentException: 列“_id”不存在

当我在模拟器上测试时,错误不会出现。以下代码的最后一行给出了错误:

adapter = new SimpleCursorAdapter(this, R.layout.list_item, c, new String[] {   
            DataHandlerDB.CONTACT_NAME_COL,
            DataHandlerDB.CONTACT_NUMBER_COL,
            DataHandlerDB.CONTACT_DURATION_COL,
            DataHandlerDB.CONTACT_DATE_COL }, new int[] {
            R.id.contact_name, R.id.phone_number, R.id.duration, R.id.date });

这是我的活动:

public class MyActivity extends Activity {

    private static final String LOG_TAG = "MyActivity";
    private ListView listview;
    private SimpleCursorAdapter adapter;        
    private DataHandlerDB handler;
    private SQLiteDatabase db;
    private OpenHelper helper;
    private Cursor c;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.main);

        helper = new OpenHelper(this);
        db = helper.getWritableDatabase();
        helper.onCreate(db);
        setBasicContent();
        c.close();
    }   


    @Override
    public void onDestroy(){

        super.onDestroy();
        DataHandlerDB.makeTheSelection(this).close();
        db.close();
        helper.close();

    }

    @Override
    public void onPause(){

        super.onPause();
        DataHandlerDB.makeTheSelection(this).close();
        db.close();
        helper.close();

    }

    @Override
    public void onStop(){

        super.onStop();
        DataHandlerDB.makeTheSelection(this).close();
        db.close();
        helper.close();

    }


    @Override
    protected void onResume(){

        super.onResume();
        setBasicContent();

    }   

    public void setBasicContent() {

        listview = (ListView) findViewById(R.id.list_view); 

        Log.i(LOG_TAG, "listview " + listview);

        c = DataHandlerDB.makeTheSelection(this);

        c.moveToFirst();

        if(db.isOpen())
            Log.i(LOG_TAG, "db is opened");

        Log.i(LOG_TAG, "cursor: " + c.getCount());

        startManagingCursor(c);

        adapter = new SimpleCursorAdapter(this, R.layout.list_item, c, new String[] {   
                DataHandlerDB.CONTACT_NAME_COL,
                DataHandlerDB.CONTACT_NUMBER_COL,
                DataHandlerDB.CONTACT_DURATION_COL,
                DataHandlerDB.CONTACT_DATE_COL }, new int[] {
                R.id.contact_name, R.id.phone_number, R.id.duration, R.id.date });

        Log.i(LOG_TAG, "before setAdapter");
        Toast.makeText(this, "Before setAdapter", Toast.LENGTH_SHORT).show();

        listview.setAdapter(adapter);

        db.close();

        if(db.isOpen()){

            Log.i(LOG_TAG, "db is opened.");

        }

        if(!c.isClosed()){

            Log.i(LOG_TAG, "cursor is opened");

        }           
    }       
}

查询并返回 Cursor 的函数位于 DataHandlerDB 类中:

public class DataHandlerDB {

private static final String DATABASE_NAME = "calls.db";
private static final int DATABASE_VERSION = 1;

protected static String CONTACT_NAME_COL = "contact_name";
protected static String CONTACT_NUMBER_COL = "contact_number";
protected static String CONTACT_DURATION_COL = "duration";
protected static String CONTACT_DATE_COL = "date";
protected static String CONTACT_MONTH_COL = "month";

// create the DB
public static SQLiteDatabase createDB(Context ctx) {
    OpenHelper helper = new OpenHelper(ctx);
    SQLiteDatabase db = helper.getWritableDatabase();
    helper.onCreate(db);
    helper.onOpen(db);
    db.close();
    return db;
}

public static Cursor makeTheSelection(Context ctx) {

    OpenHelper helper = new OpenHelper(ctx);
    SQLiteDatabase db = helper.getWritableDatabase();

    Cursor cursor = db.query(TABLE_NAME_2, null, null, null, null, null,
            "duration desc");

    cursor.moveToFirst();
    db.close();

    return cursor;
}
    // class OpenHelper
public static class OpenHelper extends SQLiteOpenHelper {

    private final Context mContext;

    OpenHelper(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.mContext = context;

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i(LOG_TAG, "entrou no onCreate");
        String[] sql = mContext.getString(
                R.string.MyAppDatabase_OnCreate).split("\n");

        db.beginTransaction();

        try {
            execMultipleSQL(db, sql);
            db.setTransactionSuccessful();
        } catch (SQLException e) {

            Log.e("Error creating tables and debug data", e.toString());
            throw e;

        } finally {
            db.endTransaction();

        }
    }

    private void execMultipleSQL(SQLiteDatabase db, String[] sql) {

        for (String s : sql) {

            if (s.trim().length() > 0) {

                db.execSQL(s);
            }
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

         Log.w("MyDB Database",
         "Upgrading database, this will drop tables and recreate.");
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db);

    }

    @Override
    public void onOpen(SQLiteDatabase db) {

        super.onOpen(db);
    }
}
}

这是 XML使用 SQL 命令的文件:

<string name="MyAppDatabase_OnCreate">
    "CREATE TABLE IF NOT EXISTS contact_data(_id INTEGER PRIMARY KEY AUTOINCREMENT, contact_id INTEGER, contact_name VARCHAR(50), number_type VARCHAR(50), contact_number VARCHAR(50), duration TIME, duration_sum TIME, date DATE, current_time TIME, cont INTEGER, type VARCHAR, month VARCHAR(50), day VARCHAR(50), year VARCHAR(50));"
</string>

我认为应用程序在首次启动时并未创建数据库。我认为是这样,因为它可以找到 _id 列,但它是在 XML 代码中显式编写的,以使用 _id 列创建它。我还认为这是因为我已经在 SELECT 方法中显式编写了列,包括 _id。我是这样做的:

Cursor cursor = db.query(TABLE_NAME_2, 
                new String[]{
                "_id", 
                "contact_id", 
                "contact_name", 
                "number_type", 
                "contact_number", 
                "duration", 
                "duration_sum", 
                "date", 
                "current_time", 
                "cont", "type", 
                "month", 
                "day", 
                "year"}, null, null, null, null,
                "duration desc");

在这种情况下,我收到的错误几乎是相同的:

原因: android.database.sqlite.SQLiteException: 没有这样的列: _id: ,同时 编译:SELECT _id, contact_id, 联系人姓名、号码类型、 联系号码、持续时间、 持续时间总和、日期、当前时间、 续、类型、月、日、年 FROM contact_data ORDER BY 持续时间说明

我已经记录了数据库的第一列,如下所示:

Log.i(LOG_TAG, "Cursor(0)" + cursor.getColumnName(0));

它打印了 id,而不是 _id。可以看到,语句中写有_id。关于如何解决这个问题有什么建议吗?

I'm trying to debug my application on a real device but I get this error:

ERROR/AndroidRuntime(981): Caused by:
java.lang.IllegalArgumentException:
column '_id' does not exist

When I'm testing on an emulator, the error doesn't appear. The error is given in the last line of the following code:

adapter = new SimpleCursorAdapter(this, R.layout.list_item, c, new String[] {   
            DataHandlerDB.CONTACT_NAME_COL,
            DataHandlerDB.CONTACT_NUMBER_COL,
            DataHandlerDB.CONTACT_DURATION_COL,
            DataHandlerDB.CONTACT_DATE_COL }, new int[] {
            R.id.contact_name, R.id.phone_number, R.id.duration, R.id.date });

Here is my activity:

public class MyActivity extends Activity {

    private static final String LOG_TAG = "MyActivity";
    private ListView listview;
    private SimpleCursorAdapter adapter;        
    private DataHandlerDB handler;
    private SQLiteDatabase db;
    private OpenHelper helper;
    private Cursor c;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.main);

        helper = new OpenHelper(this);
        db = helper.getWritableDatabase();
        helper.onCreate(db);
        setBasicContent();
        c.close();
    }   


    @Override
    public void onDestroy(){

        super.onDestroy();
        DataHandlerDB.makeTheSelection(this).close();
        db.close();
        helper.close();

    }

    @Override
    public void onPause(){

        super.onPause();
        DataHandlerDB.makeTheSelection(this).close();
        db.close();
        helper.close();

    }

    @Override
    public void onStop(){

        super.onStop();
        DataHandlerDB.makeTheSelection(this).close();
        db.close();
        helper.close();

    }


    @Override
    protected void onResume(){

        super.onResume();
        setBasicContent();

    }   

    public void setBasicContent() {

        listview = (ListView) findViewById(R.id.list_view); 

        Log.i(LOG_TAG, "listview " + listview);

        c = DataHandlerDB.makeTheSelection(this);

        c.moveToFirst();

        if(db.isOpen())
            Log.i(LOG_TAG, "db is opened");

        Log.i(LOG_TAG, "cursor: " + c.getCount());

        startManagingCursor(c);

        adapter = new SimpleCursorAdapter(this, R.layout.list_item, c, new String[] {   
                DataHandlerDB.CONTACT_NAME_COL,
                DataHandlerDB.CONTACT_NUMBER_COL,
                DataHandlerDB.CONTACT_DURATION_COL,
                DataHandlerDB.CONTACT_DATE_COL }, new int[] {
                R.id.contact_name, R.id.phone_number, R.id.duration, R.id.date });

        Log.i(LOG_TAG, "before setAdapter");
        Toast.makeText(this, "Before setAdapter", Toast.LENGTH_SHORT).show();

        listview.setAdapter(adapter);

        db.close();

        if(db.isOpen()){

            Log.i(LOG_TAG, "db is opened.");

        }

        if(!c.isClosed()){

            Log.i(LOG_TAG, "cursor is opened");

        }           
    }       
}

The function that queries and returns the Cursor is in the class DataHandlerDB:

public class DataHandlerDB {

private static final String DATABASE_NAME = "calls.db";
private static final int DATABASE_VERSION = 1;

protected static String CONTACT_NAME_COL = "contact_name";
protected static String CONTACT_NUMBER_COL = "contact_number";
protected static String CONTACT_DURATION_COL = "duration";
protected static String CONTACT_DATE_COL = "date";
protected static String CONTACT_MONTH_COL = "month";

// create the DB
public static SQLiteDatabase createDB(Context ctx) {
    OpenHelper helper = new OpenHelper(ctx);
    SQLiteDatabase db = helper.getWritableDatabase();
    helper.onCreate(db);
    helper.onOpen(db);
    db.close();
    return db;
}

public static Cursor makeTheSelection(Context ctx) {

    OpenHelper helper = new OpenHelper(ctx);
    SQLiteDatabase db = helper.getWritableDatabase();

    Cursor cursor = db.query(TABLE_NAME_2, null, null, null, null, null,
            "duration desc");

    cursor.moveToFirst();
    db.close();

    return cursor;
}
    // class OpenHelper
public static class OpenHelper extends SQLiteOpenHelper {

    private final Context mContext;

    OpenHelper(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.mContext = context;

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i(LOG_TAG, "entrou no onCreate");
        String[] sql = mContext.getString(
                R.string.MyAppDatabase_OnCreate).split("\n");

        db.beginTransaction();

        try {
            execMultipleSQL(db, sql);
            db.setTransactionSuccessful();
        } catch (SQLException e) {

            Log.e("Error creating tables and debug data", e.toString());
            throw e;

        } finally {
            db.endTransaction();

        }
    }

    private void execMultipleSQL(SQLiteDatabase db, String[] sql) {

        for (String s : sql) {

            if (s.trim().length() > 0) {

                db.execSQL(s);
            }
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

         Log.w("MyDB Database",
         "Upgrading database, this will drop tables and recreate.");
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db);

    }

    @Override
    public void onOpen(SQLiteDatabase db) {

        super.onOpen(db);
    }
}
}

Here is the XML file with the SQL command:

<string name="MyAppDatabase_OnCreate">
    "CREATE TABLE IF NOT EXISTS contact_data(_id INTEGER PRIMARY KEY AUTOINCREMENT, contact_id INTEGER, contact_name VARCHAR(50), number_type VARCHAR(50), contact_number VARCHAR(50), duration TIME, duration_sum TIME, date DATE, current_time TIME, cont INTEGER, type VARCHAR, month VARCHAR(50), day VARCHAR(50), year VARCHAR(50));"
</string>

I think the application is not creating the database when it first starts up. I think so because it can find the column _id, but it is explicitly written in the XML code to create it with the _id column. I also think that because I've explicitly written the columns in the SELECT method, including the _id. I did it like this:

Cursor cursor = db.query(TABLE_NAME_2, 
                new String[]{
                "_id", 
                "contact_id", 
                "contact_name", 
                "number_type", 
                "contact_number", 
                "duration", 
                "duration_sum", 
                "date", 
                "current_time", 
                "cont", "type", 
                "month", 
                "day", 
                "year"}, null, null, null, null,
                "duration desc");

In this case, the error I receive is almost the same:

Caused by:
android.database.sqlite.SQLiteException:
no such column: _id: , while
compiling: SELECT _id, contact_id,
contact_name, number_type,
contact_number, duration,
duration_sum, date, current_time,
cont, type, month, day, year FROM
contact_data ORDER BY duration desc

I've logged the first column of the database like so:

Log.i(LOG_TAG, "Cursor(0)" + cursor.getColumnName(0));

It printed id, not _id. As you can see, there is _id written in the statement. Any suggestions on how to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

神经大条 2024-11-10 21:28:00

您正在尝试使用需要名为 _id 的列的游标。它就像编辑表创建语句并添加名为 _id 的列一样简单。

它的声明看起来像这样:

_id INTEGER PRIMARY KEY AUTOINCREMENT

添加这个然后你就可以使用它了。我相信这是使用 SimpleCursorAdapter 所必需的要求。

更新

"CREATE TABLE IF NOT EXISTS contact_data( _id INTEGER PRIMARY KEY AUTOINCREMENT, contact_id INTEGER, contact_name VARCHAR(50), number_type VARCHAR(50), contact_number VARCHAR(50), duration TIME, duration_sum TIME, date DATE, current_time TIME, cont INTEGER, type VARCHAR, month VARCHAR(50), day VARCHAR(50), year VARCHAR(50));"

解决方案:在左括号“(”和_id之间添加一个空格

You are trying to use a cursor that REQUIRES a column called _id. Its as simple as editing your table creation statement and adding a column called _id.

Its declartion looks something like this:

_id INTEGER PRIMARY KEY AUTOINCREMENT

Add this and you will then be able to use it. I believe this is a requirement that is required in order to use a SimpleCursorAdapter.

UPDATE

"CREATE TABLE IF NOT EXISTS contact_data( _id INTEGER PRIMARY KEY AUTOINCREMENT, contact_id INTEGER, contact_name VARCHAR(50), number_type VARCHAR(50), contact_number VARCHAR(50), duration TIME, duration_sum TIME, date DATE, current_time TIME, cont INTEGER, type VARCHAR, month VARCHAR(50), day VARCHAR(50), year VARCHAR(50));"

Solution: add a space between the left parenthesis '(' and _id

始终不够 2024-11-10 21:28:00

我遇到了类似的问题,因为我没有将 _id 列添加到投影参数中,因此将 _id 添加到查询的投影参数就是解决方案。 (由@nobugs 评论


示例:

 String[] projections = {"_id", "name", "age"};

 Cursor cursor = db.query(domainClass.getSimpleName(), projections, 
     null, null, null, null, null);

I have had similar problem because I was not adding the _id column to the projection argument, so adding _id to the projections argument of the query was the solution. (commented by @nobugs)


Example:

 String[] projections = {"_id", "name", "age"};

 Cursor cursor = db.query(domainClass.getSimpleName(), projections, 
     null, null, null, null, null);
水溶 2024-11-10 21:28:00

首先卸载应用程序,然后执行以下步骤:

  1. 清理项目
  2. 重建项目
  3. 调试应用程序(Shift+F9)

First of all uninstall the app, and then do following steps:

  1. Clean the project
  2. rebuild he project
  3. debug the app(Shift+F9)
许你一世情深 2024-11-10 21:28:00

CursorAdapter 始终需要存在 _id 列。因此,在用于创建 Cursor 的投影列表中,您需要添加 _id 列。

CursorAdapter always requires a _id column to be present. So, in the list of projections used for creating Cursor, you need to add _id column.

晨敛清荷 2024-11-10 21:28:00

我这样做并解决了我的问题。

之前

选择 id

之后

选择 id 作为 _id

I did this and solved my problem.

Before

SELECT id

After

SELECT id as _id

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文