需要帮助从Android的SQLite数据库中读取

时间:2021-11-14 02:02:09

I have read through many sites and tutorials and have been unable to solve my problem. I have an application that works until I try to read from my database (it opens it just fine). I know its not the right way to do things but I have two global strings that I pass from my main activity to my DataBaseHelper class (DB_TABLE and spinnerPipeLengthText). When I call the following line of code to retrieve the data from all the columns in a specific row my application crashes.

我已阅读了许多网站和教程,但无法解决我的问题。我有一个应用程序,直到我尝试从我的数据库读取它(它打开它就好了)。我知道它不是正确的做事方式,但我有两个全局字符串,我从主活动传递到我的DataBaseHelper类(DB_TABLE和spinnerPipeLengthText)。当我调用以下代码行来检索特定行中所有列的数据时,我的应用程序崩溃了。

Cursor cursorData = myDataBase.query(CSSTPipeSizingActivity.DB_TABLE, null, 
            CSSTPipeSizingActivity.spinnerPipeLengthText, null, null, null, null, null);

Here is my complete getData method:

这是我完整的getData方法:

public Cursor getData() {

    Cursor cursorData = myDataBase.query(CSSTPipeSizingActivity.DB_TABLE, null, 
            CSSTPipeSizingActivity.spinnerPipeLengthText, null, null, null, null, null);
    if (cursorData != null)
        cursorData.moveToFirst();

    cursorData.moveToFirst();
    CSSTPipeSizingActivity.textViewSize15.setText(cursorData.getString(1));
    cursorData.moveToNext();        
    CSSTPipeSizingActivity.textViewSize19.setText(cursorData.getString(1));
    cursorData.moveToFirst();
    CSSTPipeSizingActivity.textViewSize25.setText(cursorData.getString(1));
    cursorData.moveToNext();        
    CSSTPipeSizingActivity.textViewSize31.setText(cursorData.getString(1));
    cursorData.moveToFirst();
    CSSTPipeSizingActivity.textViewSize37.setText(cursorData.getString(1));
    cursorData.moveToNext();        
    CSSTPipeSizingActivity.textViewSize46.setText(cursorData.getString(1));
    cursorData.moveToNext();        
    CSSTPipeSizingActivity.textViewSize62.setText(cursorData.getString(1));

    cursorData.close();
    return cursorData;
} // end method getData

As you can see from my code, I am trying to take the data and input it straight into text views in the main.xml file. I don't know if this is possible but that will be for me to figure out later. Right now I need to get the data.

从我的代码中可以看出,我正在尝试获取数据并将其直接输入到main.xml文件中的文本视图中。我不知道这是否可能,但我将在稍后解决。现在我需要获取数据。

Here is my logcat:

这是我的logcat:

03-12 19:23:33.587: D/AndroidRuntime(20191): Shutting down VM
03-12 19:23:33.587: W/dalvikvm(20191): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-12 19:23:33.617: E/AndroidRuntime(20191): FATAL EXCEPTION: main
03-12 19:23:33.617: E/AndroidRuntime(20191): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skyesmechanical.CSSTPipeSizing/com.skyesmechanical.CSSTPipeSizing.CSSTPipeSizingActivity}: java.lang.NullPointerException
03-12 19:23:33.617: E/AndroidRuntime(20191):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-12 19:23:33.617: E/AndroidRuntime(20191):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-12 19:23:33.617: E/AndroidRuntime(20191):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-12 19:23:33.617: E/AndroidRuntime(20191):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-12 19:23:33.617: E/AndroidRuntime(20191):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 19:23:33.617: E/AndroidRuntime(20191):    at android.os.Looper.loop(Looper.java:123)
03-12 19:23:33.617: E/AndroidRuntime(20191):    at android.app.ActivityThread.main(ActivityThread.java:3683)
03-12 19:23:33.617: E/AndroidRuntime(20191):    at java.lang.reflect.Method.invokeNative(Native Method)
03-12 19:23:33.617: E/AndroidRuntime(20191):    at java.lang.reflect.Method.invoke(Method.java:507)
03-12 19:23:33.617: E/AndroidRuntime(20191):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-12 19:23:33.617: E/AndroidRuntime(20191):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-12 19:23:33.617: E/AndroidRuntime(20191):    at dalvik.system.NativeStart.main(Native Method)
03-12 19:23:33.617: E/AndroidRuntime(20191): Caused by: java.lang.NullPointerException
03-12 19:23:33.617: E/AndroidRuntime(20191):    at com.skyesmechanical.CSSTPipeSizing.DataBaseHelper.getData(DataBaseHelper.java:173)
03-12 19:23:33.617: E/AndroidRuntime(20191):    at com.skyesmechanical.CSSTPipeSizing.CSSTPipeSizingActivity.onCreate(CSSTPipeSizingActivity.java:105)
03-12 19:23:33.617: E/AndroidRuntime(20191):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-12 19:23:33.617: E/AndroidRuntime(20191):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-12 19:23:33.617: E/AndroidRuntime(20191):    ... 11 more

Thanks for your help!

谢谢你的帮助!

3 个解决方案

