• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps SQLite Database Help

After entering data in the edit text fields and pressing the book button, the application crashes. The message "Unfortunately the application Appointment DB has stopped working" is displayed. Can't figure out what's wrong. Here is my code.

activity_main.xml

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">

<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@String/id_string"
android:id="@+id/textView" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="@+id/clientID" />
</TableRow>

<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@String/name_string"
android:id="@+id/textView3" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/clientName" />
</TableRow>

<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@String/phone_string"
android:id="@+id/textView2" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/clientPhone" />
</TableRow>

<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@String/time_string"
android:id="@+id/textView1" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="time"
android:ems="10"
android:id="@+id/clientTime" />
</TableRow>
</TableLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@String/book_string"
android:id="@+id/button"
android:onClick="newClient"/>

</LinearLayout>
</LinearLayout>


Apointment.java

package com.example.android.appointmentdb;

/**
* Created by Jamila on 7/16/2015.
*/
public class Appointment {

private int _id;
private String _clientName;
private String _clientPhone;
private String _clientTime;

public Appointment() {

}

public Appointment(int id, String clientName, String clientPhone, String clientTime) {
this._id = id;
this._clientName = clientName;
this._clientPhone = clientPhone;
this._clientTime = clientTime;
}

public Appointment(String clientName, String clientPhone, String clientTime) {
this._clientName = clientName;
this._clientPhone = clientPhone;
this._clientTime = clientTime;
}

public void setID(int id) {
this._id = id;
}

public int getID() {
return this._id;
}

public void setClientName(String clientName) {
this._clientName = clientName;
}

public String getClientName() {
return this._clientName;
}

public void setClientTime(String clientTime) {
this._clientTime = clientTime;
}

public String getClientTime() {
return this._clientTime;
}

public void setClientPhone(String clientPhone) {
this._clientPhone = clientPhone;
}

public String getClientPhone() {
return this._clientPhone;
}
}


MyDBHandler.java

package com.example.android.appointmentdb;

/**
* Created by Jamila on 7/16/2015.
*/
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;

public class MyDBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "thursdayDB.db";
private static final String TABLE_APPOINTMENTS = "7-16-2015";

public static final String COLUMN_ID = "_id";
public static final String COLUMN_CLIENTNAME = "clientname";
public static final String COLUMN_CLIENTPHONE = "clientphone";
public static final String COLUMN_CLIENTTIME = "clienttime";

public MyDBHandler(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@override
public void onCreate(SQLiteDatabase db) {

String CREATE_PRODUCTS_TABLE = "CREATE TABLE " +
TABLE_APPOINTMENTS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY,"
+ COLUMN_CLIENTNAME+ " TEXT,"
+ COLUMN_CLIENTPHONE + " TEXT,"
+ COLUMN_CLIENTTIME + " TEXT" + ")";
db.execSQL(CREATE_PRODUCTS_TABLE);

}

@override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {

db.execSQL("DROP TABLE IF EXISTS " + TABLE_APPOINTMENTS);
onCreate(db);

}

public void addClient(Appointment appointment) {

ContentValues values = new ContentValues();
values.put(COLUMN_CLIENTNAME, appointment.getClientName());
values.put(COLUMN_CLIENTPHONE, appointment.getClientPhone());
values.put(COLUMN_CLIENTTIME, appointment.getClientTime());

SQLiteDatabase db = this.getWritableDatabase();

db.insert(TABLE_APPOINTMENTS, null, values);
db.close();
}

public Appointment findAppointment(String clientname) {
String query = "Select * FROM " + TABLE_APPOINTMENTS + " WHERE " + COLUMN_CLIENTNAME + " = \"" + clientname + "\"";

SQLiteDatabase db = this.getWritableDatabase();

Cursor cursor = db.rawQuery(query, null);

Appointment appointment = new Appointment();

if (cursor.moveToFirst()) {
cursor.moveToFirst();
appointment.setID(Integer.parseInt(cursor.getString(0)));
appointment.setClientName(cursor.getString(1));
appointment.setClientPhone(cursor.getString(2));
appointment.setClientTime(cursor.getString(3));
cursor.close();
} else {
appointment = null;
}
db.close();
return appointment;
}

}



