如何在我的Android手机中存储sqlite数据库

时间:2015-08-06 04:01:30

标签: java android database sqlite

我是新的Android开发人员,对我的英语感到抱歉。

  1. 如何在Android手机中存储SQLite数据库。     当我在手机上安装我的应用程序并在手机上搜索但我的SD卡上没有创建数据库时。

  2. 无法推送选择:本地路径不存在      下面代码我成功数据库 created data-->data-->package-->databases-->database name

  3. 但我在桌面上推送数据库无法推送选择:本地路径不存在。该错误发生在控制台上。

        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();
                            }
                    }
                    }
            });
    

0 个答案:

没有答案