I’m new to databases, so just bear with me. I’m trying to have my database set to when an entry is created it is displayed in ListView as the date it was created on (for example, “December 23, 2011) and then you can click on that and see everything which you had created on that day. This is what I have so far:
public class DbAdapter
{
private static final String TAG = "DbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE =
"create table entry (_id integer primary key autoincrement, "
+ "date text UNIQUE, " +
"velocity integer not null, " +
"type text not null, " +
"xCoord real not null, " +
"yCoord real not null, " +
"color integer not null);";
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "entry";
private static final int DATABASE_VERSION = 2;
private final Context mCtx;
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into " + DATABASE_TABLE + "(date) values (?)";
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 entry");
onCreate(db);
}
}
public DbAdapter(Context ctx)
{
this.mCtx = ctx;
}
public DbAdapter open() throws SQLException
{
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
this.insertStmt = this.mDb.compileStatement(INSERT);
return this;
}
public void close()
{
mDbHelper.close();
}
public long addEntry(String date, int velocity, String type, float xCoord, float yCoord, int color)
{
ContentValues initialValues = new ContentValues();
initialValues.put("date", date);
initialValues.put("velocity", velocity);
initialValues.put("type", type);
initialValues.put("xCoord", xCoord);
initialValues.put("yCoord", yCoord);
initialValues.put("color", color);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteEntry(long rowId)
{
return mDb.delete(DATABASE_TABLE, "_id" + "=" + rowId, null) > 0;
}
public Cursor fetchAllEntries()
{
return mDb.query(DATABASE_TABLE, new String[] {"_id", "date", "velocity",
"type", "xCoord", "yCoord", "color"}, null, null, null, null, null);
}
public Cursor fetchEntry(long rowId) throws SQLException
{
Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {"_id", "date",
"velocity", "type"}, "_id" + "=" + rowId, null,
null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor fetchInfo(long rowId) throws SQLException
{
Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {"_id", "velocity", "xCoord", "yCoord", "color"}, "_id" + "=" + rowId, null, null, null, null, null);
if(mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
}
This works fine for the first entry I create, but I can’t create more entries for that certain day, it gives me an error saying:
12-23 09:22:16.460: E/Database(272): Error inserting velocity=17 yCoord=121.0 xCoord=344.0 color=3 type=UNFINISHED date=December 23, 2011
12-23 09:22:16.460: E/Database(272): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
12-23 09:22:16.460: E/Database(272): at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
12-23 09:22:16.460: E/Database(272): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
12-23 09:22:16.460: E/Database(272): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549)
12-23 09:22:16.460: E/Database(272): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
12-23 09:22:16.460: E/Database(272): at com.x17.projects.strikezone.DbAdapter.addEntry(DbAdapter.java:96)
12-23 09:22:16.460: E/Database(272): at com.x17.projects.strikezone.Tab1$2.onClick(Tab1.java:121)
12-23 09:22:16.460: E/Database(272): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
12-23 09:22:16.460: E/Database(272): at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 09:22:16.460: E/Database(272): at android.os.Looper.loop(Looper.java:123)
12-23 09:22:16.460: E/Database(272): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-23 09:22:16.460: E/Database(272): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 09:22:16.460: E/Database(272): at java.lang.reflect.Method.invoke(Method.java:521)
12-23 09:22:16.460: E/Database(272): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-23 09:22:16.460: E/Database(272): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-23 09:22:16.460: E/Database(272): at dalvik.system.NativeStart.main(Native Method)
I know that this error is happening because I’m trying to insert a value into a row which already has a value. So my question is, how can I insert multiple values within the date it was created on. I have an insert statement which is not being used, because I don’t know where and how to use it, and I’m not sure if it is correct. As I am new to this, just a point to the right direction would be helpful.
Thanks in advance.
UPDATE:
I removed “not null” from all columns and changed my insert statement to:
private static final String INSERT = "INSERT INTO " + DATABASE_TABLE + "(date, velocity, type, xCoord, yCoord, color) VALUES (?,?,?,?,?,?)";
and I’m inserting the values like this:
public long addEntry(String date, int velocity, String type, float xCoord, float yCoord, int color)
{
this.insertStmt.bindString(1, date);
this.insertStmt.bindLong(2, velocity);
this.insertStmt.bindString(3, type);
this.insertStmt.bindDouble(4, xCoord);
this.insertStmt.bindDouble(5, yCoord);
this.insertStmt.bindLong(6, color);
return this.insertStmt.executeInsert();
}
I’m still getting the same “error code 19: constraint failed” error.
12-23 11:36:03.343: E/AndroidRuntime(418): FATAL EXCEPTION: main
12-23 11:36:03.343: E/AndroidRuntime(418): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
12-23 11:36:03.343: E/AndroidRuntime(418): at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
12-23 11:36:03.343: E/AndroidRuntime(418): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:81)
12-23 11:36:03.343: E/AndroidRuntime(418): at com.x17.projects.strikezone.DbAdapter.addEntry(DbAdapter.java:86)
12-23 11:36:03.343: E/AndroidRuntime(418): at com.x17.projects.strikezone.Tab1$2.onClick(Tab1.java:120)
12-23 11:36:03.343: E/AndroidRuntime(418): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
12-23 11:36:03.343: E/AndroidRuntime(418): at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 11:36:03.343: E/AndroidRuntime(418): at android.os.Looper.loop(Looper.java:123)
12-23 11:36:03.343: E/AndroidRuntime(418): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-23 11:36:03.343: E/AndroidRuntime(418): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 11:36:03.343: E/AndroidRuntime(418): at java.lang.reflect.Method.invoke(Method.java:521)
12-23 11:36:03.343: E/AndroidRuntime(418): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-23 11:36:03.343: E/AndroidRuntime(418): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-23 11:36:03.343: E/AndroidRuntime(418): at dalvik.system.NativeStart.main(Native Method)
What am I doing wrong?
I suspect the “date text UNIQUE” is the problem for you, if you remove the UNIQUE then you should be able to insert multiple entries with the same date — if I’m understanding you correctly.
Problem 2: if you set your fields in the database to be “
not null” then you must specify values for those fields in your “insert” statement. Currently you are only setting the date field in yourinsertstatement. So you either need to specify values for all of your fields in your ‘insert‘ statement, or you need to remove the “not null” specifications in your table create.To get the dates for your list view you will need to do some sort of
SELECTquery, possibly grouped by the date field so you only get one row per date. Something like this should work: