如何将单个字段的数据获取到 SQLite 数据库的变量中?
我正在开发我的第一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信,当
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), callcursor.moveToFirst()
before you get the float. I've been bitten by that before.你可以获得像这样的数据
You can get data like