vendredi 26 février 2016

Android app crash after SQLite Database query

I am creating a newsletter and I am using a button to enter text from editText fields to store on an SQLite Database. I am entering a name, email address, and phone number. What is making my app crash when I hit the submit button?

newsletterDBAdapter.java

public class newsletterDBAdapter {

public static final String KEY_NAME = "name";
public static final String KEY_EMAIL = "email";
public static final String KEY_PHONE = "phone";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "information";
private static final String DATABASE_TABLE = "titles";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE =
    "create table titles (_id integer primary key autoincrement, "
    + "name text not null, email text not null, "
    + "phone text not null);";



private final Context context;

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public newsletterDBAdapter(Context ctx)
    {
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
    }

private static class DatabaseHelper extends SQLiteOpenHelper
{
    DatabaseHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion,
                          int newVersion)
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion
                + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS");
        onCreate(db);
    }
}

//---opens the database---
public newsletterDBAdapter open() throws SQLException
{
    db = DBHelper.getWritableDatabase();
    return this;
}

//---closes the database---
public void close()
{
    DBHelper.close();
}



//---insert a title into the database---
public long insertTitle(String name, String email, String phone)
    {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_EMAIL, email);
    initialValues.put(KEY_PHONE, phone);
    return db.insert(DATABASE_TABLE, null, initialValues);
    }


}

newsletterFragment.java

public class newsletterFragment extends Fragment {
private Button button1;
newsletterDBAdapter db = new newsletterDBAdapter(this.getActivity());


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View rootView = inflater.inflate(R.layout.fragment_newsletter, container, false);
    button1 = (Button) rootView.findViewById(R.id.button);
    button1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick (View v){
        db.open();

        EditText editTextname = (EditText)v.findViewById(R.id.editText4);
        String nameValue = editTextname.getText().toString();

        EditText editTextEmail = (EditText)v.findViewById(R.id.editText7);
        String emailValue = editTextEmail.getText().toString();

        EditText editTextPhone = (EditText)v.findViewById(R.id.editText8);
        String phoneValue = editTextPhone.getText().toString();

        db.insertTitle(nameValue, emailValue, phoneValue);
        db.close();

    }


});

    return rootView;


}}

Aucun commentaire:

Enregistrer un commentaire