There is an error when i run my android program.
My Booking.java
package one.two;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
public class Booking extends ListActivity
{
private DBAdapter db;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = new DBAdapter(this);
db.open();
setContentView(R.layout.main);
long id;
id = db.insertTime(
new String("08:00"));
Cursor spinnerCursor = db.getSpinnerData();
startManagingCursor(spinnerCursor);
Spinner colourSpinner = (Spinner) findViewById(R.id.spinner);
/*Create an array to specify the fields we want to display in the list (only the 'colourName' column in this case) */
/* String[] from = new String[]{DBAdapter.KEY_ARRIVAL};
/* and an array of the fields we want to bind those fields to (in this case just the textView 'tvDBViewRow' from our new db_view_row.xml layout above)
int[] to = new int[]{R.id.bookingtxt1};
Now create a simple cursor adapter..
SimpleCursorAdapter colourAdapter =
new SimpleCursorAdapter(this, R.layout.booking, spinnerCursor, from, to);
colourSpinner.setAdapter(colourAdapter);*/
}
}
DBAdapter.java
package one.two;
import java.io.IOException;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
private static String DB_PATH = "/data/data/one.two/databases/";
private static String DB_NAME = "irsyad";
//values for the login table
public static final String KEY_ROWID = "_id";
public static final String KEY_USERNAME = "Username";
public static final String KEY_PASSWORD = "Password";
public static final String KEY_LNAME = "LastName";
public static final String KEY_FNAME ="FirstName";
public static final String KEY_ROWID2 = "_id";
public static final String KEY_STATUS = "status";
public static final String KEY_DESTINATION = "destination";
public static final String KEY_ARRIVAL = "arrival";
public static final String KEY_FERRY ="ferry";
private static final String TAG = "DBAdapter";
//declare Database name, tables names
private static final String DATABASE_NAME = "irsyad";
private static final String DATABASE_TABLE = "User";
private static final String DATABASE_TABLE_2 = "port";
private static final int DATABASE_VERSION = 1;
//declares the rules for the database tables
private static final String DATABASE_CREATE =
"create table user (_id integer primary key autoincrement, "
+ "Username text not null, Password text not null,"
+ "LastName text not null, FirstName text not null);";
private static final String DATABASE_CREATE_2 =
"create table port (_id integer primary key autoincrement, "
+ "status text not null, destination text not null,"
+ "arrival text not null, ferry text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(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);
}
//Create the tables with the rules we set.
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE_2);
}
//OnUpgrade is only for use when u changed the database's version to 2 etc.
@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 port");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close() {
DBHelper.close();
}
//Method for inserting login details, can be used in other java files when DBAdapter is
//declared in the java file. e.g. DBAdapter db = new DBAdapter(this);
public long insertUser(String Username, String Password, String LastName, String FirstName) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USERNAME, Username);
initialValues.put(KEY_PASSWORD, Password);
initialValues.put(KEY_LNAME, LastName);
initialValues.put(KEY_FNAME, FirstName);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular title---
public boolean deleteUser(long rowId) {
return db.delete(DATABASE_TABLE, KEY_ROWID +
"=" + rowId, null) > 0;
}
public long insertTime(String arrival) {
ContentValues initialValue = new ContentValues();
initialValue.put(KEY_ARRIVAL, arrival);
return db.insert(DATABASE_TABLE_2, null, initialValue);
}
//method for retrieving all the inputs from database
public Cursor getAllUser() {
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_USERNAME,
KEY_PASSWORD,
KEY_LNAME,
KEY_FNAME,},
null,
null,
null,
null,
null);
}
public Cursor getAllData() {
return db.query(DATABASE_TABLE_2, new String[] {
KEY_ROWID2,
KEY_STATUS,
KEY_DESTINATION,
KEY_ARRIVAL,
KEY_FERRY,},
null,
null,
null,
null,
null);
}
//---retrieves a particular title---
public Cursor getUser(long rowId) throws SQLException {
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_USERNAME,
KEY_PASSWORD,
KEY_LNAME,
KEY_FNAME,},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getSpinnerData() throws SQLException {
Cursor mCursor =
db.query(DATABASE_TABLE_2, new String[] {
KEY_ARRIVAL,},
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a title---
public boolean updateUser(long rowId, String Username, String Password, String LastName, String FirstName) {
ContentValues args = new ContentValues();
args.put(KEY_USERNAME, Username);
args.put(KEY_PASSWORD, Password);
args.put(KEY_LNAME, LastName);
args.put(KEY_FNAME, FirstName);
return db.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
catch(SQLiteException e) {
//database does't exist yet.
}
if(checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if(dbExist) {
//do nothing - database already exist
}
else {
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE_2);
}
}
}
Error log
08-29 07:01:55.966: ERROR/Database(994): Error inserting arrival=08:00
08-29 07:01:55.966: ERROR/Database(994): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
08-29 07:01:55.966: ERROR/Database(994): at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
08-29 07:01:55.966: ERROR/Database(994): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:66)
08-29 07:01:55.966: ERROR/Database(994): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1313)
08-29 07:01:55.966: ERROR/Database(994): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1173)
08-29 07:01:55.966: ERROR/Database(994): at one.two.DBAdapter.insertTime(DBAdapter.java:126)
08-29 07:01:55.966: ERROR/Database(994): at one.two.Booking.onCreate(Booking.java:22)
08-29 07:01:55.966: ERROR/Database(994): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-29 07:01:55.966: ERROR/Database(994): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
08-29 07:01:55.966: ERROR/Database(994): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
08-29 07:01:55.966: ERROR/Database(994): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
08-29 07:01:55.966: ERROR/Database(994): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
08-29 07:01:55.966: ERROR/Database(994): at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 07:01:55.966: ERROR/Database(994): at android.os.Looper.loop(Looper.java:123)
08-29 07:01:55.966: ERROR/Database(994): at android.app.ActivityThread.main(ActivityThread.java:3948)
08-29 07:01:55.966: ERROR/Database(994): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 07:01:55.966: ERROR/Database(994): at java.lang.reflect.Method.invoke(Method.java:521)
08-29 07:01:55.966: ERROR/Database(994): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
08-29 07:01:55.966: ERROR/Database(994): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
08-29 07:01:55.966: ERROR/Database(994): at dalvik.system.NativeStart.main(Native Method)