IllegalArgumentException:列_id不存在

时间:2013-06-09 18:32:01

标签: java android sqlite illegalargumentexception

我一直在尝试在ListView中显示数据库中的记录。我可以使用adb shell命令添加已确认的记录。我可以在那里查看记录。但是当我试图实现一个视图按钮来显示记录时,它只是崩溃并出现错误:illegalArgumentException。 column_id不存在是它的根。但我相信它存在!请帮助,因为这让我非常生气!

DISPLAYCURSOR 代码:

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

   }
    @SuppressWarnings("deprecation")
    public void filldata(){
    db.open();

   Cursor cursor = db.getAllRecords();
  startManagingCursor(cursor);

   String[] columns = new String[] {DBAdapter.invest_type,DBAdapter.curr_per_share_price, DBAdapter.share_name,
   DBAdapter.no_of_shares,DBAdapter.share_identity,DBAdapter.purchase_price,
   DBAdapter.purchase_from,DBAdapter.purchase_date,DBAdapter.purchase_contact};

  int[] to = new int[] { R.id.investmenttype,R.id.currpershareprice,R.id.sharename,R.id.shareno,R.id.shareid,                

   R.id.purprice,
   R.id.purfrom,R.id.purdate,R.id.purcon};
    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter( this, R.layout.row, cursor, columns, to);
   this.setListAdapter(mAdapter);
   db.close();
  }   
     } 

DBAdapter getallrecords功能:

  public Cursor getAllRecords()
   {
    return db.query(DATABASETABLE, new String[] { _id,invest_type,curr_per_share_price,
   share_name ,no_of_shares,share_identity,purchase_price,purchase_from,purchase_date,purchase_contact}, null,           
   null, null,null, null);        }

ROW.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/investmenttype"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="27dp" />

<TextView
android:id="@+id/currpershareprice"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/investmenttype"
android:layout_alignBottom="@+id/investmenttype"
android:layout_toRightOf="@+id/investmenttype" />

<TextView
 android:id="@+id/sharename"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/currpershareprice"
android:layout_alignBottom="@+id/currpershareprice"
android:layout_toRightOf="@+id/currpershareprice" />

<TextView
android:id="@+id/shareno"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/sharename"
android:layout_alignBottom="@+id/sharename"
android:layout_toRightOf="@+id/sharename" />

<TextView
android:id="@+id/shareid"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/shareno"
android:layout_alignBottom="@+id/shareno"
android:layout_toRightOf="@+id/shareno" />

<TextView
android:id="@+id/purprice"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/shareid"
android:layout_alignBottom="@+id/shareid"
android:layout_toRightOf="@+id/shareid" />
<TextView
android:id="@+id/purfrom"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/purprice"
android:layout_alignBottom="@+id/purprice"
android:layout_toRightOf="@+id/purprice" />
<TextView
android:id="@+id/purdate"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/purfrom"
android:layout_alignBottom="@+id/purfrom"
android:layout_toRightOf="@+id/purfrom" />
<TextView
 android:id="@+id/purcon"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/purdate"
android:layout_alignBottom="@+id/purdate"
android:layout_toRightOf="@+id/purdate" />
</LinearLayout>
</ScrollView>

activity_display_cursor

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ListView android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nodata"/>

</LinearLayout>

整个 DBAdapter 代码:

package com.example.studentdatabase;

