2014-03-02 64 views
0

我的數據庫類是DB.java如何從非數據庫類打開數據庫?

package com.example.pocketbooker; 

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.provider.BaseColumns; 

public class DB extends SQLiteOpenHelper implements BaseColumns{ 

    private static final String DATABASE_NAME = "pb_database.db"; 
    private static final int DATABASE_VERSION = 1; 


    public DB(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL("CREATE TABLE Members(_id INTEGER PRIMARY KEY AUTOINCREMENT, Mem_Name VARCHAR(50));"); 
     db.execSQL("CREATE TABLE Incomes(_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
     "Inc_Name VARCHAR(50), Inc_Sum VARCHAR(10), Inc_Mem VARCHAR (50), Inc_Change_Date DATE, " + 
       "Inc_Perbeg_Date DATE, Inc_Period VARCHAR (4));"); 
     db.execSQL("CREATE TABLE Outcomes(_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       "Out_Name VARCHAR(50), Out_Sum VARCHAR(10), Out_Mem VARCHAR (50), Out_Change_Date DATE, " + 
         "Out_Perbeg_Date DATE, Out_Period VARCHAR (4));"); 
     db.execSQL("CREATE TABLE Goals(_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       "Goal_Name VARCHAR(50), Goal_Sum VARCHAR(10), Goal_Mem VARCHAR (50), Goal_Change_Date DATE, " + 
         "Goal_Perbeg_Date DATE, Goal_Period VARCHAR (4));"); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) { 
     // TODO Auto-generated method stub 

    } 

} 

我的 「其他」 類Dialog_mem.java

package com.example.pocketbooker; 

import android.annotation.SuppressLint; 
import android.app.DialogFragment; 
import android.content.ContentValues; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.EditText; 
import android.widget.Toast; 

@SuppressLint({ "NewApi", "ValidFragment" }) 
public class Dialog_mem extends DialogFragment implements OnClickListener { 
    EditText memname; 

    ContentValues cv = new ContentValues(); 
    private SQLiteDatabase database; 
    private DB dbHelper; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    getDialog().setTitle("Добавить Члена Семьи"); 

    View v = inflater.inflate(R.layout.mem_dialog, null); 

    v.findViewById(R.id.mem_btnOK).setOnClickListener(this); 
    v.findViewById(R.id.mem_btnCancel).setOnClickListener(this); 
    memname=(EditText) v.findViewById(R.id.mem_name); 

    return v; 

    } 


public void onClick(View v) { 

     switch(v.getId()) 
     { case R.id.mem_btnOK: 
      database = dbHelper.getWritableDatabase(); 
      cv.put("Mem_Name", memname.getText().toString()); 
      database.insert("Members", "Mem_Name", cv); 
      database.close(); 

      Toast.makeText(getActivity(), "Добавлено", Toast.LENGTH_SHORT).show(); 
     default: 
      dismiss();} 

    } 

    public void onDismiss(DialogInterface dialog) { 
    super.onDismiss(dialog); 

    } 

    public void onCancel(DialogInterface dialog) { 
    super.onCancel(dialog); 

    } 

} 

的Eclipse顯示NullPointerException。我想這是上下文錯誤,但我不知道如何指出所需的上下文。 getActivity()是錯誤的。 「這個也是。 getApplicationContext()根本不起作用。

+0

哪一行是拋出NullPointerException?你永遠不應該在Fragment類中打開數據庫。相反,將您的數據庫處理方法移出到一個新的DatabaseUtils類或類似的名稱。這樣,您所有的數據庫訪問都將在一個類中進行,這將使測試變得更加容易,並將數據庫從界面代碼中分離出來。 – SDJMcHattie

+0

閱讀並使用ContentProvider – pskink

回答

3

您需要初始化dbHelper實例。 你可以做的是
1.爲所有數據庫操作創建一個單例類。
2.創建一個數據源類。通過它你將訪問數據庫。
3.在數據源類中添加open()方法

/** 
* Open the Database 
* @throws SQLException 
*/ 
public void open() throws SQLException { 
    if (dbHelper != null) { 
     db = dbHelper.getWritableDatabase(); 
    } 
} 
/** 
* Close 
*/ 
public void close() { 
    dbHelper.close(); 
} 


