Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6985863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:44:06+00:00 2026-05-27T18:44:06+00:00

I’m new to databases, so just bear with me. I’m trying to have my

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:44:07+00:00Added an answer on May 27, 2026 at 6:44 pm

    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 your insert statement. 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 SELECT query, possibly grouped by the date field so you only get one row per date. Something like this should work:

    SELECT * from table GROUP BY date
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a reasonable size flat file database of text documents mostly saved in
I have a view passing on information from a database: def serve_article(request, id): served_article
I am trying to loop through a bunch of documents I have to put
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.