SQLiteOpenHelper.getReadableDatabase()方法崩溃了

时间:2017-04-20 21:35:46

标签: android sqlite android-sqlite

这是我的DatabaseHelper类

public class DatabaseHelper extends SQLiteOpenHelper

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "MyAccountsDB.db";

    public static final String TABLE_Accounts = "Accounts";

    public static final String COLUMN_AccountID= "AccountID";
    public static final String COLUMN_WebSite= "WebSite";
    public static final String COLUMN_Email= "Email";
    public static final String COLUMN_UserName = "UserName";
    public static final String COLUMN_Password= "Password";


    private static final String TEXT_TYPE = " TEXT";
    private static final String INT_TYPE = " INTEGER";
    private static final String COMMA_SEP = ",";

    private static final String SQL_CREATE_TABLE_Accounts =
            "CREATE TABLE " + TABLE_Accounts +
                    " (" +
                    COLUMN_AccountID + INT_TYPE +" PRIMARY KEY AUTOINCREMENT NOT NULL"+ COMMA_SEP +
                    COLUMN_WebSite + TEXT_TYPE + COMMA_SEP +
                    COLUMN_Email + TEXT_TYPE + COMMA_SEP +
                    COLUMN_UserName + TEXT_TYPE + COMMA_SEP +
                    COLUMN_Password+ TEXT_TYPE + ")";
    public DatabaseHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(SQL_CREATE_TABLE_Accounts);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1)
    {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_Accounts);
        onCreate(db);
    }
    public void CreateAccount(Account acc)
    {
        SQLiteDatabase db = getWritableDatabase();

        String insertAccount = "Insert into Accounts (WebSite,Email,UserName,Password) values" +
        "('" + acc.getWebSite() + "','" + acc.geteMail() + "','" + acc.getUserName() + "','" + acc.getPassWord() + "')";
        db.execSQL(insertAccount);
    }

    public ArrayList<Account> getAllAccounts()
    {
        String query = "SELECT AccountID , WebSite FROM Accounts";
        ArrayList<Account> Accounts = new ArrayList<Account>();
        SQLiteDatabase database = getReadableDatabase();//when its called here no problem and load all the accounts the way i want
        Cursor c = database.rawQuery(query, null);
        if (c != null)
        {
            while (c.moveToNext())
            {
                Account acc = new Account();
                acc.setAccountID(c.getInt(0));
                acc.setWebSite(c.getString(1));
                Accounts.add(acc);
            }
        }
        return Accounts;
    }
    public Account getAccountByID(int id)
    {
        Account acc  = new Account();
        String query = "SELECT WebSite , eMail , UserName , Password from Accounts where AccountID = " + id;
        SQLiteDatabase database = getReadableDatabase();//but when i call it here following exception thrown 
        Cursor c = database.rawQuery(query, null);
        if (c != null)
        {
            while (c.moveToFirst())
            {
                acc.setAccountID(c.getInt(0));
                acc.setWebSite(c.getString(1));
                acc.seteMail(c.getString(2));
                acc.setUserName(c.getString(3));
                acc.setPassWord(c.getString(4));
            }
        }
        return acc;
    }
NullPointerException :
Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference

我有另一个使用相同方法和函数语法的应用程序,它没有问题

3 个答案:

答案 0 :(得分:0)

我认为您需要以不同方式创建数据库帮助程序。

例如,

public class MainActivity extends Activity {

    private DatabaseHelper db; // Don't initialize here

    @Override
    protected void onCreate(Bundle b) {
        ...
        db = new DatabaseHelper(this); // Do it here
    }
}

如果在整个活动中使用相同的db变量,那么应该修复当前错误,但是在SQL查询中仍然存在与表定义不匹配的其他问题。

除非您希望该循环永不结束,否则您也不想使用while (c.moveToFirst())

public Account getAccountByID(int id)
{
    Account acc  = new Account();
    SQLiteDatabase database = getReadableDatabase();

    Cursor c = database.query(TABLE_Accounts, 
            { COLUMN_WebSite , COLUMN_Email , COLUMN_UserName , COLUMN_Password }, 
            COLUMN_AccountID + "=?", 
            { String.valueOf(id) }, 
            null, null, null, null);
        );    

    try {
        if (c != null && c.moveToFirst())
        {
            acc.setAccountID(id);
            acc.setWebSite(c.getString(0));
            acc.seteMail(c.getString(1));
            acc.setUserName(c.getString(2));
            acc.setPassWord(c.getString(3));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (c != null) c.close();
    }
    return acc;
}

答案 1 :(得分:0)

谢谢大家......我找到了答案......我正在打电话给 来自片段的getAccountById方法...所以我必须重新传递  再次向它上下文

UpdateAccountFragment a = new UpdateAccountFragment(accountID,getActivity());

并且UpdateAccountFragment的构造函数看起来像

 public UpdateAccountFragment (int accountID , Context context)
{
    this.context = context;
    this.accountID = accountID;
}

答案 2 :(得分:-1)

问题出在您的查询中:

String query = "SELECT WebSite , eMail , UserName , Password from Accounts where AccountID = " + id;

您使用eMail代替Email。您的column表中没有Accounts,名为eMail。同时将from更改为FROM,将where子句更改为WHERE

更改Query,如下所示:

String query = "SELECT WebSite , Email , UserName , Password FROM Accounts WHERE AccountID = " + id;

从您的instanceDatabaseHelper创建Activity Fragment,如下所示:

来自活动:

public class MainActivity extends AppCompatActivity {

    DatabaseHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db = new DatabaseHelper(this); // Context is this

        ...............
        ....................
    }

}

来自片段:

public class UpdateAccountFragment extends Fragment {

    DatabaseHelper db;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        db = new DatabaseHelper(getActivity()); // Context is getActivity()

        ...............
        ....................
    }
}

希望这会有所帮助〜