#1


1  

Well, you' re closing your cursor and then trying to return it... not gonna work too well that way. Either close the cursor and return nothing or return the cursor.

好吧,你正在关闭你的光标,然后试图返回它......不会那样做得那么好。关闭光标并返回任何内容或返回光标。

You also keep iterating over the first couple entries when you do movetofirst, movetonext, then movetofirst again.

当你执行movetofirst,movetonext,然后再次移动时,你也会继续迭代前几个条目。

I also believe your getString needs a little help.

我也相信你的getString需要一些帮助。

Here's how I go about it:

这是我如何去做的:

titleTxt.setText(eventedit.getString(eventedit
    .getColumnIndexOrThrow(AttendanceDB.EVENT_NAME)));

titleTxt is a TextView reference I set earlier.
eventEdit is the cursor I'm wanting to get the info from (and created earlier)
AttendanceDB is my DB adapter class
EVENT_NAME is the column name variable I set in my DB adapter class

titleTxt是我之前设置的TextView参考。 eventEdit是我想要从中获取信息的游标(并在之前创建)AttendanceDB是我的数据库适配器类EVENT_NAME是我在数据库适配器类中设置的列名变量

EDIT

Ok, now that I re-read your question, I realize I was answering the wrong portion. The problem is your query is messed up. I think it should be more like this:

好的,现在我重新阅读了你的问题,我意识到我正在回答错误的部分。问题是你的查询搞砸了。我认为应该更像这样:

Cursor cursorData = myDataBase.query(CSSTPipeSizingActivity.DB_TABLE, null,  
    COLUMN_ID + "=" + CSSTPipeSizingActivity.spinnerPipeLengthText, null, null, null, null, null);                           

You need to tell the query what column to search for the info you want (CSSTPipeSizingActivity.spinnerPipeLengthText). So replace COLUMN_ID with your column name or the variable for it and you should be good to go.

您需要告诉查询要搜索所需信息的列(CSSTPipeSizingActivity.spinnerPipeLengthText)。所以用您的列名或变量替换COLUMN_ID,你应该好好去。

You really should review the info here, especially the various query method and their required parameters.

你真的应该在这里查看信息,特别是各种查询方法及其所需的参数。

#2


0  

You can see from your logcat that the error is a NullPointerException at line 173. Without line numbers in your code, it's hard to know exactly what the issue is in this case, but my guess is that line 173 is the cursorData.moveToFirst() that isn't in the if-statement.

您可以从logcat看到错误是第173行的NullPointerException。在代码中没有行号,在这种情况下很难确切知道问题是什么,但我的猜测是第173行是cursorData.moveToFirst()那不在if语句中。

Consider the case where cursorData is null -

考虑cursorData为null的情况 -

You check that cursorData is not null, and if that's the case, you call moveToFirst(). But then you call moveToFirst() in the next line; this will happen whether cursorData is null or not.

检查cursorData是否为null,如果是这种情况,则调用moveToFirst()。但是你在下一行调用moveToFirst();无论cursorData是否为null,都会发生这种情况。

A common design pattern would be for getData to simply return the cursor, and for the calling method to handle the extraction. This could look something like:

一个常见的设计模式是getData简单地返回游标,以及调用方法来处理提取。这看起来像是这样的:

{ ... if (cursorData != null) cursorData.moveToFirst(); return cursorData; }

{... if(cursorData!= null)cursorData.moveToFirst(); return cursorData; }

I hope this helps!

我希望这有帮助!

#3


0  

Firstly, think you can put all your code using Cursor in IF statement:

首先,假设您可以在IF语句中使用Cursor放置所有代码:

    if (cursorData != null){
        cursorData.moveToFirst();
        CSSTPipeSizingActivity.textViewSize15.setText(cursorData.getString(1));
        cursorData.moveToNext();        
        CSSTPipeSizingActivity.textViewSize19.setText(cursorData.getString(1));
        cursorData.moveToFirst();
        CSSTPipeSizingActivity.textViewSize25.setText(cursorData.getString(1));
        cursorData.moveToNext();        
        CSSTPipeSizingActivity.textViewSize31.setText(cursorData.getString(1));
        cursorData.moveToFirst();
        CSSTPipeSizingActivity.textViewSize37.setText(cursorData.getString(1));
        cursorData.moveToNext();        
        CSSTPipeSizingActivity.textViewSize46.setText(cursorData.getString(1));
        cursorData.moveToNext();        
        CSSTPipeSizingActivity.textViewSize62.setText(cursorData.getString(1));
        cursorData.close();
    }

Second, you may check below code b/c you move to next twice:

其次,你可以检查下面的代码b / c你移动到下一次两次:

cursorData.moveToNext();        
CSSTPipeSizingActivity.textViewSize46.setText(cursorData.getString(1));
cursorData.moveToNext();        
CSSTPipeSizingActivity.textViewSize62.setText(cursorData.getString(1));

Finally, use your setText with a String casting for all setText method:

最后,对所有setText方法使用带有String转换的setText:

CSSTPipeSizingActivity.textViewSize15.setText("" + cursorData.getString(1));

