数据库操作的封装

创建表

private void createTable(SQLiteDatabase db){   
//创建表SQL语句   
String stu_table="create table usertable(_id integer primary key autoincrement,sname text,snumber text)";   
//执行SQL语句   
db.execSQL(stu_table);   
} 

插入一条数据

private boolean insert(SQLiteDatabase db){   
//实例化常量值   
ContentValues cValue = new ContentValues();   
//添加用户名   
cValue.put("sname","xiaoming");   
//添加密码   
cValue.put("snumber","01005");   
//调用insert()方法插入数据   
db.insert("stu_table",null,cValue);   
} 

删除数据

private boolean delete(SQLiteDatabase db) {   
//删除条件   
String whereClause = "id=?";   
//删除条件参数   
String[] whereArgs = {String.valueOf(2)};   
//执行删除   
db.delete("stu_table",whereClause,whereArgs);   
}  

修改数据

private boolean update(SQLiteDatabase db) {   
//实例化内容值 ContentValues values = new ContentValues();   
//在values中添加内容   
values.put("snumber","101003");   
//修改条件   
String whereClause = "id=?";   
//修改添加参数   
String[] whereArgs={String.valuesOf(1)};   
//修改   
db.update("usertable",values,whereClause,whereArgs);   
}  

查询指定数据

private void queryAll(SQLiteDatabase db){
Cursor cur = mysql.rawQuery("SELECT * FROM classes WHERE name = 'aab';", null);     
if (cur != null) 
{   
    while (cur.moveToNext()) 
    {    
        address = cur.getString(4);   
    }   
    Toast.makeText(TimetableActivity.this, "0", Toast.LENGTH_LONG).show();  
}    
else
{      
    Toast.makeText(TimetableActivity.this, "1", Toast.LENGTH_LONG).show();     
}     
cur.close();
}

查询数据

private void queryAll(SQLiteDatabase db) {   
//查询获得游标   
Cursor cursor = db.query ("usertable",null,null,null,null,null,null);   

//判断游标是否为空   
if(cursor.moveToFirst() {   
//遍历游标   
for(int i=0;i

删除数据

private void drop(SQLiteDatabase db){   
//删除表的SQL语句   
String sql ="DROP TABLE stu_table";   
//执行SQL   
db.execSQL(sql);   
}

你可能感兴趣的:(Android应用开发)