我想让我的代码干净整洁简单,所以我想创建数据库,但我想通过主要活动从另一个类调用它...我试图这样做但它没有用......所以,它应该如何工作?
注意:我是Android开发中的新手。所以,对不起这类问题...
这是主要活动:
package com.DataStorage.Excercise;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
public class DataStorageActivity extends Activity {
private Context context;
public SQLiteDatabase db_1=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AccessingDB acc= new AccessingDB(context);
acc.onCreate(db_1);
}
}
具有DB代码的另一个类..:
package com.DataStorage.Excercise;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class AccessingDB extends SQLiteOpenHelper {
public AccessingDB(Context context) {
super(context, "Test_1", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CreateTable());
db.execSQL(Insert_1());
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
/*
public String CreateDB_1(){
String s1= "create database Test_1;";
return s1;
}
*/
private String CreateTable(){
String s2="create table Customer(id_1 INTEGER,fn TEXT,ln TEXt);";
return s2;
}
private String Insert_1(){
String s3="insert into Customer values(1,'aaa','bbb');";
return s3;
}
}
非常感谢...
答案 0 :(得分:0)
如果您在MainActivity中调用数据库,是否只能在其他活动中调用MainActivity,然后使用Main调用数据库?像这样:
在您的MainActivity中:
Database db;
在您的其他活动中:
MainActivity main;
main.db.somequery; //call the method you want
或者我是否误解了你需要别的东西?