import android.content.ContentValues;
public class DBAdapter{
    public static final String DATABASENAME = "new";
    public static final String DATABASETABLE="equities";
    public static final String _id="id";
    public static  final String invest_type = "investmenttype";
    public static final String curr_per_share_price = "currentshareprice";
    public static final String share_name="sharename";
    public static final String no_of_shares="noofshares";
    public static final String share_identity="shareidentity";
    public static final String purchase_price="purchaseprice";
    public static final String purchase_from="webagent";
    public static final String purchase_date="purchasedate";
    public static final String purchase_contact="agentcontactno";
    public static final String DATABASECREATE=
    "create table if not exists equities(" +
    "id INTEGER PRIMARY KEY," +
    "investmenttype VARCHAR not null," +
    "currentshareprice INTEGER," +
    "sharename VARCHAR not null," +
    "noofshares INTEGER,"+
    "shareidentity VARCHAR not null,"+
    "purchaseprice INTEGER," +
    "webagent VARCHAR not null," +
    "purchasedate DATE,"+
    "agentcontactno INTEGER);";
    public static final int DATABASEVERSION= 38;
    public static final String  TAG="DBAdapter";
    private final Context context;
    DatabaseHelper DBHelper;
    SQLiteDatabase db;
    public DBAdapter(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }
    public class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context,DATABASENAME,null,DATABASEVERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db)
        {
            //  try {
                db.execSQL(DATABASECREATE);
                //
            }
            /* catch (SQLException e) {
                e.printStackTrace();
            }
            */
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS equities");
            onCreate(db);
        }
    }
    //---opens the database---
    public DBAdapter open() throws SQLException
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }
    //---closes the database---
    public void close()
    {
        DBHelper.close();
    }
    public long addRecord(String investmenttype,String currentshareprice,String noofshares,String
    sharename,String shareidentity,String purchaseprice,
    String purchasedate,String webagent,String agentcontactno)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(invest_type, investmenttype);
        initialValues.put(curr_per_share_price,currentshareprice);
        initialValues.put(share_name, sharename);
        initialValues.put(no_of_shares, noofshares);
        initialValues.put(share_identity,shareidentity);
        initialValues.put(purchase_price, purchaseprice);
        initialValues.put(purchase_from, webagent);
        initialValues.put(purchase_date, purchasedate);
        initialValues.put(purchase_contact,agentcontactno);
        return db.insert(DATABASETABLE, null, initialValues);
    }
    //---retrieves a particular record---
    public Cursor getRecord(long rowId) throws SQLException
    {
        Cursor mCursor =
        db.query(true, DATABASETABLE, new String[] {
            _id,
            invest_type, curr_per_share_price, share_name,no_of_shares,share_identity,purchase_price,
            purchase_from,purchase_date,purchase_contact
        }
        ,
        _id + "=" + rowId, null, null, null,null,null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    public Cursor getAllRecords()
    {
        return db.query(DATABASETABLE, new String[] {
            "_id","invest_type","curr_per_share_price",
            "share_name","no_of_shares","share_identity","purchase_price","purchase_from","purchase_date","purchase_contact"
        }
        ,
        null, null, null, null, null);
    }
}

logcat的

  

06-10 12:38:17.965:I / SqliteDatabaseCpp(649):sqlite返回:错误代码= 1,msg =没有这样的列:_id,
  分贝= /数据/数据/ com.example.studentdatabase /数据库/新        06-10 12:38:17.965:D / AndroidRuntime(649):关闭VM        06-10 12:38:17.965:W / dalvikvm(649):threadid = 1:线程退出时未捕获异常(组= 0x409961f8)       06-10 12:38:18.045:E / AndroidRuntime(649):致命异常:主要       06-10 12:38:18.045:E / AndroidRuntime(649):java.lang.RuntimeException:无法启动活动C   ComponentInfo {com.example.studentdatabase / com.example.studentdatabase.DisplayCursor}:   android.database.sqlite.SQLiteException:没有这样的列:_id:,while   编译:SELECT _id,invest_type,curr_per_share_price,share_name,   no_of_shares,share_identity,purchase_price,purchase_from,   purchase_date,purchase_contact FROM股票        06-10 12:38:18.045:E / AndroidRuntime(649):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)       06-10 12:38:18.045:E / AndroidRuntime(649):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)   06-10 12:38:18.045:E / AndroidRuntime(649):at   android.app.ActivityThread.access $ 600(ActivityThread.java:122)06-10   12:38:18.045:E / AndroidRuntime(649):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1146)       06-10 12:38:18.045:E / AndroidRuntime(649):在android.os.Handler.dispatchMessage(Handler.java:99)06-10   12:38:18.045:E / AndroidRuntime(649):at   android.os.Looper.loop(Looper.java:137)       06-10 12:38:18.045:E / AndroidRuntime(649):在android.app.ActivityThread.main(ActivityThread.java:4340)06-10   12:38:18.045:E / AndroidRuntime(649):at   java.lang.reflect.Method.invokeNative(Native Method)06-10   12:38:18.045:E / AndroidRuntime(649):at   java.lang.reflect.Method.invoke(Method.java:511)06-10   12:38:18.045:E / AndroidRuntime(649):在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)   06-10 12:38:18.045:E / AndroidRuntime(649):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)       06-10 12:38:18.045:E / AndroidRuntime(649):at dalvik.system.NativeStart.main(Native Method)06-10 12:38:18.045:   E / AndroidRuntime(649):引起:   android.database.sqlite.SQLiteException:没有这样的c列:_id :,   编译时:SELECT _id,invest_type,curr_per_share_price,   share_name,no_of_shares,share_identity,purchase_price,   purchase_from,purchase_date,purchase_contact FROM equity 06-10   12:38:18.045:E / AndroidRuntime(649):在   android.database.sqlite.SQLiteCompiledSql.native_compile(母语   方法)06-10 12:38:18.045:E / AndroidRuntime(649):at   android.database.sqlite.SQLiteCompiledSql。   (SQLiteCompiledSql.java:64)06-10 12:38:18.045:   E / AndroidRuntime(649):at   android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)        06-10 12:38:18.045:E / AndroidRuntime(649):在android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)   06-10 12:38:18.045:E / AndroidRuntime(649):at   android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:127)
  06-10 12:38:18.045:E / AndroidRuntime(649):at   android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:94)
  06-10 12:38:18.045:E / AndroidRuntime(649):at   android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:53)
  06-10 12:38:18.045:E / AndroidRuntime(649):at   android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)   06-10 12:38:18.045:E / AndroidRuntime(649):at   android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)   06-10 12:38:18.045:E / AndroidRuntime(649):at   android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449)   06-10 12:38:18.045:E / AndroidRuntime(649):at   android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)   06-10 12:38:18.045:E / AndroidRuntime(649):at   android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1485)   06-10 12:38:18.045:E / AndroidRuntime(649):at   com.example.studentdatabase.DBAdapter.getAllRecords(DBAdapter.java:127)   06-10 12:38:18.045:E / AndroidRuntime(649):at   com.example.studentdatabase.DisplayCursor.filldata(DisplayCursor.java:34)       06-10 12:38:18.045:E / AndroidRuntime(649):at com.example.studentdatabase.DisplayCursor.onCreate(DisplayCursor.java:27)       06-10 12:38:18.045:E / AndroidRuntime(649):在android.app.Activity.performCreate(Activity.java:4465)06-10   12:38:18.045:E / AndroidRuntime(649):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)   06-10 12:38:18.045:E / AndroidRuntime(649):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)   06-10 12:38:18.045:E / AndroidRuntime(649):... 11更多

1 个答案:

答案 0 :(得分:5)

请参阅CursorAdapter

  

将数据从Cursor公开给ListView小部件的适配器。该   游标必须包含名为“_id”的列,否则此类将无效。

除此之外,我认为这一行:

return db.query(DATABASETABLE, new String[] { 
                                   _id, invest_type, curr_per_share_price,
                                   share_name, no_of_shares, share_identity,
                                   purchase_price, purchase_from, purchase_date,
                                   purchase_contact 
               }, null, null, null,null, null);

如果它们都是列名,应该是这样的:

return db.query(DATABASETABLE, new String[] { 
                                   "_id", "invest_type", "curr_per_share_price",
                                   "share_name", "no_of_shares", 
                                   "share_identity", "purchase_price", 
                                   "purchase_from", "purchase_date", 
                                   "purchase_contact" 
               }, null, null, null,null, null);