2017-10-08 181 views
0

Java編碼/ Android應用程序開發很新穎。我正在嘗試製作一個應用程序,它將獲取信息數據並使用SQLite進行管理。現在,當我嘗試添加信息時,什麼也沒有輸入,當我查看數據庫時,我收到「找不到數據」。無法將值插入到SQLite數據庫中?

這是我DatabaseHelper代碼:

public class DatabaseHelper extends SQLiteOpenHelper { 
    public static final String DATABASE_NAME = "database.db"; 
    public static final String TABLE_NAME = "userinfo"; 
    public static final String COL_1 = "AGE"; 
    public static final String COL_2 = "SEX"; 
    public static final String COL_3 = "HEIGHT"; 
    public static final String COL_4 = "WEIGHT"; 
    public static final String COL_5 = "TYPE"; 
    public static final String COL_6 = "ACTIVITY"; 
    public String str = null; 

    public SQLiteDatabase database; 
    public DatabaseHelper db; 

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

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("create table " + TABLE_NAME + " (AGE INTEGER, SEX TEXT, HEIGHT INTEGER, WEIGHT INTEGER, TYPE INTEGER, ACTIVITY INTEGER)"); 

    } 

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

    public boolean insertData(Integer age, String sex) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(COL_1, age); 
     contentValues.put(COL_2, sex); 
     long result = database.insert(TABLE_NAME, null, contentValues); 

     if(result == -1) 
      return false; 
     else 
      return true; 
    } 

    public boolean insertData2(Integer height, Integer weight, Integer type) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(COL_3,height); 
     contentValues.put(COL_4,weight); 
     contentValues.put(COL_5,type); 
     long result = db.insert(TABLE_NAME, null, contentValues); 
     if(result == -1) 
      return false; 
     else 
      return true; 
    } 

    public boolean insertData3(Integer activity) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(COL_6,activity); 
     long result = db.insert(TABLE_NAME, null, contentValues); 
     if(result == -1) 
      return false; 
     else 
      return true; 
    } 

    public Cursor getAllData() { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor res = db.rawQuery("select * from " + TABLE_NAME, null); 
     return res; 
    } 
} 

三種不同的「InsertData」是爲需要將數據添加到數據庫中三項活動。這是做到這一點的正確方法嗎?另外這裏是調用第一個「InsertData」的活動。

public class info1 extends AppCompatActivity { 

    DatabaseHelper myDb; 
    EditText editText13,editText14; 
    Button btnAddData; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_info1); 

     editText13 = (EditText)findViewById(R.id.editText13); 
     editText14 = (EditText)findViewById(R.id.editText14); 
     btnAddData = (Button)findViewById(R.id.button8); 
    } 

    public void AddData() { 
     btnAddData.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         boolean isInserted = myDb.insertData(Integer.parseInt(editText13.getText().toString()),editText14.getText().toString()); 
         if(isInserted == true) 
          Toast.makeText(info1.this, "Data Inserted",Toast.LENGTH_LONG).show(); 
         else 
          Toast.makeText(info1.this, "Data Not Inserted",Toast.LENGTH_LONG).show(); 
        } 
       } 
     ); 
    } 

    public void NextPage2(View view) { 
     Intent i = new Intent(info1.this, info2.class); 
     startActivity(i); 
    } 
} 

任何幫助將不勝感激!我一直盯着它幾個小時,但我不知道是什麼改變......

+1

有你把斷點或原木在您的插入方法,看看那裏發生了什麼? – codeMagic

回答

0

您似乎沒有被調用AddData方法,因此沒有添加onClickListener。

 btnAddData = (Button)findViewById(R.id.button8); 

後添加新行

 AddData(); 
+0

嗯,這肯定幫助我認爲,應用程序崩潰現在點擊按鈕,這是很好的。這意味着至少按鈕點擊正在做某件事,而這只是需要修復的代碼。謝謝,我會根據@codeMagic的建議添加日誌,看看我能否從中獲益! –