SQLite详解
一、新建SQLite操作类(继承SQLiteOpenHelper)
1 public class SQLiteTest extends SQLiteOpenHelper { 2 final static String DB_NAME = "test.db";//数据库名 3 private int version = 1;//数据库版本名 4 public SQLiteTest(Context context) {//当在别处调用时,只要传上下文就行了 5 super(context, DB_NAME, null, 1); 6 } 7 8 @Override 9 public void onCreate(SQLiteDatabase db) { 10 //建表 11 String sql = "create table if not exists students(" + 12 "s_id integer primary key autoincrement," + 13 "s_name varchar(30)," + 14 "s_sex integer," + 15 "s_age integer)"; 16 db.execSQL(sql); 17 } 18 19 @Override 20 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 21 // 升级数据库 22 db.execSQL("drop table if exeits students"); 23 this.onCreate(db); 24 25 } 26 27 }
二、新建对象类(比如是学生数据库就新建学生类)
注意点:get和set方法、构造方法
三、对象操作类
1 public class StudentDao { 2 private Context context; 3 SQLiteTest db ; 4 SQLiteDatabase sb; 5 public StudentDao(Context context) { 6 super(); 7 this.context = context; 8 db = new SQLiteTest(context); 9 sb = db.getWritableDatabase(); 10 } 11 //添加数据 12 public void addStudent(Student s){ 13 String sql = "insert into students values(null,?,?,?)"; 14 sb.execSQL(sql,new Object[]{s.getS_name(),s.getS_sex(),s.getS_age()}); 15 } 16 //删 17 public void delStudent(int id){ 18 String sql = "delete from students where s_id=?"; 19 sb.execSQL(sql,new Object[]{id}); 20 } 21 //改 22 public void updateStudent(Student s){ 23 String sql = "update students set s_name=?,s_sex=?,s_age=? where s_id=?)"; 24 sb.execSQL(sql,new Object[]{s.getS_name(),s.getS_sex(),s.getS_age(),s.getS_id()}); 25 } 26 //查 27 public List<Student> getAllStudent(){ 28 List<Student> list = new ArrayList<Student>(); 29 Student s; 30 String sql = "select * from students"; 31 //Cursor邮标 32 Cursor c = sb.rawQuery(sql, new String[]{}); 33 while (c.moveToNext()) { 34 int id = c.getInt(c.getColumnIndex("s_id")); 35 int sex = c.getInt(c.getColumnIndex("s_sex")); 36 String showSex; 37 if(sex==0){ 38 showSex="女"; 39 }else { 40 showSex="男"; 41 } 42 int age = c.getInt(c.getColumnIndex("s_age")); 43 String name = c.getString(c.getColumnIndex("s_name")); 44 45 s = new Student(id, name, age, showSex); 46 list.add(s); 47 } 48 49 return list; 50 51 } 52 }
五、在Activity中操作它:
1 private Button btn1; 2 ListView lv; 3 StudentDao sd; 4 List<Student> list; 5 int sid; 6 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 init(); 11 12 } 13 14 public void init() { 15 sd = new StudentDao(MainActivity.this); 16 list = sd.getAllStudent(); 17 btn1 = (Button) findViewById(R.id.add); 18 lv = (ListView) findViewById(R.id.lv_student); 19 lv.setAdapter(adapter); 20 LongClick(); 21 } 22 23 24 25 public void LongClick() { 26 lv.setOnItemLongClickListener(new OnItemLongClickListener() { 27 28 @Override 29 public boolean onItemLongClick(AdapterView<?> parent, View view, 30 int position, long id) { 31 // TODO Auto-generated method stub 32 sid = list.get(position).getS_id(); 33 AlertDialog.Builder confirm = new AlertDialog.Builder( 34 MainActivity.this); 35 confirm.setTitle("删除确认"); 36 confirm.setMessage("你确认要删除此信息吗?"); 37 confirm.setNegativeButton("否", new OnClickListener() { 38 39 @Override 40 public void onClick(DialogInterface dialog, int which) { 41 // TODO Auto-generated method stub 42 43 } 44 }); 45 confirm.setPositiveButton("是", new OnClickListener() { 46 47 @Override 48 public void onClick(DialogInterface dialog, int which) { 49 // TODO Auto-generated method stub 50 51 sd.delStudent(sid); 52 init(); 53 } 54 }); 55 confirm.setCancelable(false); 56 confirm.show(); 57 return true; 58 } 59 60 }); 61 } 62 63 public void addStu(View v) { 64 65 Intent intent = new Intent(); 66 intent.setClass(MainActivity.this, NewStuActivity.class); 67 startActivity(intent); 68 } 69 70 BaseAdapter adapter = new BaseAdapter() { 71 @Override 72 public View getView(int position, View convertView, ViewGroup parent) { 73 convertView = View.inflate(MainActivity.this, R.layout.student, 74 null); 75 TextView et_id = (TextView) convertView.findViewById(R.id.tv_id); 76 TextView et_name = (TextView) convertView 77 .findViewById(R.id.tv_name); 78 TextView et_sex = (TextView) convertView.findViewById(R.id.tv_sex); 79 TextView et_age = (TextView) convertView.findViewById(R.id.tv_age); 80 81 et_id.setText(list.get(position).getS_id() + ""); 82 et_name.setText(list.get(position).getS_name()); 83 et_sex.setText(list.get(position).getShowSex()); 84 et_age.setText(list.get(position).getS_age() + ""); 85 return convertView; 86 } 87 88 @Override 89 public long getItemId(int position) { 90 // TODO Auto-generated method stub 91 return position; 92 } 93 94 @Override 95 public Object getItem(int position) { 96 // TODO Auto-generated method stub 97 return list.get(position); 98 } 99 100 @Override 101 public int getCount() { 102 // TODO Auto-generated method stub 103 return list.size(); 104 } 105 };
xml略
种一棵树最早的时间是十年前,其次是现在。