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 7667535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:07:45+00:00 2026-05-31T15:07:45+00:00

I’m creating an android application in which i will need to store data using

  • 0

I’m creating an android application in which i will need to store data using SQLite. it needs to store date, days between 2 dates and some text fields. I have looked up some tutorials online and the method that i am most comfortable with is this one here:

public class DataHelper {

private static final String DATABASE_NAME = "example.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "table1";

private Context context;
private SQLiteDatabase db;

private SQLiteStatement insertStmt;
private static final String INSERT = "insert into "
  + TABLE_NAME + "(name) values (?)";

public DataHelper(Context context) {
  this.context = context;
  OpenHelper openHelper = new OpenHelper(this.context);
  this.db = openHelper.getWritableDatabase();
  this.insertStmt = this.db.compileStatement(INSERT);
}

public long insert(String name) {
  this.insertStmt.bindString(1, name);
  return this.insertStmt.executeInsert();
}

public void deleteAll() {
  this.db.delete(TABLE_NAME, null, null);
}

public List<String> selectAll() {
  List<String> list = new ArrayList<String>();
  Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" },
    null, null, null, null, "name desc");
  if (cursor.moveToFirst()) {
     do {
        list.add(cursor.getString(0));
     } while (cursor.moveToNext());
  }
  if (cursor != null && !cursor.isClosed()) {
     cursor.close();
  }
  return list;
}

private static class OpenHelper extends SQLiteOpenHelper {

  OpenHelper(Context context) {
     super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
     db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT)");
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
     Log.w("Example", "Upgrading database, this will drop tables and recreate.");
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
     onCreate(db);
  }
}
}

