“_id”列不存在

时间:2015-05-05 00:22:04

标签: android sqlite

java.lang.IllegalArgumentException:列'_id'不存在。

确定是的,这是正确的,但为什么我得到这个消息是我很困惑的。如果有的话,那就是列'_id2'会更加明显。 有人可以告诉我我做错了什么以及我该如何解决它?

我有一个波纹管格式的数据库。

id2 LastEdit
1   2015-04-28 12:27:28

使用下面的方法从我的表中获取数据并将其放入int。

   Cursor cursor = dbManager.fetch2();
    final String[] from = new String[] { DatabaseHelper._ID2,
            DatabaseHelper.TUT_EDIT };
    final int[] to = new int[] { R.id.pix, R.id.LastEdit };

将其转换为字符串

之前
    LastEdit =(R.id.LastEdit);
    String TT = String.valueOf(LastEdit);
    Log.i("lastingthe edit: ", TT);

我的DBManager文件

    import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DBManager {

private DatabaseHelper dbHelper;

private Context context;

private SQLiteDatabase database;

public DBManager(Context c) {
    context = c;
}

public DBManager open() throws SQLException {
    dbHelper = new DatabaseHelper(context);
    database = dbHelper.getWritableDatabase();
    return this;

}

public void close() {
    dbHelper.close();
}


public void insert(String id, String cat, String title, String datec, String datem, String name, String image, String video) {
    ContentValues contentValue = new ContentValues();
    contentValue.put(DatabaseHelper.TAG_TUTID, id);
    contentValue.put(DatabaseHelper.TAG_CATID, cat);
    contentValue.put(DatabaseHelper.TAG_TUTTITLE, title);
    contentValue.put(DatabaseHelper.TAG_NAME, name);
    contentValue.put(DatabaseHelper.TAG_DATEC, datec);
    contentValue.put(DatabaseHelper.TAG_DATEM, datem);
    contentValue.put(DatabaseHelper.TAG_IMAGE, image);
    contentValue.put(DatabaseHelper.TAG_VIDEO, video);
    database.insert(DatabaseHelper.TABLE_NAME, null, contentValue);
    Log.d("testing insert: ", cat);
}

public void insert2(String edit) {
    ContentValues contentValue = new ContentValues();
    contentValue.put(DatabaseHelper.TUT_EDIT, edit);
    database.insert(DatabaseHelper.TABLE_NAME2, null, contentValue);
    Log.d("testing insert2: ", edit);
}

public Cursor fetch() {
    String[] columns = new String[] { DatabaseHelper._ID, DatabaseHelper.TAG_TUTID, DatabaseHelper.TAG_CATID, DatabaseHelper.TAG_TUTTITLE, DatabaseHelper.TAG_NAME, DatabaseHelper.TAG_DATEC, DatabaseHelper.TAG_DATEM, DatabaseHelper.TAG_IMAGE, DatabaseHelper.TAG_VIDEO };
    Cursor cursor = database.query(DatabaseHelper.TABLE_NAME, columns, null, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
    }
    return cursor;
}
public Cursor fetch2() {
    String[] columns = new String[] { DatabaseHelper._ID2, DatabaseHelper.TUT_EDIT };
    Cursor cursor = database.query(DatabaseHelper.TABLE_NAME2, columns, null, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
    }
    return cursor;
}

public int update(long _id, String id, String cat, String title, String datec, String datem, String name, String image, String video) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(DatabaseHelper.TAG_TUTID, id);
    contentValues.put(DatabaseHelper.TAG_CATID, cat);
    contentValues.put(DatabaseHelper.TAG_TUTTITLE, title);
    contentValues.put(DatabaseHelper.TAG_NAME, name);
    contentValues.put(DatabaseHelper.TAG_DATEC, datec);
    contentValues.put(DatabaseHelper.TAG_DATEM, datem);
    contentValues.put(DatabaseHelper.TAG_IMAGE, image);
    contentValues.put(DatabaseHelper.TAG_VIDEO, video);
    Log.d("testing update: ", image);

    int i = database.update(DatabaseHelper.TABLE_NAME, contentValues, DatabaseHelper._ID + " = " + _id, null);
    return i;
}
public int update2(long _id, String edit) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(DatabaseHelper.TUT_EDIT, edit);
    Log.d("testing update2: ", edit);

    int i = database.update(DatabaseHelper.TABLE_NAME2, contentValues, DatabaseHelper._ID2 + " = " + _id, null);
    return i;
}
public void delete(long _id) {
    database.delete(DatabaseHelper.TABLE_NAME, DatabaseHelper._ID + "=" + _id, null);
}
}

