我有一個插入我的價值到我的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;
}
}
我不知道,你可以用'Count'作爲表名。 –
在我使用「countings」之前,問題仍然存在......所以我只是嘗試了一個不同的名字...... –
刪除所有'while(true)'...從來沒有沒有很好的理由這樣做 –