坑1:
使用SQLiteOpenHelper的时候如果建立的表中存在不为空的字段,但是用ContentValues()的方式来插入数据的话恰好没有往这个字段里插入数据,那么执行后市没法往数据库里插入数据的。单是写在这里可能觉得这个问题很平常,下面以代码的形式展示出来。
MySqliteDatabase.java
1 package com.jikexueyuan.hellonotes; 2 3 import android.content.Context; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.database.sqlite.SQLiteDatabase.CursorFactory; 6 import android.database.sqlite.SQLiteOpenHelper; 7 8 public class MySqliteDatabase extends SQLiteOpenHelper { 9 public static final String _ID = "_id"; 10 public static final String CONTENT = "content"; 11 public static final String TIME = "time"; 12 public static final String TABLE = "mydiary"; 13 public static final String TABLE_DRAFT = "draft"; 14 public static final String DATABASENAME = "wyldb.db"; 15 16 public MySqliteDatabase(Context context, String name, 17 CursorFactory factory, int version) { 18 super(context, name, factory, version); 19 // TODO Auto-generated constructor stub 20 21 } 22 23 /** 24 * 三个参数的构造器 25 * 26 * @param context 27 * @param name 28 * @param factory 29 */ 30 public MySqliteDatabase(Context context) { 31 this(context, DATABASENAME, null, 1); 32 System.out.println("MySqliteDatabase.MySqliteDatabase()构造器,新建表:" 33 + DATABASENAME + "..."); 34 } 35 36 @Override 37 public void onCreate(SQLiteDatabase db) { 38 // TODO Auto-generated method stub 39 System.out.println("MySqliteDatabase.onCreate(),创建表..."); 40 db.execSQL("create table mydiary(_id integer primary key autoincrement,content text not null,time text not null)"); 41 db.execSQL("create table draft(_id integer primary key autoincrement,content text not null,time text not null)"); 42 } 43 44 @Override 45 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 46 // TODO Auto-generated method stub 47 48 } 49 50 }
DetailActivity.java
1 package com.jikexueyuan.hellonotes; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 import android.app.Activity; 7 import android.content.ContentValues; 8 import android.content.Intent; 9 import android.database.sqlite.SQLiteDatabase; 10 import android.os.Bundle; 11 import android.view.View; 12 import android.view.View.OnClickListener; 13 import android.widget.Button; 14 import android.widget.EditText; 15 import android.widget.Toast; 16 17 public class DetailActivity extends Activity implements OnClickListener { 18 EditText et_detail; 19 Button btn_edit, btn_delete, btn_detail_save; 20 String val; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 // TODO Auto-generated method stub 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.detail); 27 et_detail = (EditText) findViewById(R.id.et_detail); 28 btn_edit = (Button) findViewById(R.id.btn_edit); 29 btn_delete = (Button) findViewById(R.id.btn_delete); 30 btn_detail_save = (Button) findViewById(R.id.btn_detail_save); 31 btn_edit.setOnClickListener(this); 32 btn_delete.setOnClickListener(this); 33 btn_detail_save.setOnClickListener(this); 34 Intent intent = getIntent(); 35 val = intent.getExtras().getString(FirstActivity.KEY_CONTENT); 36 if (!val.equals("") || val == null) { 37 // Toast.makeText(this, "获取的值不为空,已经设置,,", 0).show(); 38 et_detail.setText(val); 39 et_detail.setSelection(val.length());// 设置光标到最后 40 } 41 42 } 43 44 @Override 45 public void onClick(View v) { 46 // TODO Auto-generated method stub 47 MySqliteDatabase db = new MySqliteDatabase(DetailActivity.this); 48 SQLiteDatabase sdb = db.getWritableDatabase(); 49 switch (v.getId()) { 50 case R.id.btn_edit: 51 et_detail.setEnabled(true);// 设置文本克可编辑 52 // 可编辑的时候删除按钮隐藏,保存按钮可见 53 btn_delete.setVisibility(View.GONE); 54 btn_detail_save.setVisibility(View.VISIBLE); 55 System.out.println("===val:" + val + ",===="); 56 // 同时还要删除 57 sdb.delete(MySqliteDatabase.TABLE, MySqliteDatabase.CONTENT + "=?", 58 new String[] { val });// 有报错,暂时不处理 59 break; 60 case R.id.btn_detail_save: 61 String content = et_detail.getText().toString(); 62 ContentValues cv = new ContentValues(); 63 64 SimpleDateFormat format = new SimpleDateFormat(); 65 Date date = new Date(); 66 String time = format.format(date); 67 cv.put(MySqliteDatabase.CONTENT, content); 68 /* 69 * 之前没有插入time这个值,但是因为这个表里的time字段不能够为空,导致下main的代码都 70 * 没正确执行,即不能够按照预期把文本内容插入到数据库。 71 */ 72 cv.put(MySqliteDatabase.TIME, time); 73 System.out.println("-----btn_detail_save:content= ----"); 74 sdb.insert(MySqliteDatabase.TABLE, null, cv); 75 Intent intent = new Intent(DetailActivity.this, FirstActivity.class); 76 startActivity(intent); 77 finish(); 78 break; 79 80 default: 81 break; 82 } 83 } 84 }
具体代码:http://pan.baidu.com/s/1sjIJRit
时间: 2024-10-29 19:11:52