-1

我不明白如何使用 sum(name_column) 返回查询结果我已经尝试了很多东西,但我没有看到。

你能告诉我为什么我有这些错误:

05-04 16:06:31.663: D/AndroidRuntime(2589): Shutting down VM
05-04 16:06:31.663: W/dalvikvm(2589): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-04 16:06:31.703: E/AndroidRuntime(2589): FATAL EXCEPTION: main
05-04 16:06:31.703: E/AndroidRuntime(2589): java.lang.RuntimeException: Unable to start activity ComponentInfo{exercice.thenewboston/exercice.thenewboston.SQLView}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
05-04 16:06:31.703: E/AndroidRuntime(2589):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at android.os.Looper.loop(Looper.java:137)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at android.app.ActivityThread.main(ActivityThread.java:4424)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at java.lang.reflect.Method.invokeNative(Native Method)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at java.lang.reflect.Method.invoke(Method.java:511)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at dalvik.system.NativeStart.main(Native Method)
05-04 16:06:31.703: E/AndroidRuntime(2589): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
05-04 16:06:31.703: E/AndroidRuntime(2589):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at exercice.thenewboston.HotOrNot.getSolde(HotOrNot.java:126)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at exercice.thenewboston.SQLView.onCreate(SQLView.java:25)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at android.app.Activity.performCreate(Activity.java:4465)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-04 16:06:31.703: E/AndroidRuntime(2589):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-04 16:06:31.703: E/AndroidRuntime(2589):     ... 11 more

我的代码:

HotOrNot.java 的一部分

public String getData()   // This method works !
{
    String[] columns = new String[]{ KEY_DATE, KEY_MONTANT, KEY_DESCRIPTION};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iDate = c.getColumnIndex(KEY_DATE);
    int iMontant = c.getColumnIndex(KEY_MONTANT);
    int iDescription = c.getColumnIndex(KEY_DESCRIPTION);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
    {
        result = result + c.getString(iDate) + " " + c.getString(iMontant) + " " + c.getString(iDescription) + "\n";
    }

    // Cursor solde = ourDatabase.query(DATABASE_TABLE);

    return result;

}

public String getSolde(){

    String query = "SUM(id_operation) ";
    String[] otherColumns = new String[]{ query};
    Cursor cursorBidon = ourDatabase.query(DATABASE_TABLE, otherColumns, null, null, null, null, null);
    String otherResult = "";

    int iOperation = cursorBidon.getColumnIndex(KEY_ID);

    /*for (cursorBidon.moveToFirst(); !cursorBidon.isAfterLast(); cursorBidon.moveToNext())
    {*/
        otherResult = otherResult + cursorBidon.getString(iOperation) + "\n";
    /*}*/

    return otherResult;
    /*String query = "select SUM(id_operation) from Operations";
    String mysolde = "";
    Cursor cursorBidon = ourDatabase.rawQuery(query,null);  
    int iOperation = cursorBidon.getColumnIndex(KEY_ID);

    mysolde = cursorBidon.getString(iOperation);
    return mysolde;*/


    }

谢谢

巴拉克解的错误

