Question

I am trying to write to a table in my sqlite database in android the code I am using is as follows:

DBH = new DatabaseHelper(this);
    try {
        DBH.createDataBase();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    DBH.getWritableDatabase().rawQuery("insert into profile (_id, FirstName) values (1, 'Dave')", null);
    DBH.close();

DBH is my database helper class and it creates the database just fine and I've even been able to read from the database so i know its there. The table im trying to write to is profile and i am certain that it does in fact exist but every time i run this i get this error:

sqlite returned: error code = 1, msg = no such table: profile

I've tried reading from this table and have been able to get results so I'm pretty much stumped. Anyone got any ideas?

Edit: As requested this is my Database helper class

public class DatabaseHelper extends SQLiteOpenHelper {

private static String DB_PATH = "/data/data/com.ProjectZeus.FitnessWire/databases/";
private static String DB_NAME = "fitnesswire.db";
private SQLiteDatabase myDB;
private Context  myContext;

public DatabaseHelper(Context context) {
    super(context,DB_NAME, null, 1);
    this.myContext = context;   
}

public void createDataBase() throws IOException{
    boolean check = checkDataBase();
    if(check){
        //Log.e("Check", "Database exists");
    }else{
        this.getReadableDatabase();

        try{
            copyDataBase();
        }catch(Exception e){
            throw new Error("Error copying database");
        }
    }
}


private void copyDataBase() throws IOException {
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    String outFileName = DB_PATH + DB_NAME;

    OutputStream myOutput = new FileOutputStream(outFileName);

    byte [] buffer = new byte[1024];
    int length;
    while((length = myInput.read(buffer))>0){
        myOutput.write(buffer,0,length);
    }

    myOutput.flush();
    myOutput.close();
    myInput.close();

}

private boolean checkDataBase() {
    SQLiteDatabase checkdb = null;
    try{
        String myPath= DB_PATH + DB_NAME;
        checkdb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }catch(SQLiteException e){
        Log.e("checkDataBase()", "Database doesntexist yet");
    }

    if(checkdb != null){
        checkdb.close();
    }

    return checkdb != null ? true : false ;
}

public void openDataBase() throws SQLException {
    String myPath = DB_PATH + DB_NAME;
    myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}

@Override
public synchronized void close(){
    if(myDB != null){
        myDB.close();
    }
    super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

}

Was it helpful?

Solution

You should use db.execSQL() instead of db.rawQuery() since insert is no query (only SELECT is)

There is a lot of magic going on in rawQuery() - it may change the statement before executed by the database and also tries to build a Cursor out of the result. Some part in that process seems to fail.

OTHER TIPS

It Happens some times in Iphone as well... Data base not properly connected.

What we do (that work for us ) is that we remove all the Projects from the Simulator Clean The Project and Compile it...

i m not sure whether it gona work in Android but u can try the same

Cheers

This error can also be raised when a column doesn't exist (I know, it's not ideal, but that's how it is). So make sure you actually have columns named _id and FirstName in your table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top