一.xml布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="insert" android:text="插入" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="delete" android:text="删除" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="update" android:text="修改" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="select" android:text="查询" /> </LinearLayout>二.创建自己的数据库帮助类继承sqliteOpenHelper
public class MysqLiteOpenHelper extends sqliteOpenHelper { //创建数据库名字数字参数为版本号不可以为0 public MysqLiteOpenHelper(Context context) { super(context,"1409L",null,1); // TODO Auto-generated constructor stub } @Override public void onCreate(sqliteDatabase db) { //创建表名和字段 db.execsql("create table student(id integer primary key autoincrement,name varchar (20),sex varchar(6),age char(5)) "); } //版本更新运行下面的方法 @Override public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { //如果需要改变哪一个表,要添加的字段,上边的版本号更改运行即可 <span style="white-space:pre"> </span>//db.execsql("alter table student add age varchar(6)"); } }三,创建dao层里面有增删改查:
public class StudentDao { private sqliteDatabase sqliteDatabase; private MysqLiteOpenHelper helper; // 构造函数 public StudentDao(Context context) { helper = new MysqLiteOpenHelper(context); } // 插入,通过方法传参的方式,插入数据 public void insert(String name,String sex,String age) { //打开数据库 sqliteDatabase = helper.getReadableDatabase(); ContentValues values = new ContentValues(); values.put("name",name); values.put("sex",sex); values.put("age",age); sqliteDatabase.insert("student",values); //关闭数据库 sqliteDatabase.close(); } // 删除 public void delete(String name) { //打开数据库 sqliteDatabase = helper.getReadableDatabase(); sqliteDatabase.delete("student","name = ?",new String[] { name }); //关闭数据库 sqliteDatabase.close(); } // 修改 public void update(String newName,String oldName) { //打开数据库 sqliteDatabase = helper.getReadableDatabase(); ContentValues values = new ContentValues(); values.put("name",newName); //把姓名(name)是oldName的数据,改成为姓名(name)是newName sqliteDatabase.update("student",values,new String[] {oldName}); //关闭数据库 sqliteDatabase.close(); } // 查询 public void select(String name) { //打开数据库 sqliteDatabase = helper.getReadableDatabase(); //通过name查询数据 Cursor cursor = sqliteDatabase.query("student",new String[]{"name","sex","age"},new String[]{name},null); while (cursor.moveToNext()) { //先得到列名的角标,在通过列名角标,获得该列名的数据 String userName = cursor.getString(cursor.getColumnIndex("name")); String sex = cursor.getString(cursor.getColumnIndex("sex")); String age = cursor.getString(cursor.getColumnIndex("age")); System.out.println(userName+sex+age); } //关闭数据库 sqliteDatabase.close(); } }四,在主activity进行点击调用
public class MainActivity extends ActionBarActivity { private StudentDao dao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dao = new StudentDao(this); } // 插入 public void insert(View v) { dao.insert("张三","男","12"); } // 删除 public void delete(View v) { dao.delete("李四"); } // 修改 public void update(View v) { dao.update("李四","张三"); } // 查询 public void select(View v) { dao.select("李四"); } }