05-04 18:52:01.378: D/gralloc_goldfish(561): Emulator without GPU emulation detected.
05-04 18:52:01.378: I/dalvikvm(561): Wrote stack traces to '/data/anr/traces.txt'
05-04 18:53:22.768: E/CursorWindow(561): Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 1 columns.
05-04 18:53:22.798: D/AndroidRuntime(561): Shutting down VM
05-04 18:53:22.798: I/dalvikvm(561): threadid=3: reacting to signal 3
05-04 18:53:22.818: I/dalvikvm(561): Wrote stack traces to '/data/anr/traces.txt'
05-04 18:53:22.818: W/dalvikvm(561): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-04 18:53:22.899: E/AndroidRuntime(561): FATAL EXCEPTION: main
05-04 18:53:22.899: E/AndroidRuntime(561): java.lang.RuntimeException: Unable to start activity ComponentInfo{exercice.thenewboston/exercice.thenewboston.SQLView}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
05-04 18:53:22.899: E/AndroidRuntime(561):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-04 18:53:22.899: E/AndroidRuntime(561):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-04 18:53:22.899: E/AndroidRuntime(561):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-04 18:53:22.899: E/AndroidRuntime(561):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-04 18:53:22.899: E/AndroidRuntime(561):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 18:53:22.899: E/AndroidRuntime(561):  at android.os.Looper.loop(Looper.java:137)
05-04 18:53:22.899: E/AndroidRuntime(561):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-04 18:53:22.899: E/AndroidRuntime(561):  at java.lang.reflect.Method.invokeNative(Native Method)
05-04 18:53:22.899: E/AndroidRuntime(561):  at java.lang.reflect.Method.invoke(Method.java:511)
05-04 18:53:22.899: E/AndroidRuntime(561):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-04 18:53:22.899: E/AndroidRuntime(561):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-04 18:53:22.899: E/AndroidRuntime(561):  at dalvik.system.NativeStart.main(Native Method)
05-04 18:53:22.899: E/AndroidRuntime(561): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
05-04 18:53:22.899: E/AndroidRuntime(561):  at android.database.CursorWindow.nativeGetString(Native Method)
05-04 18:53:22.899: E/AndroidRuntime(561):  at android.database.CursorWindow.getString(CursorWindow.java:442)
05-04 18:53:22.899: E/AndroidRuntime(561):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
05-04 18:53:22.899: E/AndroidRuntime(561):  at exercice.thenewboston.HotOrNot.getSolde(HotOrNot.java:126)
05-04 18:53:22.899: E/AndroidRuntime(561):  at exercice.thenewboston.SQLView.onCreate(SQLView.java:25)
05-04 18:53:22.899: E/AndroidRuntime(561):  at android.app.Activity.performCreate(Activity.java:4465)
05-04 18:53:22.899: E/AndroidRuntime(561):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-04 18:53:22.899: E/AndroidRuntime(561):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-04 18:53:22.899: E/AndroidRuntime(561):  ... 11 more

**

编辑 2:

** 事实上,在你举这个例子之前,我已经很好地理解了。我在“return iOperation;”之后添加了这一行 并在我的其他 java 中更改 int 类型。Barack 选项 1 的结果

05-05 08:02:57.515: E/AndroidRuntime(539): java.lang.RuntimeException: Unable to start activity ComponentInfo{exercice.thenewboston/exercice.thenewboston.SQLView}: android.content.res.Resources$NotFoundException: String resource ID #0x334
05-05 08:02:57.515: E/AndroidRuntime(539):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-05 08:02:57.515: E/AndroidRuntime(539):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-05 08:02:57.515: E/AndroidRuntime(539):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-05 08:02:57.515: E/AndroidRuntime(539):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-05 08:02:57.515: E/AndroidRuntime(539):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 08:02:57.515: E/AndroidRuntime(539):  at android.os.Looper.loop(Looper.java:137)
05-05 08:02:57.515: E/AndroidRuntime(539):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-05 08:02:57.515: E/AndroidRuntime(539):  at java.lang.reflect.Method.invokeNative(Native Method)
05-05 08:02:57.515: E/AndroidRuntime(539):  at java.lang.reflect.Method.invoke(Method.java:511)
05-05 08:02:57.515: E/AndroidRuntime(539):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-05 08:02:57.515: E/AndroidRuntime(539):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-05 08:02:57.515: E/AndroidRuntime(539):  at dalvik.system.NativeStart.main(Native Method)
05-05 08:02:57.515: E/AndroidRuntime(539): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x334
05-05 08:02:57.515: E/AndroidRuntime(539):  at android.content.res.Resources.getText(Resources.java:247)
05-05 08:02:57.515: E/AndroidRuntime(539):  at android.widget.TextView.setText(TextView.java:3473)
05-05 08:02:57.515: E/AndroidRuntime(539):  at exercice.thenewboston.SQLView.onCreate(SQLView.java:27)
05-05 08:02:57.515: E/AndroidRuntime(539):  at android.app.Activity.performCreate(Activity.java:4465)
05-05 08:02:57.515: E/AndroidRuntime(539):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-05 08:02:57.515: E/AndroidRuntime(539):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-05 08:02:57.515: E/AndroidRuntime(539):  ... 11 more

选项 2 相同的错误但其他资源。

