Android数据库简单程序无效

时间:2015-03-25 12:06:17

标签: android sqlite simplecursoradapter

我正在使用SimpleCursorAdapter处理一个简单的应用程序,我收到错误java.lang.IllegalArgumentException: column '_id' does not exist,即使我有一个id = list的列表视图。 在运行应用程序时,它说,不幸的是数据库已停止。

这是我的代码:

MainActivity

package com.example.ayushi.database;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;


public class MainActivity extends Activity {

EditText text1,text2;
DatabaseHelper dbHelp;
ListView alist;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text1 = (EditText) findViewById(R.id.EText1);
    text2 = (EditText) findViewById(R.id.EText2);
    dbHelp = new DatabaseHelper(this);
   alist = (ListView) findViewById(R.id.newlist);
  // alist = getListView();
    getData();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void addNew(View view) {
    String s1 = text1.getText().toString();
    String s2 = text2.getText().toString();
    dbHelp.addData(s1,s2);
    getData();
}
public void getData()
{
    Cursor cursor = dbHelp.fetchData();

    ListAdapter myAdapter = new SimpleCursorAdapter(this,R.layout.tasks,
                            cursor,
                            new String[]{dbHelp._ID,dbHelp.COLUMN_1,dbHelp.COLUMN_2},
                            new int[]{R.id.idnum,R.id.c1,R.id.c2},0);
    alist.setAdapter(myAdapter);

}
}

DatabaseHelper

package com.example.ayushi.database;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="DbApp";
public static final int DATABASE_VERSION=1;
public static final String TABLE = "DbTable";
public static final String _ID = "id";
public static final String COLUMN_1="Task";
public static final String COLUMN_2="status";
public static final String CREATE_TABLE="CREATE TABLE "
                                        + TABLE + " ( "
                                        + _ID + " INTEGER PRIMARY KEY, "
                                        + COLUMN_1 + " TEXT, "
                                        + COLUMN_2 + " TEXT)";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void addData(String s1,String s2)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COLUMN_1,s1);
    values.put(COLUMN_2,s2);
    db.insert(TABLE,null,values);
    db.close();
}

public Cursor fetchData()
{
    SQLiteDatabase db = this.getReadableDatabase();
    return db.query(TABLE,
            new String[]{_ID,COLUMN_1,COLUMN_2},
            null, null, null, null, null);

}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE);
        onCreate(db);
}
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/AppName"/>
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/input1"
    android:layout_below="@+id/AppName"
    android:id="@+id/EText1"/>
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/input2"
    android:layout_below="@+id/EText1"
    android:id="@+id/EText2"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/EText2"
    android:text="@string/submit"
    android:onClick="addNew"
    android:id="@+id/button"
    />
<ListView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/newlist"

android:layout_below="@+id/button">

    </ListView>
</RelativeLayout>

tasks.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/idnum"/>
<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/c1"
   android:layout_below="@+id/idnum"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/c2"
    android:layout_below="@+id/c1"/>
</RelativeLayout>

logcat的:

 03-25 19:02:08.320  31391-31391/com.example.ayushi.database D/dalvikvm﹕ Zygote::ForkAndSpecialize : 0
03-25 19:02:08.321  31391-31391/com.example.ayushi.database D/dalvikvm﹕ zygote get new systemTid : 31391
03-25 19:02:08.321  31391-31391/com.example.ayushi.database D/dalvikvm﹕ Late-enabling CheckJNI
03-25 19:02:08.324  31391-31391/com.example.ayushi.database D/jdwp﹕ prepping for JDWP over ADB
 03-25 19:02:08.324  31391-31391/com.example.ayushi.database D/jdwp﹕ ADB transport startup
