我正在尝试从数据库中获取数据。我在Java类中做过吸气剂,然后将其发送给另一个Java类。外观如下:
DatabaseHelper
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
private static final String TABLE_NAME = "pushup_table";
private static final String COL1 = "ID";
private static final String COL2 = "number";
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT)";
db.execSQL(createTable);
}
public String getNumber(int i){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL2 + " FROM " + TABLE_NAME +
" WHERE " + COL1 + " = '" + i + "'";
Cursor data = db.rawQuery(query, null);
//String cursorString = data.getString( data.getColumnIndex(COL1));
String cursorString = data.getString(0);
return cursorString;
}}
test
public class test extends AppCompatActivity {
DatabaseHelper mDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ButterKnife.bind(this);
mDatabaseHelper = new DatabaseHelper(this);
}
@OnClick(R.id.button_data)
public void data(){
String numberFromDB = mDatabaseHelper.getNumber(1);
Toast.makeText(this,"number from db: " + numberFromDB, Toast.LENGTH_SHORT).show();
}
}
但是它给了我这些错误,而且什么也没发生(当我连接手机时)
E/MemoryLeakMonitorManager: MemoryLeakMonitor.jar is not exist!
E/Minikin: Could not get cmap table size!
E/vndksupport: Could not load vendor/lib64/egl/libGLES_mali_v2.so from sphal namespace: dlopen failed: library "vendor/lib64/egl/libGLES_mali_v2.so" not found.
我当然在数据库中也有一些数据。
当我在模拟器上尝试此操作时,显示了不同的错误:
07-14 19:20:56.702 9681-9681/majer.pompeczki E/AndroidRuntime: FATAL EXCEPTION: main
Process: majer.pompeczki, PID: 9681
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at majer.pompeczki.DatabaseHelper.getNumber(DatabaseHelper.java:81)
at majer.pompeczki.test.trening(test.java:68)
at majer.pompeczki.test_ViewBinding$3.doClick(test_ViewBinding.java:62)
at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)