05-05 08:12:28.331: E/AndroidRuntime(754): java.lang.RuntimeException: Unable to start activity ComponentInfo{exercice.thenewboston/exercice.thenewboston.SQLView}: android.content.res.Resources$NotFoundException: String resource ID #0x0
05-05 08:12:28.331: E/AndroidRuntime(754):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-05 08:12:28.331: E/AndroidRuntime(754):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-05 08:12:28.331: E/AndroidRuntime(754):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-05 08:12:28.331: E/AndroidRuntime(754):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-05 08:12:28.331: E/AndroidRuntime(754):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 08:12:28.331: E/AndroidRuntime(754):  at android.os.Looper.loop(Looper.java:137)
05-05 08:12:28.331: E/AndroidRuntime(754):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-05 08:12:28.331: E/AndroidRuntime(754):  at java.lang.reflect.Method.invokeNative(Native Method)
05-05 08:12:28.331: E/AndroidRuntime(754):  at java.lang.reflect.Method.invoke(Method.java:511)
05-05 08:12:28.331: E/AndroidRuntime(754):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-05 08:12:28.331: E/AndroidRuntime(754):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-05 08:12:28.331: E/AndroidRuntime(754):  at dalvik.system.NativeStart.main(Native Method)
05-05 08:12:28.331: E/AndroidRuntime(754): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
05-05 08:12:28.331: E/AndroidRuntime(754):  at android.content.res.Resources.getText(Resources.java:247)
05-05 08:12:28.331: E/AndroidRuntime(754):  at android.widget.TextView.setText(TextView.java:3473)
05-05 08:12:28.331: E/AndroidRuntime(754):  at exercice.thenewboston.SQLView.onCreate(SQLView.java:27)
05-05 08:12:28.331: E/AndroidRuntime(754):  at android.app.Activity.performCreate(Activity.java:4465)
05-05 08:12:28.331: E/AndroidRuntime(754):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-05 08:12:28.331: E/AndroidRuntime(754):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-05 08:12:28.331: E/AndroidRuntime(754):  ... 11 more

SQLView.java (full) public class SQLView extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sqlview);
        TextView tv  = (TextView) findViewById(R.id.tvSQLinfo);
        TextView voirSolde = (TextView) findViewById(R.id.tvSQLsolde);
        HotOrNot info = new HotOrNot(this);
        info.open();
        String data = info.getData();
        info.close();
        tv.setText(data);

        HotOrNot infoSolde = new HotOrNot(this);
        infoSolde.open();
        int iOperation = infoSolde.getSolde();
        info.close();
        voirSolde.setText(iOperation);
    }
}

在您的代码之后的方法 getSolde 的全部部分(代码 2 的示例,我删除了 sting otherSolde,因为我已经全部更改为 int。)

public int getSolde(){

        String query = "SUM(id_operation) AS "+ KEY_ID;
        String[] otherColumns = new String[]{ query};
        Cursor cursorBidon = ourDatabase.query(DATABASE_TABLE, otherColumns, null, null, null, null, null);
        cursorBidon.moveToFirst();
        int iOperation = cursorBidon.getColumnIndex(KEY_ID);
        return iOperation;
4

1 回答 1

1

你的问题在这里:

int iOperation = cursorBidon.getColumnIndex(KEY_ID);

您正在访问游标而不移动到第一条记录。

cursorBidon.moveToFirst();

在对光标进行任何操作之前执行此操作,您的问题应该会消失。

编辑

您的另一个问题是您正在使用 引用该列KEY_ID,而您没有在光标中返回该列。

两种可能的解决方案:

1)当您尝试从中提取数据时,按数字(或在本例中为 0)引用列。

2) 使用 SQL 函数 AS 返回列。基本上附加“... AS” + KEY_ID; 对您的查询

编辑 2

演示我上面提到的两个选项的示例:

选项1

String query = "SUM(id_operation) ";
String[] otherColumns = new String[]{ query};
String otherResult = "";
Cursor cursorBidon = ourDatabase.query(DATABASE_TABLE, otherColumns, null, null, null, null, null);
cursorBidon.moveToFirst();
int iOperation = cursorBidon.getInt(0);

选项 2

String query = "SUM(id_operation) AS " + KEY_ID;
String[] otherColumns = new String[]{ query};
String otherResult = "";
Cursor cursorBidon = ourDatabase.query(DATABASE_TABLE, otherColumns, null, null, null, null, null);
cursorBidon.moveToFirst();
int iOperation = cursorBidon.getColumnIndex(KEY_ID);

就个人而言,我会选择选项1。

于 2012-05-04T17:08:06.820 回答