03-25 19:02:08.325  31391-31397/com.example.ayushi.database D/dalvikvm﹕ Elevating priority from 0 to -8
03-25 19:02:08.325  31391-31398/com.example.ayushi.database D/jdwp﹕ JDWP: thread running
03-25 19:02:08.326  31391-31398/com.example.ayushi.database D/jdwp﹕ acceptConnection
03-25 19:02:08.326  31391-31398/com.example.ayushi.database D/jdwp﹕ trying to receive file descriptor from ADB
03-25 19:02:08.327  31391-31391/com.example.ayushi.database D/dalvikvm﹕ zygote get thread init done
03-25 19:02:08.375  31391-31391/com.example.ayushi.database W/asset﹕ AssetManager-->addDefaultAssets CIP path not exsit!
 03-25 19:02:08.380  31391-31391/com.example.ayushi.database W/ActivityThread﹕ Application com.example.ayushi.database is waiting for the debugger on port 8100...
03-25 19:02:08.386  31391-31391/com.example.ayushi.database I/System.out﹕ Sending WAIT chunk
03-25 19:02:08.407  31391-31398/com.example.ayushi.database D/jdwp﹕ received file descriptor 44 from ADB
03-25 19:02:08.436  31391-31398/com.example.ayushi.database D/jdwp﹕ processIncoming
03-25 19:02:08.437  31391-31398/com.example.ayushi.database D/jdwp﹕ processIncoming
03-25 19:02:08.437  31391-31398/com.example.ayushi.database D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x40000101, flags=0x0, dataLen=0x8
 03-25 19:02:08.443  31391-31398/com.example.ayushi.database D/jdwp﹕ processIncoming
03-25 19:02:08.443  31391-31398/com.example.ayushi.database D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x17, id=0x40000102, flags=0x0, dataLen=0xC
03-25 19:02:08.445  31391-31398/com.example.ayushi.database D/jdwp﹕ sendBufferedRequest : len=0x14
03-25 19:02:08.446  31391-31398/com.example.ayushi.database D/jdwp﹕ processIncoming
03-25 19:02:08.446  31391-31398/com.example.ayushi.database D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x40000103, flags=0x0, dataLen=0x8
 03-25 19:02:08.448  31391-31398/com.example.ayushi.database D/jdwp﹕ processIncoming
03-25 19:02:08.448  31391-31398/com.example.ayushi.database D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x40000104, flags=0x0, dataLen=0x8
03-25 19:02:08.448  31391-31398/com.example.ayushi.database D/jdwp﹕ processIncoming
03-25 19:02:08.448  31391-31398/com.example.ayushi.database D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x14, id=0x40000105, flags=0x0, dataLen=0x9
03-25 19:02:08.453  31391-31398/com.example.ayushi.database D/jdwp﹕ processIncoming
03-25 19:02:08.453  31391-31398/com.example.ayushi.database D/jdwp﹕ handlePacket : cmd=0x7, cmdSet=0x1, len=0xB, id=0x97, flags=0x0, dataLen=0x0
03-25 19:02:08.453  31391-31398/com.example.ayushi.database I/dalvikvm﹕ Debugger is active
 03-25 19:02:08.470  31391-31398/com.example.ayushi.database D/jdwp﹕   processIncoming
