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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:40:02+00:00 2026-06-11T08:40:02+00:00

What I am trying to do is to copy the database that I made

  • 0

What I am trying to do is to copy the database that I made through SQLite manager in my app.

I’ve checked database does gets created but the database is only of 3 KB (my db is not getting copied). The code of the setupdb is from here

Setupdb.java

public class Setupdb extends SQLiteOpenHelper {

  private static String DB_PATH = "";
    private static final String DB_NAME = "fergi.sqlite";
    private SQLiteDatabase myDataBase;
    private final Context myContext;

    private static Setupdb mDBConnection;


public Setupdb(Context context) {
    super(context, DB_NAME, null, 1);
    this.myContext=context;
    DB_PATH="/data/data/"
          + context.getApplicationContext().getPackageName()
          + "/databases/";
    Log.e(DB_NAME, DB_PATH);
}
public static synchronized Setupdb getDBAdapterInstance(Context context) {
  if (mDBConnection == null) {
      mDBConnection = new Setupdb(context);
  }
     return mDBConnection;
}

    public void createDataBase() throws IOException {
        boolean dbExist = checkDataBase();
        if (dbExist) {
            Log.e("db","exist");
            // do nothing - database already exist
        } else {
            // By calling following method
            // 1) an empty database will be created into the default system path of your application
            // 2) than we overwrite that database with our database.
            this.getReadableDatabase();
            try {
                Log.e("calling", "copy");
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }





}
    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try {
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READONLY);

        } catch (SQLiteException e) {
            // database does't exist yet.
        }
        if (checkDB != null) {
            checkDB.close();
        }
        return checkDB != null ? true : false;
    }

    private void copyDataBase() throws IOException {
        Log.e("copy", "coppying db");
      // Open your local db as the input stream
  InputStream myInput = myContext.getAssets().open(DB_NAME);
      // Path to the just created empty db
  String outFileName = DB_PATH + DB_NAME;
      // Open the empty db as the output stream
  OutputStream myOutput = new FileOutputStream(outFileName);
      // transfer bytes from the inputfile to the outputfile
  byte[] buffer = new byte[1024];
  int length;
  while ((length = myInput.read(buffer)) > 0) {
      myOutput.write(buffer, 0, length);
  }
      // Close the streams
  myOutput.flush();
  myOutput.close();
  myInput.close();
  } 


    public void openDataBase() throws SQLException {
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
       }
    public synchronized void close() {
        if (myDataBase != null)
            myDataBase.close();
        super.close();
    }

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


}
 }

MainActivity.java

public class MainActivity extends Activity {
String CNAME=" ques",TABLE_NAME=" JAVAQ";
Context context;
Setupdb  myDbHelper;



/* public MainActivity(Context someContext){
     context=someContext;
     startSQL();
 }  */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=this;
    startSQL();

}




 private void startSQL() {
        myDbHelper = new Setupdb(context);
        try {

            myDbHelper.createDataBase();

        } catch (IOException ioe) {

            throw new Error(ioe);

        }

        open();
    }
    public void open()throws SQLException{
        myDbHelper.openDataBase();
    }





   /* 

    Setupdb dbobj=new Setupdb(this); 

    SQLiteDatabase sqobj=dbobj.getWritableDatabase();


    try {
        dbobj.createDataBase();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace(); 
    }  
   try{
   String query = "SELECT " + CNAME + " FROM " + TABLE_NAME+ " WHERE"+" _id=1";
   Log.e("ghgh", query);
            Cursor c2 = sqobj.rawQuery(query, null);

            while (c2.moveToNext()) {

            String name =
            c2.getString(c2.getColumnIndex(CNAME));
            Log.i("LOG_TAG", " HAS NAME " + name);
            }}
            catch(Exception e){

                Log.e("err", e.toString());
            }
}

*/

}
  • 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-06-11T08:40:04+00:00Added an answer on June 11, 2026 at 8:40 am

    This the my code of starting the sql, the database class we had are the same.

    public YOURCLASS(Context someContext){
            context=someContext;
            startSQL();
        }    
    private void startSQL() {
            myDbHelper = new DataBaseHelper(context);
            try {
    
                myDbHelper.createDataBase();
    
            } catch (IOException ioe) {
    
                throw new Error(ioe);
    
            }
    
            open();
        }
        public void open()throws SQLException{
            myDbHelper.openDataBase();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to copy a database that I made with SQLite manager, in
I'm trying to copy my SQLite file that is used in my Air app
Trying to copy a database into a new database, using a PHP script that
I've been trying to pull a copy of my sqlite database from my android
I am trying to copy my pre-populated database to a writable directory (I am
I am trying to copy files from a directory that is in constant use
I am trying to copy a database with huge data from SQL Server 2005
I'm trying to copy my production database on Heroku across to my staging server.
I am trying to copy a 5mb database file to data folder from raw
I am trying to copy a MyIsam dataBase from my a remote server to

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.