SqLite数据库MVC模式

一.xml布局



    
二.创建自己的数据库帮助类继承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) {
		//如果需要改变哪一个表,要添加的字段,上边的版本号更改运行即可
		//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", null, 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,"name = ?" , new String[] {oldName});

		//关闭数据库
		sqLiteDatabase.close();
	}

	// 查询
	public void select(String name) {
		
		
		//打开数据库
		sqLiteDatabase = helper.getReadableDatabase();
		
		//通过name查询数据
		Cursor cursor = sqLiteDatabase.query("student", new String[]{"name","sex","age"}, "name = ?",new String[]{name} , null, null, 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("李四");
	}
}




你可能感兴趣的:(数据库,mvc,sqlite,dao)