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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:42:32+00:00 2026-06-10T15:42:32+00:00

I am trying to copy a database that I made with SQLite manager, in

  • 0

I am trying to copy a database that I made with SQLite manager, in which I did:

CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')

and

INSERT INTO "android_metadata" VALUES ('en_US')  

And I named all my primary keys _id. My database gets copied(in the first run there are various red messages in the logcat); thereafter, it only gives an error when I query it.

MainActivity

public class MainActivity extends Activity {
String CNAME=" ques",TABLE_NAME=" JAVAQ";




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Setupdb dbobj=new Setupdb(this); 

    try {    

        //dbobj.close();
        dbobj.createDataBase();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 



    dbobj.openDataBase();
    dbobj.close();



   try{
         SQLiteDatabase sqdb=dbobj.getReadableDatabase();



         Cursor c = sqdb.query(TABLE_NAME,
                 new String[] { CNAME },
                 null, null, null, null, null); 
         while (c.moveToNext()) {

            String name =
            c.getString(c.getColumnIndex(CNAME));
            Log.i("LOG_TAG", " HAS NAME " + name);
         }}



            catch(Exception e){

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




}}

Setupdb

public class Setupdb extends SQLiteOpenHelper {

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

    private static Setupdb mDBConnection;


public Setupdb(Context context) {
    super(context, DB_NAME, null, 3);
    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 {
        // 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) {


}

}

Stacktrace

 08-31 20:17:05.320: I/dalvikvm(9457): threadid=3: reacting to signal 3
08-31 20:17:05.370: I/dalvikvm(9457): Wrote stack traces to '/data/anr/traces.txt'
08-31 20:17:05.451: E/camprep.sqlite(9457): /data/data/com.example.mydataexplosion/databases/
08-31 20:17:05.490: E/db(9457): exist
08-31 20:17:05.521: E/CursorWindow(9457): Failed to read row 0, column -1 from a CursorWindow which has 11 rows, 1 columns.
08-31 20:17:05.521: E/err(9457): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
08-31 20:17:05.650: D/gralloc_goldfish(9457): Emulator without GPU emulation detected.
08-31 20:17:05.650: I/dalvikvm(9457): threadid=3: reacting to signal 3
08-31 20:17:05.670: I/dalvikvm(9457): Wrote stack traces to '/data/anr/traces.txt'
  • 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-10T15:42:34+00:00Added an answer on June 10, 2026 at 3:42 pm

    if you see

    failed to read row 0,column -1
    

    It means you are trying to read from a column which doesn’t exist.

    If it cannot find the column name that you specify, Cursor.getColumnIndex() returns -1 and hence, is invalid.

    There are two reasons for this:

    1. The column does not exist.
    2. The name of the column is incorrect. (so does not exist).

    Note: the name of the column is CASE SENSITIVE when using getColumnIndex()


    In your scenario:

     c.getString(c.getColumnIndex(CNAME));
    

    Check that the CNAME variable is spelt correctly, and that a column of that name exists.

    String CNAME=" ques"
    

    Should that extra leading white space be there for example..

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

Sidebar

Related Questions

I've been trying to pull a copy of my sqlite database from my android
Trying to copy a database into a new database, using a PHP script that
Basically, I'm trying to selectively copy a table from one database to another. I
I have used this example to create a database that I copy over on
I am trying to copy a table from on MySQL database on a remote
I'm trying to copy my SQLite file that is used in my Air app
I'm trying to create a data import mechanism for a database that requires high
I'm trying to create a copy of a database in SQL Management Studio (SQL
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

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.