从在另一个活动中创建的数据库读取 - 所有步骤 [英] Read from a database created in another activity - all steps
问题描述
我已经设法创建数据库并具有保存数据的方法。那是我的第二个活动(从第一个活动开始)。我想在第一个活动中打开该数据库,并从中读取数据(如果现在有任何数据)。
所有步骤和事情我必须做什么? / p>
我尝试从第二个活动导入SQLiteOpenHelper类并以这种方式读取,但没有结果。此外,尝试将完全相同的SQLiteOpenHelper放在第一和第二个活动上,这也是不对的。
编辑:
这是我的代码创建和第二个活动的保存数据库:
Android应用程序崩溃调用SQLiteHelper
编辑[2]:
我已经尝试过这样:
我的第二个活动(在SQLiteDbHelper内):
public String getSP(){
String sp;
String [] projection = {
SQLiteDbHelper.COLUMN_NAME_SP,SQLiteDbHelper.COLUMN_NAME_ST};
游标cursor = null;
cursor = this.getReadableDatabase()。query(SQLiteDbHelper.TABLE_NAME,projection,null,null,null,null,null);
if(cursor!= null){
if(cursor.moveToFirst()){
sp = cursor.getString(cursor.getColumnIndexOrThrow(SQLiteDbHelper.COLUMN_NAME_SP));
返回sp;
}
}
cursor.close();
return - ;
}
而且,在我的第一个活动中,我需要数据库的结果:
String sp = SQLiteDbHelper.getSP();
但是,它导致错误:无法静态引用非静态方法
我该怎么办?
完整正确的答案将是:
import myprojectname.ActivityB.SQLiteDbHelper;
然后使用:
SQLiteDbHelper db = new SQLiteDbHelper(this);
String sp = SQLiteDbHelper.getSP();
感谢大家分享他们的想法,他们帮助我得到这个!
I have managed to create database and have methods for saving data. That is in my 2nd activity (being opened from 1st activity). I want to open that database in 1st activity and read from it (if there are any data there now).
What are all steps and things I have to do?
I have tried importing SQLiteOpenHelper class from 2nd activity and reading that way, but no results. Also, tried placing completely the same SQLiteOpenHelper, both on 1st and 2nd activity, and that's not right, too.
EDIT: Here's my code for creating and saving database which is in 2nd activity: Android app crashing on calling SQLiteHelper
EDIT [2]: I have tried sth like this: My 2nd activity (inside SQLiteDbHelper):
public String getSP(){
String sp;
String[] projection = {
SQLiteDbHelper.COLUMN_NAME_SP, SQLiteDbHelper.COLUMN_NAME_ST};
Cursor cursor = null;
cursor = this.getReadableDatabase().query(SQLiteDbHelper.TABLE_NAME, projection, null, null, null, null, null);
if(cursor != null){
if(cursor.moveToFirst()){
sp = cursor.getString(cursor.getColumnIndexOrThrow(SQLiteDbHelper.COLUMN_NAME_SP));
return sp;
}
}
cursor.close();
return " - ";
}
And, in my 1st activity, where I need the result from the database:
String sp = SQLiteDbHelper.getSP();
but, it0s causing the error: Cannot make a static reference to the non-static method
What should I do?
The complete and correct answer would be this:
import myprojectname.ActivityB.SQLiteDbHelper;
and then use:
SQLiteDbHelper db = new SQLiteDbHelper(this);
String sp = SQLiteDbHelper.getSP();
Thanks for everyone sharing their ideas, they helped me to get to this!
这篇关于从在另一个活动中创建的数据库读取 - 所有步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!