MainActivity.java

package com.example.android.appointmentdb;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

TextView idView;
EditText clientNameBox;
EditText clientPhoneBox;
EditText clientTimeBox;

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

idView = (TextView) findViewById(R.id.clientID);
clientNameBox = (EditText) findViewById(R.id.clientName);
clientPhoneBox =(EditText) findViewById(R.id.clientPhone);
clientTimeBox =(EditText) findViewById(R.id.clientTime);
}

public void newClient (View view) {
MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1);

String phone = clientPhoneBox.getText().toString();

String name = clientNameBox.getText().toString();

String time = clientTimeBox.getText().toString();

Appointment newAppointment = new Appointment(name, phone, time);

dbHandler.addClient(newAppointment);

clientNameBox.setText("");
clientPhoneBox.setText("");
clientTimeBox.setText("");

Toast.makeText(getApplicationContext(), "Your appointment is set =)",
Toast.LENGTH_LONG).show();

}


@override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}



logcat
07-21 22:11:12.664 1912-1912/com.example.android.appointmentdb I/art﹕ Not late-enabling -Xcheck:jni (already on)
07-21 22:11:13.698 1912-1924/com.example.android.appointmentdb I/art﹕ Background sticky concurrent mark sweep GC freed 2864(249KB) AllocSpace objects, 0(0B) LOS objects, 22% free, 883KB/1135KB, paused 12.035ms total 28.887ms
07-21 22:11:13.839 1912-1936/com.example.android.appointmentdb D/OpenGLRenderer﹕ Render dirty regions requested: true
07-21 22:11:13.840 1912-1912/com.example.android.appointmentdb D/﹕ HostConnection::get() New Host Connection established 0xa6c486b0, tid 1912
07-21 22:11:13.881 1912-1912/com.example.android.appointmentdb D/Atlas﹕ Validating map...
07-21 22:11:14.012 1912-1936/com.example.android.appointmentdb D/﹕ HostConnection::get() New Host Connection established 0xa6c48760, tid 1936
07-21 22:11:14.044 1912-1924/com.example.android.appointmentdb I/art﹕ Background partial concurrent mark sweep GC freed 2286(156KB) AllocSpace objects, 0(0B) LOS objects, 50% free, 986KB/2010KB, paused 23.095ms total 46.956ms
07-21 22:11:14.089 1912-1936/com.example.android.appointmentdb I/OpenGLRenderer﹕ Initialized EGL, version 1.4
07-21 22:11:14.116 1912-1936/com.example.android.appointmentdb D/OpenGLRenderer﹕ Enabling debug mode 0
07-21 22:11:14.163 1912-1936/com.example.android.appointmentdb W/EGL_emulation﹕ eglSurfaceAttrib not implemented
07-21 22:11:14.163 1912-1936/com.example.android.appointmentdb W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xae0c2da0, error=EGL_SUCCESS
07-21 22:11:14.700 1912-1912/com.example.android.appointmentdb I/Choreographer﹕ Skipped 45 frames! The application may be doing too much work on its main thread.
07-21 22:11:15.570 1912-1912/com.example.android.appointmentdb I/Choreographer﹕ Skipped 51 frames! The application may be doing too much work on its main thread.
07-21 22:11:15.928 1912-1936/com.example.android.appointmentdb W/EGL_emulation﹕ eglSurfaceAttrib not implemented
07-21 22:11:15.928 1912-1936/com.example.android.appointmentdb W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xae0c2da0, error=EGL_SUCCESS

 
Hi there, I don't have an answer for you but why are you passing null for the parameter "name" when you create an instance of your MyDBHelper class ? a typical constructor would look like this, where DATABASE_NAME and DATABASE_VERSION are class fields
Java:
public MyDBHandler(Context context) 
{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

you only need to pass in your context.
Java:
// Calling code
MyDBHandler myHandler = new MyDBHandler(this);
 
Last edited:
Back
Top Bottom