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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:50:13+00:00 2026-05-25T01:50:13+00:00

in my test android app I intend to create and access database file, which

  • 0

in my test android app I intend to create and access database file, which will be located on SD card. I am using main activity with help of a class, which extends SQLiteOpenHelper. I want to use it the same way as before, but I have to somehow change the database PATH. Do you know, how to achieve it?

thx

My current code of a class which extends SQLiteOpenHelper:

public class DatabaseDefinition extends SQLiteOpenHelper{
private static final String DATABASE_NAME="test.db";
private static final int DATABASE_VERSION=1;

public DatabaseDefinition(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 AUTOINCREMENT, "+ NAME+" TEXT NOT NULL, " +SURNAME+" TEXT NOT NULL, "+PHONE+" INT NOT NULL);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
    onCreate(db);

}

And code of my main:

public class DatabaseExampleActivity extends Activity {
   private DatabaseDefinition database;
   private static String[] FROM={_ID, NAME, SURNAME,PHONE};
   private static String ORDER_BY=" DESC";


   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       database= new DatabaseDefinition(this); 
       try{
         String name = null;
         String surname=null;
         int phone=0;
         addEvent("John", "Black", 111012345);

    }finally{
        database.close();
    }
  }



}
  • 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-25T01:50:13+00:00Added an answer on May 25, 2026 at 1:50 am

    First you have to specify the path of the sdcard. You can do that by creating a string like this:

    public static final String  DATABASE_FILE_PATH = "/sdcard";
    

    But for you should call

    Environment.getExternalStorageDirectory()   
    

    to get the root path to the SD card and use that to create the database. After that you create the database as you want. Here is an example

    public class DatabaseHelper
    { 
       private static final String TAG                  = "DatabaseHelper";
    
      public static final String  DATABASE_FILE_PATH      = Environment.getExternalStorageDirectory();
      public static final String  DATABASE_NAME      = "mydb"; 
      public static final String  TRACKS_TABLE        = "tracks";
      public static final String  TRACK_INFO_TABLE        = "track_info";
    
      private static final String TRACKS_TABLE_CREATE     = "create table "
               + TRACKS_TABLE
               + " (_id integer primary key autoincrement, title text not null, description text null, created_at date not null);";
    
      private static final String TRACK_INFO_TABLE_CREATE = "create table " 
               + TRACK_INFO_TABLE 
               + " (_id integer primary key autoincrement, track_id integer not null, latitude real not null, longitude real not null, altitude real not null, created_at date not null);";
    
    private SQLiteDatabase      database;
    
    public DatabaseHelper() 
    {  
        try
        {
            database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
                + File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READWRITE);
        }
        catch (SQLiteException ex)
        {
            Log.e(TAG, "error -- " + ex.getMessage(), ex);
            // error means tables does not exits
            createTables();
        }
        finally
        {
            DBUtil.safeCloseDataBase(database);
        }
    }
    
    private void createTables()
    {
        database.execSQL(TRACKS_TABLE_CREATE);
        database.execSQL(TRACK_INFO_TABLE_CREATE);
    }
    
    public void close()
    {
        DBUtil.safeCloseDataBase(database);
    }
    
    public SQLiteDatabase getReadableDatabase()
    {
        database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
                + File.separator + DATABASE_NAME, null,
                SQLiteDatabase.OPEN_READONLY);
        return database;
    }
    
    public SQLiteDatabase getWritableDatabase()
    {
        database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
                + File.separator + DATABASE_NAME, null,
                SQLiteDatabase.OPEN_READWRITE);
        return database;
    }
    

    And in the end you have to set permission in manifest like this: android.permission.WRITE_EXTERNAL_STORAGE

    Good luck 🙂
    Arkde

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

Sidebar

Related Questions

I'm trying to test an Activity in android which will show a ProgressDialog and
I have created following activity package com.ali.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import
I wanted to implement the ActionBar (Android 4.0) in a test App to see
I've modified my previous code for login. package log1.log2; import android.app.Activity; import android.content.Intent; import
I'm using eclipse, and I have two android projects which have different topics. And
I'm writing an sample app to create a Server on Android and a client
I am making android app in which I am facing problem . The overview
I am new to Android and Java. I have constructed an app using HTML/Javascript
i am getting java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fb.test/com.fb.test.Fb_testActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f020005 error
Is there any virtual machine to test Android? How can I debug the program

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.