2017-02-18 89 views
0

我有一個插入我的價值到我的SQLite數據庫的問題。我在監視器(Logcat)中看不到錯誤。當我點擊按鈕插入數據時,應用程序就會凍結。應用程序沒有響應,而SQLite數據庫execSQL()

這裏是我打開數據庫:

newdb = openOrCreateDatabase("Count_DB",MODE_PRIVATE,null); 
newdb.execSQL("CREATE TABLE IF NOT EXISTS Count(Name VARCHAR(50),Description VARCHAR(200),NoC VARCHAR(1000),Time VARCHAR(100));"); 

這裏是我保存的數據onClick事件:

cr.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       Vibrator n = (Vibrator)getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE); 
       n.vibrate(500); 
       DateFormat df = new SimpleDateFormat("MM/dd/yy HH:mm:ss"); 
       Date now = Calendar.getInstance().getTime(); 
       cDT = df.format(now); 
       customD = new Dialog(nCounter.this); 
       customD.setContentView(R.layout.custom_dialog); 
       DisplayMetrics metrics = getResources().getDisplayMetrics(); 
       int width = metrics.widthPixels; 
       //int height = metrics.heightPixels; 
       customD.getWindow().setLayout((6 * width)/7, RelativeLayout.LayoutParams.WRAP_CONTENT); 
       customD.setTitle("Save Counter"); 
       Button s = (Button)customD.findViewById(R.id.button); 
       s.setText(Integer.toString(x)); 
       e1 = (EditText)customD.findViewById(R.id.editText); 
       e2 = (EditText)customD.findViewById(R.id.editText2); 
       save = (Button)customD.findViewById(R.id.button3); 
       cancel = (Button)customD.findViewById(R.id.button2); 
       save.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View vv) { 
         name = e1.getText().toString(); 
         desc = e2.getText().toString(); 
         if (!name.equals("") && !desc.equals("")){ 
          InsertintoCount(); 
          while(true){ 
           Snackbar.make(vv, "Countings Saved!", Snackbar.LENGTH_LONG).show(); 
          } 
         } 
         else { 
           Toast.makeText(getApplicationContext(), "Please fill in the fields!", Toast.LENGTH_LONG).show(); 
         } 

        } 
       }); 
       cancel.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         customD.dismiss(); 
        } 
       }); 
       customD.show(); 
       return true; 
      } 
     }); 

這是InsertintoCount()方法:

public boolean InsertintoCount(){ 
    String sqlc = "INSERT INTO Count VALUES('"+name+"','"+desc+"','"+x+"','"+cDT+"');"; 
    newdb.execSQL(sqlc); 
    while(true){ 
     return true; 
    } 
} 
+0

我不知道,你可以用'Count'作爲表名。 –

+0

在我使用「countings」之前,問題仍然存在......所以我只是嘗試了一個不同的名字...... –

+0

刪除所有'while(true)'...從來沒有沒有很好的理由這樣做 –

回答

2

您在onClick事件中運行了一個無限循環。毫無疑問,您的應用沒有迴應。

刪除這個while循環:

while(true){ 
Snackbar.make(vv, "Countings Saved!", Snackbar.LENGTH_LONG).show(); 
} 
+0

是啊!當我刪除while循環時,凍結得到了停止,但是當我檢查時,值沒有被插入到數據庫中 –

+0

@AlokNaushad嘗試更改表名。 「Count」已被定義爲方法名稱。 –

+0

當我更改表名稱時,當我第一次添加該值時,它被添加,但第二次添加一個值時,它不顯示......爲什麼? –

0

我不知道你的意思是凍結,你怎麼注意到它?不過,我想作以下修改

save.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View vv) { 
        name = e1.getText().toString(); 
        desc = e2.getText().toString(); 
        if (!name.isEmpty() && !desc.isEmpty()){ 
         InsertintoCount(); 
        } else { 
         Toast.makeText(getApplicationContext(), "Please fill in the fields!", Toast.LENGTH_LONG).show(); 
        } 
       } 
      }); 

然後在InsertingCount()(也確保xcDT初始化):

public void InsertintoCount(){ 
    String sqlc = "INSERT INTO Count VALUES('"+name+"','"+desc+"','"+x+"','"+cDT+"');"; 
    newdb.execSQL(sqlc); 
} 

試試吧,告訴我們回去如何去!

+0

該應用程序只是沒有響應,我無法做任何事情。它剛剛凍結..! –

+0

我如何「初始化」它?我在課程開始時聲明瞭這些變量... –

+0

使用我提供的代碼並告訴我們它是否工作。 – JonZarate