Why do I get this error when trying to write to a database?

advertisements

I have this database code and the file where I want to write to the database. But when trying to find the database in the filexplorer there is no database. In logcat I'm getting this:

02-04 21:16:02.722: INFO/Database(3361): sqlite returned: error code = 14, msg = cannot open file at source line 25467
02-04 21:16:02.722: ERROR/Database(3361): sqlite3_open_v2("/data/data/com.sr.android/databases/myapp", &handle, 6, NULL) failed

Why don't I have any database? Where is the error?

public class Main extends TabActivity {
    DataBaseHandler db = new DataBaseHandler(this);
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getBrands(); 

private boolean getBrands() {

        JsonGetBrands reader = new JsonGetBrands("testa2.php");
        String Brand = reader.getBrands();
        System.out.println("hej plus " +Brand);
        db.open();
        long id = 0 ;
        db.InsertBrand(Brand);
        id = db.getAllEntries();
        Context context = getApplicationContext();
        CharSequence text = "The Brands" + Brand +"har lagts till i databasen\n brands total= " +id;
         int duration = Toast.LENGTH_LONG;
          Toast toast = Toast.makeText(context, text, duration);
          toast.show();

          db.close();

 int id = 0;
        public static final String KEY_ROWID = "_id";
        public static final String KEY_BRAND = "Brand";
        private static final String TAG = "DBAdapter";

        private static final String DATABASE_NAME = "mydb";
        private static final String DATABASE_TABLE = "tblBrands";
        private static final int DATABASE_VERSION = 1;

        private static final String DATABASE_CREATE =
            "create table tblBrands (_id integer primary key autoincrement, "
            + "Brand text not null );";

        private final Context context;

        private DatabaseHelper DBHelper;
        private SQLiteDatabase db;

        public DataBaseHandler(Context ctx)
        {
            this.context = ctx;
            DBHelper = new DatabaseHelper(context);
        }

     private static class DatabaseHelper extends SQLiteOpenHelper
        {
            DatabaseHelper(Context context)
            {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }

            @Override
            public void onCreate(SQLiteDatabase db)
            {
                db.execSQL(DATABASE_CREATE);
            }

            @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 tblBrands");
                onCreate(db);
            }
        }
    //---opens the database---
     public DataBaseHandler open() throws SQLException
     {
         db = DBHelper.getWritableDatabase();
         return this;
     }

     //---closes the database---
     public void close()
     {
         DBHelper.close();
     }
    //---insert a title into the database---
     public long InsertBrand(String Brand)
     {
         ContentValues initialValues = new ContentValues();
         initialValues.put(KEY_BRAND, Brand);
         return db.insert(DATABASE_TABLE, null, initialValues);
     }

     public int getAllEntries()
     {
         Cursor cursor = db.rawQuery(
                     "SELECT COUNT(Brand) FROM tblBrands", null);
                 if(cursor.moveToFirst()) {
                     return cursor.getInt(0);
                 }
                 return cursor.getInt(0);

     }
     public String getRandomEntry()
        {

            id = getAllEntries();
            Random random = new Random();
             int rand = random.nextInt(getAllEntries());
                if(rand == 0)
                ++rand;
                Cursor cursor = db.rawQuery(
                        "SELECT Brand FROM tblBrands WHERE _id = " + rand, null);
                    if(cursor.moveToFirst()) {
                        return cursor.getString(0);
                    }
                    return cursor.getString(0);

        }

    }


I had the same problem on AVD Emulator. Solution that worked for me was to uninstall the app using the applications/manage applications choosing the application fro the applications list and hitting the uninstall option on the emulator then re-installed the app and all is good. Everything just worked!