[問題]Android 的 database 問題

看板java作者 (非常念舊)時間9年前 (2014/09/06 16:21), 9年前編輯推噓0(005)
留言5則, 2人參與, 最新討論串1/1
關於程式碼...我找一天還不知道原因,執行APP時,出現問題 點擊新增紀錄 button 時,它回傳 -1...也就是沒新增成功 = =" 請問程式碼是哪裡可能出錯呢? 謝謝。 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //建立SQLiteOpenHelper物件 dbHelper=new MyDBHelper(this); db=dbHelper.getWritableDatabase(); //取得TextView物件 output=(TextView)findViewById(R.id.lblOutput); txtTitle=(EditText)findViewById(R.id.txtTitle); txtPrice=(EditText)findViewById(R.id.txtPrice); txtNewPrice=(EditText)findViewById(R.id.txtNewPrice); } public void btn1_Click(View view){ long id; ContentValues cv=new ContentValues(); cv.put("title", txtTitle.getText().toString());; double price=Double.parseDouble(txtPrice.getText().toString()); cv.put("price", price);; id=db.insert(DATABASE_TABLE, null, cv); output.setText("新增記錄成功: "+id); } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.136.153.183 ※ 文章網址: http://www.ptt.cc/bbs/java/M.1409991717.A.77D.html

09/06 16:33, , 1F
我看了logcat他說 no such titles table.....
09/06 16:33, 1F

09/06 16:34, , 2F
可我在 MyDBHelper 類別的 onCreate已經建立 titles
09/06 16:34, 2F

09/06 16:34, , 3F
資料表了
09/06 16:34, 3F
db.execSQL("CREATE TABLE titles (_id integer primary key autoincrement, " + "title text no null, price real no null)"); ※ 編輯: gn00618777 (220.136.153.183), 09/06/2014 16:35:42

09/07 18:44, , 4F
如果再MyDBHelper constructor 建立呢?
09/07 18:44, 4F

09/07 18:45, , 5F
create table if not exist xxxx
09/07 18:45, 5F
文章代碼(AID): #1K2iGbTz (java)