4.寫,你需要存取權限的DataSource類數據庫和調用數據庫操作這個數據庫實例的方法。

1

那麼,我用akashsr的答案(謝謝你,男人,這個想法)。如果任何人有興趣,我是如何做到的(最後)是代碼。

DB.java

package com.example.pocketbooker; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.provider.BaseColumns; 

public class DB extends SQLiteOpenHelper implements BaseColumns{ 

    private static final String DATABASE_NAME = "pb_database.db"; 
    private static final int DATABASE_VERSION = 1; 
    private static DB sInstance; 
    ContentValues cv; 
    public DB(Context context) { 

     super(context, DATABASE_NAME, null, DATABASE_VERSION);} 



    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL("CREATE TABLE Members(_id INTEGER PRIMARY KEY AUTOINCREMENT, Mem_Name VARCHAR(50));"); 
     db.execSQL("CREATE TABLE Incomes(_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
     "Inc_Name VARCHAR(50), Inc_Sum VARCHAR(10), Inc_Mem VARCHAR (50), Inc_Change_Date DATE, " + 
       "Inc_Perbeg_Date DATE, Inc_Period VARCHAR (4));"); 
     db.execSQL("CREATE TABLE Outcomes(_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       "Out_Name VARCHAR(50), Out_Sum VARCHAR(10), Out_Mem VARCHAR (50), Out_Change_Date DATE, " + 
         "Out_Perbeg_Date DATE, Out_Period VARCHAR (4));"); 
     db.execSQL("CREATE TABLE Goals(_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       "Goal_Name VARCHAR(50), Goal_Sum VARCHAR(10), Goal_Mem VARCHAR (50), Goal_Change_Date DATE, " + 
         "Goal_Perbeg_Date DATE, Goal_Period VARCHAR (4));"); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) { 
     // TODO Auto-generated method stub 

    } 

} 

創建DataSource類與插入方法。 PBDataSource.java

package com.example.pocketbooker; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 

public class PBDataSource { 

    DB dbHelper; 
    SQLiteDatabase db; 
    public PBDataSource(Context context) { 
      dbHelper = new DB(context); 
      } 
    public void open() throws SQLException { 
     if (dbHelper != null) { 
      db = dbHelper.getWritableDatabase(); 
     } 
    } 

    public void close() { 
     dbHelper.close(); 
    } 

    public void insertrecord (String a, String b, String c) 
    {ContentValues cv= new ContentValues(); 
     cv.put(b,c); 
     db.insert(a, null, cv); 
    } 
} 

在我DialogFragment類使用這種方法Dialog_mem.java

package com.example.pocketbooker; 

import android.annotation.SuppressLint; 
import android.app.DialogFragment; 
import android.content.ContentValues; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.EditText; 
import android.widget.Toast; 

@SuppressLint({ "NewApi", "ValidFragment" }) 
public class Dialog_mem extends DialogFragment implements OnClickListener { 
    EditText memname; 
    private PBDataSource datasource; 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    getDialog().setTitle("Добавить Члена Семьи"); 

    View v = inflater.inflate(R.layout.mem_dialog, null); 

    v.findViewById(R.id.mem_btnOK).setOnClickListener(this); 
    v.findViewById(R.id.mem_btnCancel).setOnClickListener(this); 
    memname=(EditText) v.findViewById(R.id.mem_name); 

    datasource = new PBDataSource(getActivity()); 
    return v; 

    } 


public void onClick(View v) { 

     switch(v.getId()) 
     { case R.id.mem_btnOK: 

      datasource.open(); 
      datasource.insertrecord("Members","Mem_Name", memname.getText().toString()); 
      datasource.close(); 
      Toast.makeText(getActivity(), "Добавлено", Toast.LENGTH_SHORT).show(); 
     default: 
      dismiss();} 

    } 

    public void onDismiss(DialogInterface dialog) { 
    super.onDismiss(dialog); 

    } 

    public void onCancel(DialogInterface dialog) { 
    super.onCancel(dialog); 

    }} 

當然,我是一個菜鳥。我儘可能地學習自己。我希望它能幫助像我這樣的人。