7.android数据库操作实例

1. 在使用数据库时必须创建一个操作类继承SQLiteOpenHelper

package cn.service;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class dbhelper extends SQLiteOpenHelper {

public dbhelper(Context context) {
  super(context, "数据库名", null, num);   //int num 版本号,当版本号发生变化时数据库会自动调用onUpgrade()方法
// TODO Auto-generated constructor stub
}


//数据库第一次创建时调用onCreate()

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person(personid integer primary key autoincrement,name varchar(20))");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("alter table person add phone varchar(12)");

}

}


2.根据实际情况为每个表创建类对象

package cn.domain;

public class person {
public Integer personid;
public String name;
public String phone;

public person(String name, String phone) {

this.name = name;
this.phone = phone;
}
public person(Integer personid, String name, String phone) {
this.personid = personid;
this.name = name;
this.phone = phone;
}
public Integer getPersonid() {
return personid;
}
public void setPersonid(Integer personid) {
this.personid = personid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "person [personid=" + personid + ", name=" + name + ", phone="
+ phone + "]";
}

}

3.为每个表创建数据库操作方法

package cn.domain;

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

import cn.service.dbhelper;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class personservice {
public dbhelper dber;

public personservice(Context context) {
this.dber = new dbhelper(context);
}

public void save(person p) {
SQLiteDatabase db = dber.getWritableDatabase();
db.execSQL("insert into person(name,phone) values(?,?)", new Object[] {
p.getName(), p.getPhone() });
db.close();
}

public void delete(Integer id) {
SQLiteDatabase db = dber.getWritableDatabase();
db.execSQL("delete from person where personid=?", new Object[] { id });
db.close();
}

public void update(person p) {
SQLiteDatabase db = dber.getWritableDatabase();
db.execSQL("update person set name=?,phone=? where personid=?",
new Object[] { p.getName(), p.getPhone(), p.getPersonid() });
db.close();
}

public person find(Integer id) {
SQLiteDatabase db = dber.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from person where personid=?",
new String[] { id.toString() });
if (cursor.moveToFirst()) {
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;
}

public List<person> getscrolldata(int offset, int maxresult) {
List<person> persons = new ArrayList<person>();
SQLiteDatabase db = dber.getReadableDatabase();
Cursor cursor = db.rawQuery(
"select * from person order by personid asc 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));
}
cursor.close();
return persons;

}

public long getcount() {
SQLiteDatabase db = dber.getReadableDatabase();
Cursor cursor = db.rawQuery("select count(*) from person", null);
cursor.moveToFirst();
long result = cursor.getLong(0);
return result;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值