gpt4 book ai didi

java - _ID不存在

转载 作者:行者123 更新时间:2023-12-02 08:09:05 25 4
gpt4 key购买 nike

我正在尝试从幻灯片 bbdd 获取数据并将其显示在 ListView 中,但出现错误。

public class DataBaseHelper extends SQLiteOpenHelper{

private static String DB_PATH = "/data/data/com.rbrlnx.lugares/databases/";
private static final String DATABASE_NAME="db.db";
SQLiteDatabase db;

String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS lugares (" +
" _id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"nombre text," +
"descripcion text,"+
"latitud real," +
"longitud real," +
"foto String);";

/*Primero se crea constructor, funcion onCreate, onUpgrade,Abrir y Cerrar*/

public DataBaseHelper(Context context){
super(context,DATABASE_NAME,null,1);

}

public void onCreate(SQLiteDatabase db){
try {
openDataBase();
db.execSQL(CREATE_TABLE);

} catch (Exception e) {
// handle exception
}
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
public void openDataBase() throws SQLException{

//Open the database
String myPath = DB_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

}

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


/*Despues metodos para añadir y obtener datos*/

public long addNombre(String nombre){

ContentValues cv = new ContentValues();
cv.put("nombre", nombre);
return db.insert("lugares", null, cv);
}

public long addDescripcion(String descripcion){

ContentValues cv = new ContentValues();
cv.put("descripcion", descripcion);
return db.insert("lugares", null, cv);

}
public long addLatitud(double latitud){

ContentValues cv = new ContentValues();
cv.put("latitud", latitud);
return db.insert("lugares", null, cv);

}
public long addLongitud(double longitud){

ContentValues cv = new ContentValues();
cv.put("longitud", longitud);
return db.insert("lugares", null, cv);

}

public long addFoto(String foto) {

ContentValues cv = new ContentValues();
cv.put("foto", foto);
return db.insert("lugares", null, cv);

}


public Cursor getNombres(){
SQLiteDatabase db = this.getWritableDatabase();

Cursor respuesta = db.rawQuery("select nombre from lugares", null);
return respuesta;

}







}

public class listatab extends ListActivity{ 

Context context;
ListView listanombres;
DataBaseHelper ayudabbdd;


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DataBaseHelper ayudabbdd = new DataBaseHelper(this);
Cursor nombresC;
nombresC = (Cursor) ayudabbdd.getNombres();
startManagingCursor(nombresC);
if(nombresC!=null){
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.listatab, nombresC, new String[] { "nombre" }, new int[] { R.id.lista });
this.setListAdapter(adapter);
this.getListView().setTextFilterEnabled(true);

}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (ayudabbdd != null) {
ayudabbdd.close();
}
}
}

日志猫向我显示了这个错误:

10-11 00:55:49.930: ERROR/AndroidRuntime(32392): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rbrlnx.lugares/com.rbrlnx.lugares.listatab}: java.lang.IllegalArgumentException: column '_id' does not exist
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.view.View.performClick(View.java:2485)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.view.View$PerformClick.run(View.java:9080)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.os.Handler.handleCallback(Handler.java:587)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.os.Handler.dispatchMessage(Handler.java:92)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.os.Looper.loop(Looper.java:130)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at java.lang.reflect.Method.invokeNative(Native Method)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at java.lang.reflect.Method.invoke(Method.java:507)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): at dalvik.system.NativeStart.main(Native Method)
10-11 00:55:49.930: ERROR/AndroidRuntime(32392): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist

最佳答案

你的openDataBase()方法是非常错误的。您不应提供数据库的路径。数据库会为您创建并在 onCreate() 方法中传递给您。如果删除 openDataBase() 方法并且不从 onCreate() 调用它,并在 中设置 this.db = db >onCreate(),您可能会遇到此错误。

有关在 Android 中使用数据库的更多帮助,请参阅此 Android Database Tutorial 。祝你好运!

关于java - _ID不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7719657/

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