#1


1  

Well, you' re closing your cursor and then trying to return it... not gonna work too well that way. Either close the cursor and return nothing or return the cursor.

好吧,你正在关闭你的光标,然后试图返回它......不会那样做得那么好。关闭光标并返回任何内容或返回光标。

You also keep iterating over the first couple entries when you do movetofirst, movetonext, then movetofirst again.

当你执行movetofirst,movetonext,然后再次移动时,你也会继续迭代前几个条目。

I also believe your getString needs a little help.

我也相信你的getString需要一些帮助。

Here's how I go about it:

这是我如何去做的:

titleTxt.setText(eventedit.getString(eventedit
    .getColumnIndexOrThrow(AttendanceDB.EVENT_NAME)));

titleTxt is a TextView reference I set earlier.
eventEdit is the cursor I'm wanting to get the info from (and created earlier)
AttendanceDB is my DB adapter class
EVENT_NAME is the column name variable I set in my DB adapter class

titleTxt是我之前设置的TextView参考。 eventEdit是我想要从中获取信息的游标(并在之前创建)AttendanceDB是我的数据库适配器类EVENT_NAME是我在数据库适配器类中设置的列名变量

EDIT

Ok, now that I re-read your question, I realize I was answering the wrong portion. The problem is your query is messed up. I think it should be more like this:

好的,现在我重新阅读了你的问题,我意识到我正在回答错误的部分。问题是你的查询搞砸了。我认为应该更像这样:

Cursor cursorData = myDataBase.query(CSSTPipeSizingActivity.DB_TABLE, null,  
    COLUMN_ID + "=" + CSSTPipeSizingActivity.spinnerPipeLengthText, null, null, null, null, null);                           

You need to tell the query what column to search for the info you want (CSSTPipeSizingActivity.spinnerPipeLengthText). So replace COLUMN_ID with your column name or the variable for it and you should be good to go.

您需要告诉查询要搜索所需信息的列(CSSTPipeSizingActivity.spinnerPipeLengthText)。所以用您的列名或变量替换COLUMN_ID,你应该好好去。

You really should review the info here, especially the various query method and their required parameters.

你真的应该在这里查看信息,特别是各种查询方法及其所需的参数。

#2


0  

You can see from your logcat that the error is a NullPointerException at line 173. Without line numbers in your code, it's hard to know exactly what the issue is in this case, but my guess is that line 173 is the cursorData.moveToFirst() that isn't in the if-statement.

您可以从logcat看到错误是第173行的NullPointerException。在代码中没有行号,在这种情况下很难确切知道问题是什么,但我的猜测是第173行是cursorData.moveToFirst()那不在if语句中。

Consider the case where cursorData is null -

考虑cursorData为null的情况 -

You check that cursorData is not null, and if that's the case, you call moveToFirst(). But then you call moveToFirst() in the next line; this will happen whether cursorData is null or not.

检查cursorData是否为null,如果是这种情况,则调用moveToFirst()。但是你在下一行调用moveToFirst();无论cursorData是否为null,都会发生这种情况。

A common design pattern would be for getData to simply return the cursor, and for the calling method to handle the extraction. This could look something like:

一个常见的设计模式是getData简单地返回游标,以及调用方法来处理提取。这看起来像是这样的:

{ ... if (cursorData != null) cursorData.moveToFirst(); return cursorData; }

{... if(cursorData!= null)cursorData.moveToFirst(); return cursorData; }

I hope this helps!

我希望这有帮助!

#3


0  

Firstly, think you can put all your code using Cursor in IF statement:

首先,假设您可以在IF语句中使用Cursor放置所有代码:

    if (cursorData != null){
        cursorData.moveToFirst();
        CSSTPipeSizingActivity.textViewSize15.setText(cursorData.getString(1));
        cursorData.moveToNext();        
        CSSTPipeSizingActivity.textViewSize19.setText(cursorData.getString(1));
        cursorData.moveToFirst();
        CSSTPipeSizingActivity.textViewSize25.setText(cursorData.getString(1));
        cursorData.moveToNext();        
        CSSTPipeSizingActivity.textViewSize31.setText(cursorData.getString(1));
        cursorData.moveToFirst();
        CSSTPipeSizingActivity.textViewSize37.setText(cursorData.getString(1));
        cursorData.moveToNext();        
        CSSTPipeSizingActivity.textViewSize46.setText(cursorData.getString(1));
        cursorData.moveToNext();        
        CSSTPipeSizingActivity.textViewSize62.setText(cursorData.getString(1));
        cursorData.close();
    }

Second, you may check below code b/c you move to next twice:

其次,你可以检查下面的代码b / c你移动到下一次两次:

cursorData.moveToNext();        
CSSTPipeSizingActivity.textViewSize46.setText(cursorData.getString(1));
cursorData.moveToNext();        
CSSTPipeSizingActivity.textViewSize62.setText(cursorData.getString(1));

Finally, use your setText with a String casting for all setText method:

最后,对所有setText方法使用带有String转换的setText:

CSSTPipeSizingActivity.textViewSize15.setText("" + cursorData.getString(1));