如何将单个字段的数据获取到 SQLite 数据库的变量中?

发布于 2024-10-12 01:35:18 字数 1541 浏览 5 评论 0原文

我正在开发我的第一个 Android 应用程序,并设置了一个 SQLite 数据库并使用它,我已经能够成功使用游标来检索一些数据,并使用 SimpleCursorAdapter 使数据显示为列表。

我现在希望检索特定字段将结果放入浮点变量。然而,我在网上找到的所有示例似乎都希望您能够处理具有多行/列的大型检索,并且它们都已改编为列表或数组。 我不知道如何将一个字段的内容放入变量中。

我尝试使用myVariable = cursor.getFloat(0),我认为这是沿着正确的线路,但我也无法让它工作。

每次我运行我的应用程序时,我都会遇到空指针异常错误,我尝试了一些try catch块,但我对这一切都很陌生,而且还没有尝试过。在故障排除方面是否取得了成功。这可能是一个简单的查询打字错误。

我将在下面包含一些涉及的代码,但欢迎任何一般或具体的建议,感谢您花时间查看。

public void Main(){

    //gets the rowId we put with the intent from Budget.java, sets a default value of -1 'incase.
    selectedRowId = getIntent().getLongExtra("rowId", -1);
    Cursor costCursor= FindBudgetForId(selectedRowId);
    MyText(costCursor, selectedRowId);

}

public Cursor FindBudgetForId(long selectedRowId){

    SQLiteDatabase db = budgetData.getWritableDatabase();
    String rowBudgetQuery = "SELECT BUDGET_AMOUNT FROM CAT_BUD_TAB WHERE _ID="+ selectedRowId;
    Cursor c = db.rawQuery(rowBudgetQuery,null);
    startManagingCursor(c); 
    return c;


}

public void MyText(Cursor costCursor, long selectedRowId){      
    TextView whatDoText=(TextView)this.findViewById(R.id.keypad_text);
    whatDoText.setText("row: " +(String.valueOf(selectedRowId)));
    //set the current  budget amount in the edit text box
    retrievedAmount = costCursor.getFloat(3);
    EditText inputHint=(EditText)this.findViewById(R.id.cost_input);
    inputHint.setText((String.valueOf(retrievedAmount)));

}

I'm working on my first android app and have an SQLite database set up and working with it, I've been able to successfully use a cursor to retrieve some data and used SimpleCursorAdapter to make the data appear as a list.

I'm now looking to retrieve a specific field and put the result into a float variable. However all the examples i find online seem to expect you to be handling large retrievals with multiple rows/columns and they've all been adapted into lists or arrays. I can't figure out how to just get the contents of that one field into a variable.

I tried using myVariable = cursor.getFloat(0) which i think is along the right lines but i couldn't get that working either.

Every time i run my application i just get null pointer exception errors, I've tried some try catch blocks but I'm new to all this in general and haven't had any success in troubleshooting. It might be something a simple as a query typo..

I'll include the bit of code involved below, but any suggestions general or specific are welcome, Thanks for taking the time to look.

public void Main(){

    //gets the rowId we put with the intent from Budget.java, sets a default value of -1 'incase.
    selectedRowId = getIntent().getLongExtra("rowId", -1);
    Cursor costCursor= FindBudgetForId(selectedRowId);
    MyText(costCursor, selectedRowId);

}

public Cursor FindBudgetForId(long selectedRowId){

    SQLiteDatabase db = budgetData.getWritableDatabase();
    String rowBudgetQuery = "SELECT BUDGET_AMOUNT FROM CAT_BUD_TAB WHERE _ID="+ selectedRowId;
    Cursor c = db.rawQuery(rowBudgetQuery,null);
    startManagingCursor(c); 
    return c;


}

public void MyText(Cursor costCursor, long selectedRowId){      
    TextView whatDoText=(TextView)this.findViewById(R.id.keypad_text);
    whatDoText.setText("row: " +(String.valueOf(selectedRowId)));
    //set the current  budget amount in the edit text box
    retrievedAmount = costCursor.getFloat(3);
    EditText inputHint=(EditText)this.findViewById(R.id.cost_input);
    inputHint.setText((String.valueOf(retrievedAmount)));

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杯敬自由 2024-10-19 01:35:18

我相信,当 SqliteDatabase#rawQuery 返回 Cursor 时,它最初位于第一个结果之前。因此,为了实际从获得的结果中读取值(而不获取 NullPointerExceptionExplosionDeath),请在获取浮点数之前调用 cursor.moveToFirst() 。我以前也被这个咬过。

I believe that when SqliteDatabase#rawQuery returns a Cursor, it is initially positioned before the first result. So, in order to actually read a value from the result you get (without getting NullPointerExceptionExplosionDeath), call cursor.moveToFirst() before you get the float. I've been bitten by that before.

谈情不如逗狗 2024-10-19 01:35:18

你可以获得像这样的数据

    Cursor c =//Query;
    int numRows = c.getCount();
if (numRows > 0) 
    {
    c.moveToFirst();
    While(numRows>0) // or for loop
    {
          String strName = c.getString(0);
              Int i = c.getInt(1);
              numRows--;
    }       
    }

You can get data like

    Cursor c =//Query;
    int numRows = c.getCount();
if (numRows > 0) 
    {
    c.moveToFirst();
    While(numRows>0) // or for loop
    {
          String strName = c.getString(0);
              Int i = c.getInt(1);
              numRows--;
    }       
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文