2017-01-04 15 views
0

私はSQLite DBファイルをアセットに入れ、アプリケーションにインポートします。 selectクエリは正常に動作し、結果を表示しますが、テーブルにデータを挿入しようとすると同時にクエリが機能しません。問題を特定できるようにエラーは表示されません。SQLiteDatabaseへのデータの追加

DatabaseHelperクラス: addProject()メソッドは、データを挿入するメソッドです。

public class DatabaseHelper extends SQLiteOpenHelper { 
    private static final int DB_VERSION=1; 
    private static String DB_PATH=""; 
    private static final String DB_NAME="AndroidProject.sqlite"; 

    //Projects Table attrib 
    String TABLE_PROJECT="Project"; 
    String KEY_PROJECT_ID = "_id"; 
    String KEY_PROJECT_NAME = "project_name"; 
    String KEY_PROJECT_DESC = "project_desc"; 
    String KEY_PROJECT_TYPE = "project_type"; 
    String KEY_PROJECT_START_DATE = "start_date"; 
    String KEY_PROJECT_END_DATE = "end_date"; 

    private SQLiteDatabase myDatabase; 
    private final Context myContext; 

    public DatabaseHelper(Context context) { 
     super(context, DB_NAME, null, DB_VERSION); 

     if (Build.VERSION.SDK_INT>=15){ 
      DB_PATH=context.getApplicationInfo().dataDir + "/databases/"; 
     } 
     else { 
      DB_PATH= Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/databases/"; 
     } 
     this.myContext = context; 
    } 


    @Override 
    public void onCreate(SQLiteDatabase db) { 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    } 

    public void checkAndCopyDatabase(){ 
     boolean doExists=checkDatabase(); 
     if (doExists){ 
      Log.d("TAG", "Database alredy exists"); 
     } 
     else{ 
      this.getReadableDatabase(); 
     } 
     try { 
      copyDatabase(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      Log.d("TAG", "Error Copying DATABASE"); 
     } 
    } 

    public boolean checkDatabase(){ 
     SQLiteDatabase checkDB=null; 
     try { 
      String myPath=DB_PATH+DB_NAME; 
      checkDB=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE); 
     }catch (SQLiteException e){ 

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

     return checkDB != null ? true : false; 
    } 

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

    public void openDatabase(){ 
     String myPath = DB_PATH + DB_NAME; 
     myDatabase=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE); 
    } 

    public synchronized void close(){ 
     if (myDatabase != null){ 
      myDatabase.close(); 
     } 

     super.close(); 
    } 

    public Cursor QueryData(String query){ 
     return myDatabase.rawQuery(query,null); 
    } 


    public void addProject(SqlProjects blog){ 

      //SQLiteDatabase db = this.getWritableDatabase(); 
      ContentValues values = new ContentValues(); 
      values.put(KEY_PROJECT_NAME, blog.get_project_name()); 
      values.put(KEY_PROJECT_DESC, blog.get_project_desc()); 
      values.put(KEY_PROJECT_TYPE, blog.get_project_type()); 
      values.put(KEY_PROJECT_START_DATE, blog.get_project_start_date()); 
      values.put(KEY_PROJECT_END_DATE, blog.get_project_end_date()); 


      try { 
       Log.d("TAG", "Adding data"); 
       myDatabase.insertOrThrow(TABLE_PROJECT, null, values); 
      } catch (SQLiteException e) { 

       e.printStackTrace(); 
      } 




     myDatabase.close(); 
    } 
} 

SqlProjectsクラスコンストラクタ、ゲッターとセッター

public class SqlProjects { 

    private DatabaseHelper helper; 

    int _id; 
    String _project_name, _project_desc,_project_type; 
    String _project_start_date,_project_end_date; 


    public SqlProjects() { 
    } 

    public SqlProjects(String _project_name, String _project_desc, String _project_type, String _project_start_date, String _project_end_date) { 
     this._project_name = _project_name; 
     this._project_desc = _project_desc; 
     this._project_type = _project_type; 
     this._project_start_date = _project_start_date; 
     this._project_end_date = _project_end_date; 
    } 


