mercredi 5 août 2015

How to store sqlite database in my android phone

I am new android developer and sorry about my English.

1] how to store SQLite database in android phone. When I install my application on mobile phone and search on mobile but database is not created on my SD card.

2]Failed to push selection: Local path doesn't exist below code I successfully database created data-->data-->package-->databases-->database name]

but I push database on my desktop Failed to push selection: Local path doesn't exist. That error occur on console.

    HotOrNot.java-->database
    ublic class HotOrNot {
        public static final String KEY_DATE = "date";
        public static final String KEY_NAME = "name";
        public static final String KEY_SALARY = "salary";
        public static final String KEY_ALLOWANCE = "allowance";
        private static final String TAG = "DBAdapter";
        private static final String DATABASE_NAME = "MegaLight";
        private static final String TABLE_SALARY = "salary";
        private static final int DATABASE_VERSION = 2;

        private DbHelper ourHelper;
        private final Context ourContext;
        private SQLiteDatabase ourDatabase;
        /*private static final String CREATE_TABLE_SALARY =
        "create table salary (date integer not null, "
        + " name text not null, salary integer not null, allowance integer not null);";
    */
        private static class DbHelper extends SQLiteOpenHelper{



            public DbHelper(Context context) {
                super(context,DATABASE_NAME, null,DATABASE_VERSION);
                // TODO Auto-generated constructor stub
            }

            @Override
            public void onCreate(SQLiteDatabase db) {
                // TODO Auto-generated method stub
                String CREATE_TABLE_SALARY = "CREATE TABLE " + TABLE_SALARY+ "("
                        + KEY_DATE + " TEXT, "
                        + KEY_NAME + " TEXT, " 
                        + KEY_SALARY + " INTEGER, "
                        + KEY_ALLOWANCE + " INTEGER " 

                        + ");";



                db.execSQL(CREATE_TABLE_SALARY);

                Log.d(TAG,"salary Table Create Successfull");
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                // TODO Auto-generated method stub
                db.execSQL("DROP TABLE IF EXISTS" + TABLE_SALARY);
                onCreate(db);
            }

        }
        public HotOrNot(Context c){
            ourContext=c;
        }
        public HotOrNot open(){
            ourHelper=new DbHelper(ourContext);
            ourDatabase=ourHelper.getWritableDatabase();
            return this;

        }
        public void close(){
            ourHelper.close();


        }

    Mainactivity.java
    Save=(Button) findViewById(R.id.btnSave);
            View=(Button) findViewById(R.id.btnView);
            Display=(Button) findViewById(R.id.Display);
            Update=(Button) findViewById(R.id.update);
            Delete=(Button) findViewById(R.id.delete);
           Save.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                boolean didItWork=true;
                // TODO Auto-generated method stub
                try{
                String date=Date.getText().toString();
                String name=Name.getText().toString();
                String salary=Salary.getText().toString();
                String allowance=Allowance.getText().toString();
                HotOrNot entry=new HotOrNot(MainActivity.this);
                entry.open();
                entry.createEntry(date,name,salary,allowance);

                entry.close();
                Date.setText("");
                Name.setText("");
                Salary.setText("");
                Allowance.setText("");
                }
                catch(Exception e){
                    didItWork=false;
                                }
                finally{
                    if(didItWork){
                        /*Dialog d=new Dialog(this);
                        d.setTitle("Heck Yea");
                        TextView tv=new TextView(this);
                        tv.setText("success");
                        d.setContentView(tv);
                        d.show();
    */              
                        Toast.makeText(getApplicationContext(), "data succssfully insert",Toast.LENGTH_SHORT).show();
                        }
                }
                }
        });

Aucun commentaire:

Enregistrer un commentaire