eclipse 从另一个活动中创建的数据库读取 - 所有步骤
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15856716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read from a database created in another activity - all steps
提问by cgv
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.
我尝试从第二个活动中导入 SQLiteOpenHelper 类并以这种方式阅读,但没有结果。此外,尝试在第一个和第二个活动中放置完全相同的 SQLiteOpenHelper,这也是不对的。
EDIT: Here's my code for creating and saving database which is in 2nd activity: Android app crashing on calling SQLiteHelper
编辑:这是我在第二个活动中创建和保存数据库的代码: Android 应用程序在调用 SQLiteHelper 时崩溃
EDIT [2]: I have tried sth like this: My 2nd activity (inside SQLiteDbHelper):
编辑 [2]:我尝试过这样的事情:我的第二个活动(在 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?
我该怎么办?
回答by Code-Apprentice
In my own Android app, I created a class which extends SQLiteOpenHelper. Any Activity can create an instance of this class in order to communicate with the database. You can see the complete code of my class hereif you want an example.
在我自己的 Android 应用程序中,我创建了一个扩展 SQLiteOpenHelper 的类。任何 Activity 都可以创建此类的实例,以便与数据库进行通信。如果需要示例,可以在此处查看我的课程的完整代码。
回答by WHDeveloper
Here, use this example from extending the SQLiteOpenHelper
在这里,使用这个扩展 SQLiteOpenHelper 的例子
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class YourSQLAdapter {
public static final String DBNAME = "filename.db";
public static final String TABLE = "tablename";
public static final int VERSION = 1;
public static final String KEY_ID = "_id";
// Add other fields here
private SQLiteDatabase sqLiteDatabase;
private SQLiteHelper sqLiteHelper;
private Context mContext;
//this is how I write the create db script, you may do it you own way;)
public static String a = "TEXT";
public static String b = "INTEGER";
public static String c = "PRIMARY KEY";
public static String d = "AUTOINCREMENT";
public static String e = " ";
public static String f = ",";
private static final String SCRIPT_CREATE_DATABASE =
"CREATE TABLE IF NOT EXISTS " + TABLE + e + "(" +
KEY_ID + e + b + e + c + e + d + f + e + ");";
public YourSQLAdapter(Context context){
mContext = context;
}
public void close() {
sqLiteHelper.close();
}
public int deleteAll() {
return sqLiteDatabase.delete(TABLE, null, null);
}
public long insert(String value) {
ContentValues cv = new ContentValues();
rooster.put("fieldname(or use its key value)", value);
return sqLiteDatabase.insert(TABLE, null, cv);
}
public YourSQLAdapter openToRead() throws SQLException {
try {
sqLiteHelper = new SQLiteHelper(mContext, DBNAME, null, 1);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
} catch (Exception e){}
return this;
}
public YourSQLAdapter openToWrite() throws SQLException {
try {
sqLiteHelper = new SQLiteHelper(mContext, DBNAME, null, 1);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
} catch (Exception e){}
return this;
}
public Cursor queueAll() {
String[] KEYS = { KEY_ID /* and all other KEYS*/ };
return sqLiteDatabase.query(TABLE, KEYS, null, null, null, null, null);
}
public Cursor queueDay(String query) {
String[] KEYS = { KEY_ID /* and all other KEYS*/ };
return sqLiteDatabase.query(TABLE, KEYS, query, null, null, null, null);
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SCRIPT_CREATE_DATABASE);
}
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {}
}
}
When you create the DB use
创建数据库时使用
YourSQLAdapter sql = new YourSQLAdapter(context);
sql.openToWrite();
and if you want to read from the DB use
如果你想从数据库中读取使用
YourSQLAdapter sql = new YourSQLAdapter(context);
sql.openToRead();
Warning, if the DB is not created yet, you get an exception on openToRead() because the SQLiteOpenHelper sees it does not exist yet and tries to create it(which cant because it can't write)
警告,如果 DB 尚未创建,则 openToRead() 会出现异常,因为 SQLiteOpenHelper 发现它尚不存在并尝试创建它(因为它无法写入而无法创建)
Hope this helps;)
希望这可以帮助;)
回答by WHDeveloper
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!
感谢大家分享他们的想法,他们帮助我做到了这一点!
回答by cgv
You're interacting with your db from an Activity, that isn't a good way to go about it. You should create a plain class file separate of either Activity that extends SQLiteOpenHelper and has public methods you create for the direct interactions of reading and writing to your database. A quick search shows some good examples of doing this.
您正在通过 Activity 与您的数据库进行交互,这不是一个好方法。您应该创建一个与扩展 SQLiteOpenHelper 的 Activity 分开的普通类文件,并具有您为读取和写入数据库的直接交互创建的公共方法。快速搜索显示了执行此操作的一些很好的示例。
Then anywhere you need to call data from it (like from either of your Activites) or write to db you'd only need something like...
然后在任何您需要从中调用数据的地方(例如从您的任何一个 Activites)或写入数据库,您只需要诸如...
MyDBClassName db = new MyDBClassName();
String someStoredValue = db.readFromTable(RowID-or-searchargs);
What methods are available to you (that is, db.readFromTable) would be determined by what methods you setup in this class file.
您可以使用哪些方法(即 db.readFromTable)取决于您在此类文件中设置的方法。