    public SqlProjects(int _id, String _project_name, String _project_desc, String _project_type, String _project_start_date, String _project_end_date) { 
     this._id = _id; 
     this._project_name = _project_name; 
     this._project_desc = _project_desc; 
     this._project_type = _project_type; 
     this._project_start_date = _project_start_date; 
     this._project_end_date = _project_end_date; 
    } 

    public int get_project_id() { 
     return _id; 
    } 

    public void set_project_id(int _project_id) { 
     this._id = _project_id; 
    } 

    public String get_project_name() { 
     return _project_name; 
    } 

    public void set_project_name(String _project_name) { 
     this._project_name = _project_name; 
    } 

    public String get_project_desc() { 
     return _project_desc; 
    } 

    public void set_project_desc(String _project_desc) { 
     this._project_desc = _project_desc; 
    } 

    public String get_project_type() { 
     return _project_type; 
    } 

    public void set_project_type(String _project_type) { 
     this._project_type = _project_type; 
    } 

    public String get_project_start_date() { 
     return _project_start_date; 
    } 

    public void set_project_start_date(String _project_start_date) { 
     this._project_start_date = _project_start_date; 
    } 

    public String get_project_end_date() { 
     return _project_end_date; 
    } 

    public void set_project_end_date(String _project_end_date) { 
     this._project_end_date = _project_end_date; 
    } 
} 

メインクラス

final DatabaseHelper db = new DatabaseHelper(Add_Project.this); 

btnAddProjects.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if (!(TextUtils.isEmpty(editName.getText().toString())) && !(TextUtils.isEmpty(editDesc.getText().toString())) && !(tvStartDate.getText().equals("")) && !(tvEndDate.getText().equals(""))) { 

        db.addProject(new SqlProjects(editName.getText().toString(), editDesc.getText().toString(), editType.getSelectedItem().toString(), tvStartDate.getText().toString(), tvEndDate.getText().toString())); 

        editName.setText(""); 
        editDesc.setText(""); 
        tvStartDate.setText(""); 
        tvEndDate.setText(""); 

        Toast.makeText(getApplicationContext(), "Added Successfully", Toast.LENGTH_SHORT).show(); 
        onBackPressed(); 
       } 
    } 
     }); 
+0

資産ファイル –

+0

からデータベースをインポートしていますか?トーストですか? –

+1

はい私は、正常に追加されたことを言う乾杯を得るが、私はデータベースを開いた場合、それはデータを追加しません。 –

答えて

0

私はあなたが挿入操作前にデータベースをオープンするのを忘れていると思います。

btnAddProjects.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (!(TextUtils.isEmpty(editName.getText().toString())) && !(TextUtils.isEmpty(editDesc.getText().toString())) && !(tvStartDate.getText().equals("")) && !(tvEndDate.getText().equals(""))) { 
       //open db before any operation 
       db.openDatabase(); 
       db.addProject(new SqlProjects(editName.getText().toString(), editDesc.getText().toString(), editType.getSelectedItem().toString(), tvStartDate.getText().toString(), tvEndDate.getText().toString())); 

       editName.setText(""); 
       editDesc.setText(""); 
       tvStartDate.setText(""); 
       tvEndDate.setText(""); 

       Toast.makeText(getApplicationContext(), "Added Successfully", Toast.LENGTH_SHORT).show(); 
       onBackPressed(); 
      } 
     } 
    }); 

とにかくチェックアウトしたようです。

+0

私は上記の行を追加しようとしましたが、それは役に立ちません。 DbHelperClass(addProject()メソッド)で 'myDatabase.openDatabase();'を試しました –

+0

@VishalKinjavdekarはDbHelperClassの中で 'open()'という新しいメソッドを作成し、このメソッドに 'myDatabase.open挿入前に –

+0

これも試してみました..うまくいきません –

関連する問題