sqlite 数据库的操作

前端之家收集整理的这篇文章主要介绍了sqlite 数据库的操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

********创建***********

import android.content.Context;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteOpenHelper;
import android.database.sqlite.sqliteDatabase.CursorFactory;

public class DataBaSEOpenHelper extends sqliteOpenHelper {
public static final String DATABASE_NAME = "my.db";
/**
* @param context 上下文
* @param name 数据库名称
* @param factory 游标工厂 --CursorFactory factory一般情况下是不需要指定的
* @param version 版本 当我们的版本号更新的时候系统会自动调用onCreate(sqliteDatabase db)这个方法
*
*/
public DataBaSEOpenHelper(Context context,int version) {
super(context,DATABASE_NAME,null,version);
}
/**
*1. 这个方法用于初始化数据库表,表结构的创建
*/
@Override
public void onCreate(sqliteDatabase db) {
db.execsql("CREATE TABLE person (personid integer primary key autoincrement,name varchar(20),age integer)");

}
/**
* 当版本号更改的时候会调用这个方法@Override
*/
public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
db.execsql("DROP TABLE IF EXISTS person");
onCreate(db);
}

}

********操作***********

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import cn.itcast.domain.Person;
import cn.itcast.sqlite.opendb.DataBaSEOpenHelper;

public class DataBaseService {private DataBaSEOpenHelper opendb;private sqliteDatabase db ;public DataBaseService(DataBaSEOpenHelper opendb) {this.opendb = opendb;db = this.opendb.getReadableDatabase();}public void save(Person person) {db.execsql("insert into person(name,age) values(?,?)",new Object[]{person.getName(),person.getAge()});}public void delete(Integer id) {db.execsql("delete from person where personid=?",new Object[]{id});}public void update(Person person) {db.execsql("update person set name=?,age=? where personid=?",person.getAge(),person.getId()});}public Cursor getCursor(int firstResult,int maxResult) {Cursor cursor = db.rawQuery("select personid as _id,name,age from person limit ?,?",new String[] {firstResult+"",maxResult+""} );return cursor;}public Person getPerson(Integer id) {Person person = null;Cursor cursor = db.rawQuery("select * from person where personid=?",new String[] {id+""} );while(cursor.moveToNext()) {person = new Person();person.setId(id);person.setName(cursor.getString(cursor.getColumnIndex("name")));person.setAge(cursor.getInt(cursor.getColumnIndex("age")));}return person;}public List<Person> findPersons(int firstResult,int maxResult) {List<Person> persons = new ArrayList<Person>();Person person = null;Cursor cursor = db.rawQuery("select personid as _id,maxResult+""} );while(cursor.moveToNext()) {person = new Person();person.setId(cursor.getInt(cursor.getColumnIndex("personid")));person.setName(cursor.getString(cursor.getColumnIndex("name")));person.setAge(cursor.getInt(cursor.getColumnIndex("age")));persons.add(person);}return persons;}public long getCount() {Cursor cursor = db.rawQuery("select count(*) from person",null);cursor.moveToFirst();return cursor.getLong(0);}}

猜你在找的Sqlite相关文章