-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:

**実際、例を示す前に、それは私がよく理解していることでした。「returniOperation;」の後に行を追加しました。他のJavaをint型に変更します。バラクのオプション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(フル)パブリッククラスSQLViewは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の例では、すべてintで変更されたため、sting otherSoldeを削除しました)。

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();

カーソルで何かをする前にそれをしてください。そうすれば問題はなくなるはずです。

編集

もう1つの問題はKEY_ID、カーソルで返されていないを使用して列を参照していることです。

2つの可能な解決策:

1)列からデータをプルしようとするときは、列を番号(またはこの場合は0)で参照します。

2)SQL関数ASを使用して列を返します。基本的に「...AS」+KEY_IDを追加します。あなたの質問に

編集2

上で述べた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 に答える