03-25 19:02:08.470  31391-31398/com.example.ayushi.database D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xF, len=0x11, id=0x99, flags=0x0, dataLen=0x6
03-25 19:02:08.475  31391-31398/com.example.ayushi.database D/jdwp﹕ processIncoming
03-25 19:02:08.475  31391-31398/com.example.ayushi.database D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xF, len=0x11, id=0x9B, flags=0x0, dataLen=0x6
03-25 19:02:08.586  31391-31391/com.example.ayushi.database I/System.out﹕ Debugger has connected
03-25 19:02:08.586  31391-31391/com.example.ayushi.database D/jdwp﹕ +++ debugger interval=111
03-25 19:02:08.587  31391-31391/com.example.ayushi.database I/System.out﹕ waiting for debugger to settle...
03-25 19:02:08.632  31391-31398/com.example.ayushi.database D/jdwp﹕ processIncoming
03-25 19:02:08.632  31391-31398/com.example.ayushi.database D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0x1, len=0xB, id=0x9D, flags=0x0, dataLen=0x0
03-25 19:02:08.669  31391-31398/com.example.ayushi.database D/jdwp﹕ processIncoming
03-25 19:02:08.669  31391-31398/com.example.ayushi.database D/jdwp﹕ handlePacket : cmd=0x11, cmdSet=0x1, len=0xB, id=0x9F, flags=0x0, dataLen=0x0
03-25 19:02:08.692  31391-31398/com.example.ayushi.database D/jdwp﹕ processIncoming
03-25 19:02:08.692  31391-31398/com.example.ayushi.database D/jdwp﹕ handlePacket : cmd=0xC, cmdSet=0x1, len=0xB, id=0xA1, flags=0x0, dataLen=0x0
03-25 19:02:08.694  31391-31398/com.example.ayushi.database D/jdwp﹕ processIncoming
03-25 19:02:08.694  31391-31398/com.example.ayushi.database D/jdwp﹕ handlePacket : cmd=0x14, cmdSet=0x1, len=0xB, id=0xA3, flags=0x0, dataLen=0x0
03-25 19:02:08.787  31391-31391/com.example.ayushi.database D/jdwp﹕ +++ debugger interval=84
03-25 19:02:08.787  31391-31391/com.example.ayushi.database I/System.out﹕ waiting for debugger to settle...
03-25 19:02:08.987  31391-31391/com.example.ayushi.database D/jdwp﹕ +++ debugger interval=284
03-25 19:02:08.987  31391-31391/com.example.ayushi.database I/System.out﹕ waiting for debugger to settle...
03-25 19:02:09.022  31391-31398/com.example.ayushi.database D/jdwp﹕ processIncoming
03-25 19:02:09.022  31391-31398/com.example.ayushi.database D/jdwp﹕ handlePacket : cmd=0x5, cmdSet=0x1, len=0xB, id=0xA5, flags=0x0, dataLen=0x0
03-25 19:02:09.024  31391-31398/com.example.ayushi.database D/jdwp﹕ processIncoming
03-25 19:02:09.024  31391-31398/com.example.ayushi.database D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xF, len=0x11, id=0xA7, flags=0x0, dataLen=0x6
03-25 19:02:09.029  31391-31398/com.example.ayushi.database D/jdwp﹕ processIncoming
03-25 19:02:09.029  31391-31398/com.example.ayushi.database D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xF, len=0x11, id=0xA9, flags=0x0, dataLen=0x6
03-25 19:02:09.187  31391-31391/com.example.ayushi.database D/jdwp﹕ +++ debugger interval=159
03-25 19:02:09.187  31391-31391/com.example.ayushi.database I/System.out﹕ waiting for debugger to settle...
03-25 19:02:09.388  31391-31391/com.example.ayushi.database D/jdwp﹕ +++ debugger interval=359
03-25 19:02:09.388  31391-31391/com.example.ayushi.database I/System.out﹕ waiting for debugger to settle...
03-25 19:02:09.588  31391-31391/com.example.ayushi.database D/jdwp﹕ +++ debugger interval=559
03-25 19:02:09.588  31391-31391/com.example.ayushi.database I/System.out﹕ waiting for debugger to settle...
03-25 19:02:09.788  31391-31391/com.example.ayushi.database D/jdwp﹕ +++ debugger interval=760
03-25 19:02:09.788  31391-31391/com.example.ayushi.database I/System.out﹕ waiting for debugger to settle...
03-25 19:02:09.989  31391-31391/com.example.ayushi.database D/jdwp﹕ +++ debugger interval=960
03-25 19:02:09.989  31391-31391/com.example.ayushi.database I/System.out﹕ waiting for debugger to settle...
03-25 19:02:10.189  31391-31391/com.example.ayushi.database D/jdwp﹕ +++ debugger interval=1160
03-25 19:02:10.189  31391-31391/com.example.ayushi.database I/System.out﹕ waiting for debugger to settle...
03-25 19:02:10.389  31391-31391/com.example.ayushi.database D/jdwp﹕ +++ debugger interval=1361
03-25 19:02:10.390  31391-31391/com.example.ayushi.database I/System.out﹕ debugger has settled (1361)
03-25 19:02:10.401  31391-31391/com.example.ayushi.database D/jdwp﹕ sendRequest : Len=0x58
03-25 19:02:10.427  31391-31391/com.example.ayushi.database D/jdwp﹕ sendRequest : Len=0x53
03-25 19:02:10.428  31391-31391/com.example.ayushi.database D/jdwp﹕ sendRequest : Len=0x4B
03-25 19:02:10.453  31391-31391/com.example.ayushi.database D/jdwp﹕ sendRequest : Len=0x57
03-25 19:02:10.453  31391-31391/com.example.ayushi.database D/jdwp﹕ sendRequest : Len=0x62
03-25 19:02:10.470  31391-31391/com.example.ayushi.database D/jdwp﹕ sendRequest : Len=0x5C
03-25 19:02:10.487  31391-31391/com.example.ayushi.database D/jdwp﹕ sendRequest : Len=0x43
03-25 19:02:10.503  31391-31391/com.example.ayushi.database D/AbsListView﹕ checkAbsListViewlLogProperty get invalid command
03-25 19:02:10.507  31391-31391/com.example.ayushi.database D/jdwp﹕ sendRequest : Len=0x5A
03-25 19:02:10.528  31391-31391/com.example.ayushi.database D/jdwp﹕ sendRequest : Len=0x54
03-25 19:02:10.528  31391-31391/com.example.ayushi.database D/jdwp﹕ sendRequest : Len=0x52
03-25 19:02:10.529  31391-31391/com.example.ayushi.database D/jdwp﹕  sendRequest : Len=0x53
03-25 19:02:10.531  31391-31391/com.example.ayushi.database D/jdwp﹕ sendRequest : Len=0x5B
03-25 19:02:10.531  31391-31391/com.example.ayushi.database D/AndroidRuntime﹕ Shutting down VM
03-25 19:02:10.531  31391-31391/com.example.ayushi.database W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40cf29a8)
03-25 19:02:10.551  31391-31391/com.example.ayushi.database    E/AndroidRuntime﹕ FATAL EXCEPTION: main
   java.lang.RuntimeException: Unable to start activity      ComponentInfo{com.example.ayushi.database/com.example.ayushi.database.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)

 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)

       at android.app.ActivityThread.access$600(ActivityThread.java:162)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5371)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        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:168)
        at android.widget.CursorAdapter.<init>(CursorAdapter.java:145)
        at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91)
        at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
        at com.example.ayushi.database.MainActivity.getData(MainActivity.java:65)
        at com.example.ayushi.database.MainActivity.onCreate(MainActivity.java:29)
        at android.app.Activity.performCreate(Activity.java:5122)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)

