//首先先创建一个数据库
public class MysqLiteOpenHelper extends sqliteOpenHelper {
public MysqLiteOpenHelper(Context context) {
super(context,"school",null,1);
}
@Override
public void onCreate(sqliteDatabase db) {
db.execsql("create table student1(id varchar(5),student varchar(10))");
System.out.println("onCreate");
}
@Override
public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
System.out.println("onUpgrade");
}
//下面提供dao方法
public class Dao {
private MysqLiteOpenHelper helper;
Context context;
public Dao(Context context) {
super();
this.context = context;
helper = new MysqLiteOpenHelper(context);
}
//添加方法
public void add(String id,String student)
{
sqliteDatabase db = helper.getWritableDatabase();
ContentValues values= new ContentValues();
values.put("id",id);
values.put("student",student);
db.insert("student1",values);
db.close();
}
//删除方法
public void delete(String id)
{
sqliteDatabase db = helper.getWritableDatabase();
db.delete("student1","id=?",new String[]{id});
db.close();
}
//查询方法
public void select()
{
sqliteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.query("student1",null);
while(cursor.moveToNext())
{
String id = cursor.getString(cursor.getColumnIndex("id"));
String student = cursor.getString(cursor.getColumnIndex("student"));
System.out.println("id:"+id+"student:"+student);
}
db.close();
}
public void update(String id,String student) { sqliteDatabase db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("student",student); db.update("student1",values,new String[]{id}); db.close(); }
原文链接:https://www.f2er.com/sqlite/198244.html