Android之采用execSQL与rawQuery方法完成数据的添删改查操作详解
2013-06-15 17:40
1371 查看
package com.zyq.service;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.zyq.voo.Person;
public class PersonService
{
private DBOpenHelper helper;
public PersonService(Context context)
{
helper=new DBOpenHelper(context);
}
/**
* 新增一条记录
* @param person
*/
public void save(Person person)
{
SQLiteDatabase db=helper.getWritableDatabase();//Create and/or open a database that will be used for reading and writing
db.execSQL("INSERT INTO person(name,phone) values(?,?)",new Object[]{person.getName().trim(),person.getPhone().trim()});//使用占位符进行转译
// db.close(); 不关数据库连接 。可以提高性能 因为创建数据库的时候的操作模式是私有的。
//代表此数据库,只能被本应用所访问 单用户的,可以维持长久的链接
}
/**
* 更新某一条记录
* @param person
*/
public void update(Person person)
{
SQLiteDatabase db=helper.getWritableDatabase();
db.execSQL("update person set phone=?,name=? where personid=?",
new Object[]{person.getPhone().trim(),person.getName().trim(),person.getId()});
}
/**
* 根据ID查询某条记录
* @param id
* @return
*/
public Person find(Integer id)
{
SQLiteDatabase db=helper.getReadableDatabase();
Cursor cursor=db.rawQuery("select * from person where personid=?", new String[]{id.toString()});//Cursor 游标和 ResultSet 很像
if(cursor.moveToFirst())//Move the cursor to the first row. This method will return false if the cursor is empty.
{
int personid=cursor.getInt(cursor.getColumnIndex("personid"));
String name=cursor.getString(cursor.getColumnIndex("name"));
String phone=cursor.getString(cursor.getColumnIndex("phone"));
return new Person(personid,name,phone);
}
return null;
}
/**
* 删除某一条记录
* @param id
*/
public void delete(Integer id)
{
SQLiteDatabase db=helper.getWritableDatabase();
db.execSQL("delete from person where personid=?",
new Object[]{id});
}
/**
* 得到记录数
* @return
*/
public long getCount()
{
SQLiteDatabase db=helper.getReadableDatabase();
Cursor cursor=db.rawQuery("select count(*) from person", null);
cursor.moveToFirst();
return cursor.getLong(0);
}
/**
* 分页查询方法 SQL语句跟MySQL的语法一样
* @return
*/
public List<Person> getScrollData(int offset,int maxResult)
{
List<Person> persons=new ArrayList<Person>();
SQLiteDatabase db=helper.getReadableDatabase();
Cursor cursor=db.rawQuery("select * from person limit ?,?",
new String[]{String.valueOf(offset),String.valueOf(maxResult)});
while (cursor.moveToNext())
{
int personid=cursor.getInt(cursor.getColumnIndex("personid"));
String name=cursor.getString(cursor.getColumnIndex("name"));
String phone=cursor.getString(cursor.getColumnIndex("phone"));
persons.add(new Person(personid,name,phone));
}
return persons;
}
}
相关文章推荐
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作 原生的查询和执行!!
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- 采用execSQL()和rawQuery()方法完成数据的添删改查
- Android 之采用execSQL和rawQuery方法完成数据的添删改查操作
- 采用execSQL()和rawQuery()方法完成数据的添删改查
- Android操作SQLite数据库(增、删、改、查、分页等)及ListView显示数据的方法详解