mardi 28 juillet 2015

couldn't open database (Android Studio)

i am here as a newbie on Android Programming. Here i want to make an app that can note all of our cashflow. But i have some problem when i want to make a Spinner that have value from Database. When i run the program, report section in Android Studio give me a report "android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database", I have a little bit frustrating about this error. I have googling for it but still cant find a way to solve or maybe because i didn't understand what the tutorial says. Please master help me to solve this error with a simple word. Thanks a lot.

NB. This is the source code for Main_Activity (Where the spinner is create)

public class MainActivity extends ActionBarActivity {

private static Button BtnINewTrans;
private static Button BtnIViewCash;
private static Button BtnIAddCateg;
DatabaseHelper dbHelper = new DatabaseHelper(this);
Spinner selectCategory;
//ArrayAdapter<String> adapterCategory;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    onButtonClickButtonListener();
    select_spinner_Category();


}

/*ArrayList<String> my_array = new ArrayList<String>();
my_array = getTableValues();

Spinner My_spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter my_Adapter = new ArrayAdapter(this, R.layout.spinner_row, my_array);
My_spinner.setAdapter(my_Adapter);*/

public void select_spinner_Category (){
    ArrayList<String> arrayCategory = new ArrayList<String>();
    arrayCategory = dbHelper.getAllCategory();
    selectCategory = (Spinner) findViewById(R.id.spnCategSelect);
    ArrayAdapter adapterCategory  = new ArrayAdapter(this, android.R.layout.simple_spinner_item, arrayCategory);
   // adapterCategory = new ArrayList<String>(this, android.R.layout.simple_spinner_item, R.id.spnCategSelect, AllCategoryList);
    adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    selectCategory.setAdapter(adapterCategory);
    selectCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

    return true;
}

public void onButtonClickButtonListener(){
    BtnINewTrans = (Button)findViewById(R.id.btnNewTrans);
    BtnINewTrans.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intentNewTrans = new Intent ("com.example.ever_ncn.cashflow.NewTransaction");
                    startActivity(intentNewTrans);
                }
            }
    );

    BtnIViewCash = (Button)findViewById(R.id.btnViewCashflow);
    BtnIViewCash.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intentViewCash = new Intent ("com.example.ever_ncn.cashflow.ViewCashflow");
                    startActivity(intentViewCash);
                }
            }
    );

    BtnIAddCateg = (Button)findViewById(R.id.btnAddCateg);
    BtnIAddCateg.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intentAddCateg = new Intent ("com.example.ever_ncn.cashflow.AddCategory");
                    startActivity(intentAddCateg);
                }
            }
    );

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

And this one for Database_Helper

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String MyVillageSoftware = "MyVillageSoftware";
public static final String DATABASE_NAME = "Cashflow.db";
public static final String TABLE_Categ_NAME = "category_table";
public static final String COL1 = "CategId";
public static final String COL2 = "CategName";
public static final String COL3 = "Note";
public static final String COL4 = "Currency";
SQLiteDatabase db;


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 2);


}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("Create table " + TABLE_Categ_NAME +
            " (CategID Integer PRIMARY KEY AUTOINCREMENT, " +
            "CategName Text," +
            " Note Text," +
            " Currency Text)");

}

public boolean insertCategData(String categname, String note, String currency){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, categname);
    contentValues.put(COL3, note);
    contentValues.put(COL4, currency);
    long result = db.insert(TABLE_Categ_NAME, null, contentValues);
     if (result == 1)
         return true;
     else
         return false;

}

public ArrayList<String> getAllCategory() {

    ArrayList<String> AllCategoryList = new ArrayList<String>();
    try {
        db = SQLiteDatabase.openOrCreateDatabase(DATABASE_NAME, null, null);
        Cursor allrows = db.rawQuery("SELECT * FROM " + TABLE_Categ_NAME, null);
        System.out.println("COUNT : " + allrows.getCount());

        if (allrows.moveToFirst()) {
            do {

                String ID = allrows.getString(0);
                String Categ = allrows.getString(1);
                String Note = allrows.getString(2);
                String Curr = allrows.getString(3);
                AllCategoryList.add(Categ);

            } while (allrows.moveToNext());
        }
        allrows.close();
        db.close();
    } catch (Exception e) {


    }
    return AllCategoryList;
}

/*public ArrayList<String>getAllCategory(){
    ArrayList<String> AllCategoryList = new ArrayList<String>();
    SQLiteDatabase db = this.getReadableDatabase();
    String selectCateg="Select * FROM " +TABLE_Categ_NAME;
    Cursor cursor = db.rawQuery(selectCateg, null);
    if(cursor.getCount()>0){
        while (cursor.moveToNext()){
            String categname1=cursor.getString(cursor.getColumnIndex(COL2));
            AllCategoryList.add(COL2);

        }return AllCategoryList;
    }

    return AllCategoryList;

}*/


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

}

}

Aucun commentaire:

Enregistrer un commentaire