at android.app.ActivityThread.access$600(ActivityThread.java:162)   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)

at android.os.Handler.dispatchMessage(Handler.java:107)

at android.os.Looper.loop(Looper.java:194)at android.app.ActivityThread.main(ActivityThread.java:5371)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:525)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method)

03-25 19:02:13.148  31391-31391/com.example.ayushi.database I/Process﹕ Sending signal. PID: 31391 SIG: 9

编辑:在原始问题中,错误是:符号变量列表不存在。

2 个答案:

答案 0 :(得分:1)

  

java.lang.IllegalArgumentException:column&#39; _id&#39;不存在

当您查询数据时会发生这种情况,在这种情况下,它就是您的fetchData()方法。 尝试这样的事情:

public Cursor fetchData() {
    String getData = ("SELECT id as _id, Task, status FROM DbTable);
    return db.rawQuery(getData,null);
}

OR

 public Cursor fetchData() {
    String getData = ("SELECT * FROM DbTable);
    return db.rawQuery(getData,null);
}

您可以参考以下链接获取进一步说明:

Link 1 Link 2Link 3

希望这有帮助!祝好运! :)

答案 1 :(得分:1)

尝试将public static final String _ID = "id";课程中的DatabaseHelper替换为public static final String _ID = "_id";

或者@Angeline说写一个自定义查询。