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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:53:36+00:00 2026-05-15T07:53:36+00:00

Every time I am using the db its null and I just dont get

  • 0

Every time I am using the db its null and I just dont get it.

I use this code for the SQL when I have a query:

public class GameSQLHelper {
    static final String[] COUNTRIES = new String[] {
        "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra"
         };

    private static final String DB_PATH = "/countryCityGame/databases/";
    private static final String DATABASE_NAME = "events.db";
    private static final int DATABASE_VERSION = 1;
    private final Context mCtx;
    // Table name
    public static final String TABLE = "myDataBase";

    // Columns
    public static final String LETTER = "letter";
    public static final String TYPE = "type";
    public static final String VALUE = "value";

    //my database
    SQLiteDatabase myDataBase;

    private static class DatabaseHelper extends SQLiteOpenHelper {
        private static final String TAG = null;

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

        @Override
        public void onCreate(SQLiteDatabase db) {
                String sql = "create table " + TABLE + "( " + BaseColumns._ID
                + " integer primary key autoincrement, " + LETTER  + " text not null, "
                + TYPE + " text not null," + VALUE + " text not null );";
                Log.d("EventsData", "onCreate: " + sql);
                db.execSQL(sql);
                insertValuesToDB(db);

            }
            private void insertValuesToDB(SQLiteDatabase db) {


                if (db == null){

                }
                else{
                db.execSQL("INSERT INTO "

                                                + TABLE

                                                + " ("+LETTER+","+ TYPE +"," + VALUE +")"

                                                + " VALUES ('A', 'country', 'Angola');");

                  ContentValues initialValues = new ContentValues();


                  for (int i = 0 ; i < COUNTRIES.length ; i++){

                      Character tmp = (Character)COUNTRIES[i].charAt(0);

                      initialValues.put(VALUE, COUNTRIES[i]);
                      initialValues.put(TYPE, "country");
                      initialValues.put(LETTER,tmp.toString(tmp));

                  db.insert(TABLE, null, initialValues);
                  }
                }
            }
            @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 notes");
                onCreate(db);
            }

        }

        /**
         * Constructor - takes the context to allow the database to be
         * opened/created
         * 
         * @param ctx the Context within which to work
         */
        public GameSQLHelper(Context ctx) {
            this.mCtx = ctx;
        }





        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion >= newVersion)
            return;

        String sql = null;
        if (oldVersion == 1) 
            sql = "alter table " + TABLE + " add note text;";
        if (oldVersion == 2)
            sql = "";

        Log.d("EventsData", "onUpgrade  : " + sql);
        if (sql != null)
            db.execSQL(sql);
    }

     public void openDataBase() throws SQLException{

            //Open the database
            String myPath = DB_PATH + DATABASE_NAME;
            myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

        }


        public boolean existInDataBase(String serchStr ){

            Cursor c = null ;
            try{
            openDataBase();
            c =  myDataBase.query(true, TABLE, new String[] {TYPE
                    }, VALUE + "=" + serchStr, null,
                    null, null, null, null);
            }
            catch(Exception e){
                Log.d("sqlExacption", e.getMessage());

            }
            if (c == null) 
                return false;

            return true;
        }

}

Whenever I call this class (I hold an instace of it initialized: mDbHelper = new GameSQLHelper(this); where this is an activity)

I always get my mDbHelper as null how can I change it? It’s my first time working with SQL outside of mysql platform so I am kind of having problmes understanding the concept, and the android notepad example didn’t help me.

  • 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-15T07:53:37+00:00Added an answer on May 15, 2026 at 7:53 am

    Don’t open the database via SQLiteDatabase#openDatabase. Instead, create an instance of your DatabaseHelper and call getWritableDatabase on it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.