gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:13:09 25 4
gpt4 key购买 nike

数据库交易

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+"));";
}

Activity

    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): FATAL EXCEPTION: main 10-05 16:27:01.268: ERROR/AndroidRuntime(16525): java.lang.RuntimeException: Unable to start activity ComponentInfo{sh.mkt/sh.mkt.next}: java.lang.IllegalArgumentException: column '_id' does not exist 10-05 16:27:01.268: ERROR/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: ERROR/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: ERROR/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: ERROR/AndroidRuntime(16525): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist 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: ERROR/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)

嘿,再次抱歉再次打扰...但它没有用...它显示为有效但实际上没有。

最佳答案

我认为您的字段选择:

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};

因为游标适配器需要一个包含“_id”列的游标来标识它的行。不管怎样,我总是用 BaseColumns帮助我解决烦人的“_id”列。

关于android - 列 '_id' 不存在 **错误**,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7659601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com