Activity
public class MainActivity extends Activity {
private MySQLiteOpenHeilper mySQLiteOpenHeilper;
private StudentDao studentDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
studentDao = new StudentDao(this);
}
public void insert(View view) {
Student student = new Student();
student.setName("张三");
student.setPwd("1234");
studentDao.add(student);
Student student1 = new Student();
student1.setName("李四");
student1.setPwd("543");
studentDao.add(student1);
Student student2 = new Student();
student2.setName("王五");
student2.setPwd("1234");
studentDao.add(student2);
Toast.makeText(MainActivity.this,"成功", 0).show();
}
public void delete(View view) {
studentDao.delete("李四");
}
public void update(View view) {
Student student = new Student();
student.setName("王五");
// student.setPwd("124235");
// student.setName("王五王五");
studentDao.update(student);
}
public void select(View view) {
List<Student> list = studentDao.select("张三", "1234");
for (int i = 0; i <list.size(); i++) {
Student student = list.get(i);
Toast.makeText(MainActivity.this, student.getName()+":"+student.getPwd(), 0).show();
}
}
}
SQLite类
public class MySQLiteOpenHeilper extends SQLiteOpenHelper {
/**
* Context context, //上下文
* String name, // 数据库 名
CursorFactory factory, //游标工厂 一般游标初始值是 null
int version // 版本号 -->(升级的版本号) 默认值最低是1
*/
public MySQLiteOpenHeilper(Context context) {
super(context, "Student.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table student1(id integer primary key autoincrement ," +
"name varchar (10), pwd varchar(10))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
dao包
public class StudentDao {
private MySQLiteOpenHeilper heilper;
private SQLiteDatabase db;
private String stu="student1";
private SQLiteDatabase read;
public StudentDao(Context context){
heilper = new MySQLiteOpenHeilper(context);
db = heilper.getWritableDatabase();
read = heilper.getReadableDatabase();
}
// 添加的方法
public int add(Student student) {
ContentValues values = new ContentValues();
values.put("name", student.getName());
values.put("pwd", student.getPwd());
int set=(int) db.insert(stu, null, values);
return set;
}
/**
* 删除的方法
*/
public void delete(String name) {
db.delete(stu,"name=?", new String []{name});
}
/**
* 修改的方法
* @return
*/
public void update(Student student) {
ContentValues values = new ContentValues();
values.put("name", "asdjasd");
// values.put("pwd", student.getPwd());
db.update(stu, values, "name=?", new String[]{student.getName()});
}
/**
* 查询的方法
*/
public List<Student> select(String name,String pwd) {
ArrayList<Student> list=new ArrayList<Student>();
Cursor query = read.query(stu, new String[]{"name","pwd"}, "name=? and pwd=?",
new String[]{name,pwd}, null, null, null);
while (query.moveToNext()) {
Student student = new Student();
String string = query.getString(query.getColumnIndex("name"));
String string2 = query.getString(query.getColumnIndex("pwd"));
student.setName(string);
student.setPwd(string2);
list.add(student);
}
return list;
}
}
内容提供者 ContentProvider
注意:使用SQLite数据库<增,删,改,查> 可以忽略这个
public class MyContentProvipter extends ContentProvider {
private static final String TAG="MyContentProvipter";
private MySQLilteOpenhelper helper;
private static String USER="user";
@Override
public boolean onCreate() {
helper = new MySQLilteOpenhelper(getContext());
return false;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = helper.getWritableDatabase();
db.delete(USER,selection , selectionArgs);
return 0;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
Log.i(TAG, "AAAAAAAAAAAAAAAAA");
SQLiteDatabase db = helper.getWritableDatabase();
db.insert(USER, null, values);
return null;
}
// 查找
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = helper.getReadableDatabase();
db.query(USER, projection, selection, selectionArgs, null, null, sortOrder);
return null;
}
// 修改
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase db = helper.getWritableDatabase();
db.update(USER, values, selection, selectionArgs);
return 0;
}
/**
* 添加的方法
* 使用数组添加多个对象
*/
@Override
public int bulkInsert(Uri uri, ContentValues[] values) {
SQLiteDatabase db = helper.getWritableDatabase();
for (int i = 0; i < values.length; i++) {
db.insert(USER, null, values[i]);
}
return 0;
}
}