sorry about the long peice of code. I’m unsure if this way of using the database is even correct

  • 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-31T15:07:46+00:00Added an answer on May 31, 2026 at 3:07 pm
    • You are on the right track with the DataHelper. Having a DataHelper to create your database and tables is a good plan. However, have your DataHelper extend the SQLiteOpenHelper class so it is truly a Helper class.
    • Also, it is a good idea to seperate each table you are creating into different java classes. Then have your DataHelper just call the onCreate methods for those tables/classes.
    • Also, android provides a ContentProvider that helps you provide access to your data. Try providing access to your database strictly through your own ContentProvider. This provides better flexibility and a few levels of abstraction.

    Here are some examples that i have recently been working with. These are from another source as well, but modified a little. I am not sure where i got the original stuff. But here is what i have from what i was working on.

    Hope this helps!

    //SimpleNotesDBHelper (Your `DataHelper`)
    public class SimpleNotesDBHelper extends SQLiteOpenHelper {
    
        private static final String NOTES_DATABASE_NAME = "simplenotes.db";
        private static final int DB_VERSION = 1;
    
        public SimpleNotesDBHelper(Context context) {
            super(context, NOTES_DATABASE_NAME, null, DB_VERSION);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            NotesTable.onCreate(db);
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            NotesTable.onUpgrade(db, oldVersion, newVersion);
        }
    
    }
    

    //NotesTable (a seperate table in my db)
    public class NotesTable {
    
        public static final String NOTES_TABLE = "notes";
        public static final String COL_ID = "_id";
        public static final String COL_CATEGORY = "category";
        public static final String COL_SUMMARY = "summary";
        public static final String COL_DESCRIPTION = "description";
        public static final String NOTES_TABLE_CREATE = "create table "
                + NOTES_TABLE
                + "("
                + COL_ID
                + " integer primary key autoincrement, "
                + COL_CATEGORY
                + " text not null, "
                + COL_SUMMARY
                + " text not null, "
                + COL_DESCRIPTION
                + " text not null"
                + ");";
    
        public static void onCreate(SQLiteDatabase database) {
            database.execSQL(NOTES_TABLE_CREATE);
    
            insertingSomeTestData(database);
        }
    
        public static void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
            database.execSQL("drop table if exists " + NOTES_TABLE);
            onCreate(database);
        }
    }
    

    //SimpleNotesContentProvider (a ContentProvider to my db)
    public class SimpleNotesContentProvider extends ContentProvider {
    
        private SimpleNotesDBHelper databaseHelper;
    
        private static final int SIMPLE_NOTES = 10; //arbitrary
        private static final int SIMPLE_NOTE_ID = 20; //arbitrary
    
        private static final String AUTHORITY = "ashton.learning.projects.simplenotes.contentprovider";
        private static final String BASE_PATH = "simplenotes";
        private static final String DIR_BASE_TYPE = "simplenotes";
        private static final String ITEM_BASE_TYPE = "simplenote";
        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);
        public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + DIR_BASE_TYPE;
        public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + ITEM_BASE_TYPE;
        private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);      
    
        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
    
            int uriType = sURIMatcher.match(uri);
    
            SQLiteDatabase db = databaseHelper.getWritableDatabase();
            int rowsDeleted = 0;
            switch (uriType) {
            case SIMPLE_NOTES:
                rowsDeleted = db.delete(NotesTable.NOTES_TABLE, selection, selectionArgs);
                break;
            case SIMPLE_NOTE_ID:
                String id = uri.getLastPathSegment();
                if (TextUtils.isEmpty(selection)) {
                    rowsDeleted = db.delete(NotesTable.NOTES_TABLE, 
                            NotesTable.COL_ID + "=" + id, 
                            null);
                }
                else {
                    rowsDeleted = db.delete(NotesTable.NOTES_TABLE, 
                            NotesTable.COL_ID + "=" + id + " and " + selection, 
                            selectionArgs);
                }
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
            }
            getContext().getContentResolver().notifyChange(uri, null);
            return rowsDeleted;
        }
    
        @Override
        public String getType(Uri uri) {
            return null;
        }
    
        @Override
        public Uri insert(Uri uri, ContentValues values) {
    
            int uriType = sURIMatcher.match(uri);
    
            SQLiteDatabase db = databaseHelper.getWritableDatabase();
            int rowsDeleted = 0;
            long id = 0;
            switch(uriType) {
            case SIMPLE_NOTES:
                id = db.insert(NotesTable.NOTES_TABLE, null, values);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
            }
    
            getContext().getContentResolver().notifyChange(uri, null);
            return Uri.parse(BASE_PATH + "/" + id);
        }
    
        @Override
        public boolean onCreate() {
            databaseHelper = new SimpleNotesDBHelper(getContext());
            sURIMatcher.addURI(AUTHORITY, BASE_PATH, SIMPLE_NOTES);
            sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/#", SIMPLE_NOTE_ID);
            return false;
        }
    
        @Override
        public Cursor query(Uri uri, String[] projection, String selection,
                String[] selectionArgs, String sortOrder) {
    
            SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    
            queryBuilder.setTables(NotesTable.NOTES_TABLE);
    
            int uriType = sURIMatcher.match(uri);
    
            switch (uriType) {
            case SIMPLE_NOTES:
                break;
            case SIMPLE_NOTE_ID:
                queryBuilder.appendWhere(NotesTable.COL_ID 
                        + "="
                        + uri.getLastPathSegment());
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
            }
    
            SQLiteDatabase db = databaseHelper.getWritableDatabase();
    
            Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
    
            cursor.setNotificationUri(getContext().getContentResolver(), uri);
    
            return cursor;
        }
    
        @Override
        public int update(Uri uri, ContentValues values, String selection,
                String[] selectionArgs) {
    
            int uriType = sURIMatcher.match(uri);
            SQLiteDatabase db = databaseHelper.getWritableDatabase();
            int rowsUpdated = 0;
    
            switch (uriType) {
            case SIMPLE_NOTES:
                rowsUpdated = db.update(NotesTable.NOTES_TABLE, 
                        values, 
                        selection, 
                        selectionArgs);
                break;
            case SIMPLE_NOTE_ID:
                String id = uri.getLastPathSegment();
                if (TextUtils.isEmpty(selection)) {
                    rowsUpdated = db.update(NotesTable.NOTES_TABLE, 
                            values, 
                            NotesTable.COL_ID + "=" + id, 
                            null);
                }
                else {
                    rowsUpdated = db.update(NotesTable.NOTES_TABLE, 
                            values, 
                            NotesTable.COL_ID + "=" + id + " and " + selection, 
                            selectionArgs);
                }
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
            }
    
            getContext().getContentResolver().notifyChange(uri, null);
            return rowsUpdated;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters

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.