作者gn00618777 (非常念旧)
看板java
标题[问题]Android 的 database 问题
时间Sat Sep 6 16:21:52 2014
关於程式码...我找一天还不知道原因,执行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://webptt.com/cn.aspx?n=bbs/java/M.1409991717.A.77D.html
1F:→ gn00618777: 我看了logcat他说 no such titles table..... 09/06 16:33
2F:→ gn00618777: 可我在 MyDBHelper 类别的 onCreate已经建立 titles 09/06 16:34
3F:→ gn00618777: 资料表了 09/06 16:34
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
4F:→ qweqweqweqwe: 如果再MyDBHelper constructor 建立呢? 09/07 18:44
5F:→ qweqweqweqwe: create table if not exist xxxx 09/07 18:45