列'_id'不存在**错误**

时间:2011-10-05 10:00:17

标签: android sqlite

DB handel

package sh.mkt;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class handle extends SQLiteOpenHelper{

    public static final String SHARE_MKT_DB = "SHARE_DB";
    public static final String COMPANY_TABLE = "COMPANY_NAME";
    public static final String SHARE_TABLE = "SHARE_DETAIL";
    public static final int MYDATABASE_VERSION = 3;
    public static final String TAB_ID = "_id";
    public static final String TAB_ID2 = "_id2";
    public static final String COMPANY_NAME = "Company";
    public static final String SHARE_PRICE= "Shareprice";
    public static final String SHARE_VOLUME = "Sharevolume";

    public handle(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
          db.execSQL(SCRIPT_CREATE_DATABASE);
           db.execSQL(SCRIPT_CREATE_DATABASE2);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
              db.execSQL("DROP TABLE IF EXISTS " + COMPANY_TABLE);
              db.execSQL("DROP TABLE IF EXISTS " + SHARE_TABLE);
              onCreate(db);
    }

    private static final String SCRIPT_CREATE_DATABASE = "create table " + COMPANY_TABLE + " (" 
                +TAB_ID + " integer primary key autoincrement, " 
                + COMPANY_NAME + " text not null);";

    private static final String SCRIPT_CREATE_DATABASE2 = "create table " + SHARE_TABLE + " (" 
                + TAB_ID2 + " integer primary key autoincrement, " 
                + SHARE_PRICE + " text not null, " 
                + SHARE_VOLUME + " text not null, "
                + " FOREIGN KEY ("+TAB_ID2+") REFERENCES "+COMPANY_TABLE+" ("+TAB_ID+"));";
}

活动

    package sh.mkt;


import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class next extends Activity {

    TextView a;
    Button back;
    ListView list;

    ListView listContent;
    private handle hand;
    Cursor cursor;
    SimpleCursorAdapter cursorAdapter;
    private SQLiteDatabase dbObject;

    private static final String fields[] = { handle.TAB_ID2 , handle.SHARE_PRICE , handle.SHARE_VOLUME };

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

        a = (TextView) findViewById(R.id.textView123);
        back = (Button) findViewById(R.id.backb);
        list = (ListView) findViewById(R.id.contentlist3);


//      Company obj1 = new Company();
//      String val1 = obj1.getCompanyID();

        hand = new handle(getApplicationContext(),
                handle.SHARE_MKT_DB, null,
                handle.MYDATABASE_VERSION);

        dbObject = hand.getWritableDatabase();

        Cursor data = dbObject.query(handle.SHARE_TABLE, fields, null, null, null, null, null);

        String[] from = new String[]{handle.SHARE_PRICE};
        int[] to = new int[]{R.id.text2, R.id.text3};
        cursorAdapter = new SimpleCursorAdapter(this, R.layout.row2, data, from, to);
        listContent.setAdapter(cursorAdapter);


        back.setOnClickListener(backlistner);

    }

    Button.OnClickListener backlistner = new Button.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            startActivity(new Intent(
                    "android.intent.action.ShareMarketActivity"));

        }

    };
}

_id不存在

  

10-05 16:27:01.268:ERROR / AndroidRuntime(16525):致命异常:主要   10-05 16:27:01.268:ERROR / AndroidRuntime(16525):   java.lang.RuntimeException:无法启动活动   ComponentInfo {sh.mkt / sh.mkt.next}:java.lang.IllegalArgumentException:   专栏' _id'不存在10-05 16:27:01.268:   错误/ AndroidRuntime(16525):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)   10-05 16:27:01.268:ERROR / AndroidRuntime(16525):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)   10-05 16:27:01.268:ERROR / AndroidRuntime(16525):at   android.app.ActivityThread.access $ 2300(ActivityThread.java:125)10-05   16:27:01.268:ERROR / AndroidRuntime(16525):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)   10-05 16:27:01.268:ERROR / AndroidRuntime(16525):at   android.os.Handler.dispatchMessage(Handler.java:99)10-05   16:27:01.268:ERROR / AndroidRuntime(16525):at   android.os.Looper.loop(Looper.java:123)10-05 16:27:01.268:   错误/ AndroidRuntime(16525):at   android.app.ActivityThread.main(ActivityThread.java:4627)10-05   16:27:01.268:ERROR / AndroidRuntime(16525):at   java.lang.reflect.Method.invokeNative(Native Method)10-05   16:27:01.268:ERROR / AndroidRuntime(16525):at   java.lang.reflect.Method.invoke(Method.java:521)10-05 16:27:01.268:   错误/ AndroidRuntime(16525):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)   10-05 16:27:01.268:ERROR / AndroidRuntime(16525):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)10-05   16:27:01.268:ERROR / AndroidRuntime(16525):at   dalvik.system.NativeStart.main(Native Method)10-05 16:27:01.268:   错误/ AndroidRuntime(16525):引起:   java.lang.IllegalArgumentException:column' _id'不存在10-05   16:27:01.268:ERROR / AndroidRuntime(16525):at   android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)   10-05 16:27:01.268:ERROR / AndroidRuntime(16525):at   android.widget.CursorAdapter.init(CursorAdapter.java:111)10-05   16:27:01.268:ERROR / AndroidRuntime(16525):at   android.widget.CursorAdapter。(CursorAdapter.java:90)10-05   16:27:01.268:ERROR / AndroidRuntime(16525):at   android.widget.ResourceCursorAdapter。(ResourceCursorAdapter.java:47)   10-05 16:27:01.268:ERROR / AndroidRuntime(16525):at   android.widget.SimpleCursorAdapter。(SimpleCursorAdapter.java:84)   10-05 16:27:01.268:ERROR / AndroidRuntime(16525):at   sh.mkt.next.onCreate(next.java:52)10-05 16:27:01.268:   错误/ AndroidRuntime(16525):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)   10-05 16:27:01.268:ERROR / AndroidRuntime(16525):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

嘿再次抱歉再次打扰......但它没有奏效......它显示有效但实际上并没有。

3 个答案:

答案 0 :(得分:3)

我认为您的字段选择:

private static final String fields[] = {handle.SHARE_PRICE, handle.SHARE_VOLUME};

应该是

private static final String fields[] = {handle.KEY_ID, handle.SHARE_PRICE, handle.SHARE_VOLUME};

因为游标Adapter需要一个包含'_id'列的游标来标识其行。无论如何,我总是使用BaseColumns来帮助我解决恼人的'_id'专栏。

答案 1 :(得分:1)

您可以通过简单的搜索轻松找到您的快乐,请参阅this

答案 2 :(得分:0)

您是否增加了handle.MYDATABASE_VERSION

要检查这是否是升级问题,请卸载应用程序并重新安装。如果您之后没有任何问题,则应检查升级代码。