我的DBHelper文件

    import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {

// Table Name
public static final String TABLE_NAME = "TODOS";

// Table columns
public static final String _ID = "_id";
public static final String TAG_TUTID = "tutID";
public static final String TAG_CATID = "catID";
public static final String TAG_TUTTITLE = "tutTitle";
public static final String TAG_NAME = "tutText";
public static final String TAG_DATEC = "tutDateCreated";
public static final String TAG_DATEM = "tutDateModified";
public static final String TAG_IMAGE = "imageID";
public static final String TAG_VIDEO = "videoID";


public static final String TABLE_NAME2 = "TOEDIT";
public static final String _ID2 = "_id2";
public static final String TUT_EDIT = "lastEdit";



// Database Information
static final String DB_NAME = "tuts.DB";

// database version
static final int DB_VERSION = 1;

// Creating table query
private static final String CREATE_TABLE = "create table " + TABLE_NAME + "(" + _ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TAG_TUTID + " TEXT NOT NULL, " + TAG_CATID + " TEXT, " + TAG_TUTTITLE+ " TEXT, " + TAG_NAME+ " TEXT,  " + TAG_DATEC+ " TEXT, " + TAG_DATEM+ " TEXT,  " + TAG_IMAGE+ " TEXT, " + TAG_VIDEO+ " TEXT);";

private static final String CREATE_TABLE2 = "create table " + TABLE_NAME2 + "(" + _ID2
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TUT_EDIT + " TEXT NOT NULL);";

public DatabaseHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {db.execSQL(CREATE_TABLE);{db.execSQL(CREATE_TABLE2);
    Log.d("testing create: ", DB_NAME);}}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME2);
    onCreate(db);
    Log.d("testing delete: ", DB_NAME);
}
}

日志

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.drox.codesync/com.example.drox.codesync.MainScreenActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at 

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
            at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
            at android.widget.CursorAdapter.init(CursorAdapter.java:172)
            at android.widget.CursorAdapter.<init>(CursorAdapter.java:149)
            at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91)
            at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
            at com.example.drox.codesync.MainScreenActivity.onCreate(MainScreenActivity.java:80)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

我认为必须选择&#39; (或者选择&#39;)称为_id的东西,因为SimpleCursorAdapter需要它。

来自documentation

  

处理内容URI ID

     

按照惯例,提供商提供对表中单行的访问权限   接受内容URI,其中包含该行末尾的行ID值   URI。同样按照惯例,提供商将ID值与表格进行匹配   _ID列,并对匹配的行执行请求的访问。

     

此约定有助于应用程序访问的通用设计模式   提供者。该应用程序对提供程序进行查询并显示   使用CursorListView中生成CursorAdapter。定义   CursorAdapter要求Cursor中的一列为_ID

所以你的第二个表创建应该如下所示

private static final String CREATE_TABLE2 = "create table " + TABLE_NAME2 + "(" + _ID
    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TUT_EDIT + " TEXT NOT NULL);";

你应该根据第二张表中的新id字段编辑下面的方法

public Cursor fetch2() {
    String[] columns = new String[] { DatabaseHelper._ID, DatabaseHelper.TUT_EDIT };
    Cursor cursor = database.query(DatabaseHelper.TABLE_NAME2, columns, null, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
    }
    return cursor;
}


public int update2(long _id, String edit) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(DatabaseHelper.TUT_EDIT, edit);
    Log.d("testing update2: ", edit);
    int i = database.update(DatabaseHelper.TABLE_NAME2, contentValues, DatabaseHelper._ID+ " = " + _id, null);
    return i;
}