android 操作sqllite 数据库
2014-07-18 13:47
169 查看
[java] view
plaincopyprint?
package com.b509.activity.patient.mydoctors.service;
public class Person {
private Integer id;
private String time;
private String describe;
private String illness;
private byte[] pic;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDeccribe() {
return describe;
}
public void setDeccribe(String deccribe) {
this.describe = deccribe;
}
public String getIllness() {
return illness;
}
public void setIllness(String illness) {
this.illness = illness;
}
public byte[] getPic() {
return pic;
}
public void setPic(byte[] pic) {
this.pic = pic;
}
public Person(){
}
public Person(Integer id, String time,String describe,String illness,byte[] pic) {
this.id = id;
this.time = time;
this.describe=describe;
this.illness=illness;
this.pic = pic;
}
public Person( String time,String describe,String illness) {
this.time = time;
this.describe=describe;
this.illness=illness;
}
}
[java] view
plaincopyprint?
package com.b509.activity.patient.mydoctors.service;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class OtherPersonService {
private DBOpenHelper dbOpenHelper;
public OtherPersonService(Context context) {
this.dbOpenHelper = new DBOpenHelper(context);
}
// ILLid time describe illness
public void save(Person person) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("ILLid", person.getId());
values.put("time", person.getTime());
values.put("describe", person.getDeccribe());
values.put("illness", person.getIllness());
db.insert("ILLNESS", null, values);
db.close();
}
// SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
public void update(Person person) {
// update person set name =? where personid =?
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("time", person.getTime());
values.put("describe", person.getDeccribe());
values.put("illness", person.getIllness());
System.out.println(person.getDeccribe());
System.out.println(person.getIllness());
System.out.println(person.getTime());
System.out.println(person.getId());
Integer i = person.getId();
String id_str = String.valueOf(i);
System.out.println(id_str + "把id转化为一个字符串");
db.update("ILLNESS", values, "ILLid=?", new String[] { id_str });
db.close();
}
public void delete(Integer id) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.delete("ILLNESS", "ILLid=?", new String[] { id.toString() });
db.close();
}
public List<Person> find2() {
// 如果只对数据进行读取,建议使用此方法
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("ILLNESS", new String[] { "time", "illness" },
null, null, null, null, "time");
List<Person> persons = new ArrayList<Person>();
// select personid,name from person where personid=? order by ... limit
// 3,5
if (cursor.moveToFirst()) {
String timeill = cursor.getString(cursor.getColumnIndex("time"));
String kindsill = cursor
.getString(cursor.getColumnIndex("illness"));
System.out.println(timeill);
System.out.println(kindsill);
Person person = new Person();
person.setTime(timeill);
person.setIllness(kindsill);
persons.add(person);
db.close();
return persons;
}
return null;
}
public Person find3(int id) {
// 如果只对数据进行读取,建议使用此方法
String str_id = id + "";
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("ILLNESS", new String[] { "time", "illness",
" describe" }, str_id, null, null, null, "time");
Person person = null;
if (cursor.moveToFirst()) {
String timeill = cursor.getString(cursor.getColumnIndex("time"));
String kindsill = cursor
.getString(cursor.getColumnIndex("illness"));
String disill = cursor.getString(cursor.getColumnIndex("describe"));
System.out.println(timeill);
System.out.println(kindsill);
person = new Person();
person.setTime(timeill);
person.setIllness(kindsill);
person.setDeccribe(disill);
return person;
}
db.close();
return null;
}
public List<Person> find() {
// 如果只对数据进行读取,建议使用此方法
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("ILLNESS", new String[] { "ILLid", "time",
"illness" }, null, null, null, null, "time");
List<Person> persons = new ArrayList<Person>();
// select personid,name from person where personid=? order by ... limit
// 3,5
if (cursor.moveToFirst()) {
Person person = new Person();
System.out.println("---------------");
String timeill = cursor.getString(cursor.getColumnIndex("time"));
Integer idill = cursor.getInt(cursor.getColumnIndex("ILLid"));
String kindsill = cursor
.getString(cursor.getColumnIndex("illness"));
System.out.println(timeill);
System.out.println(idill);
System.out.println(kindsill);
person.setTime(timeill);
person.setIllness(kindsill);
persons.add(person);
db.close();
return persons;
}
return null;
}
public List<Person> getScrollData(Integer offset, Integer maxResult) {
List<Person> persons = new ArrayList<Person>();
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("ILLNESS", new String[] { "ILLid", "time",
"illness" }, null, null, null, null, "time", offset + ","
+ maxResult);
while (cursor.moveToNext()) {
int personid = cursor.getInt(cursor.getColumnIndex("ILLid"));
String time_ill = cursor.getString(cursor.getColumnIndex("time"));
String ill = cursor.getString(cursor.getColumnIndex("illness"));
Person p = new Person();
p.setId(personid);
p.setIllness(ill);
p.setTime(time_ill);
persons.add(p);
}
cursor.close();
db.close();
return persons;
}
public long getCount() {// select count(*) from person
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
/*
* Cursor cursor = db.query("ILLNESS", new String[] { "count(*)" },
* null, null, null, null, null);
*/
Cursor cursor = db.query("ILLNESS", new String[] { "Illid" }, null,
null, null, null, null);
db.close();
return cursor.getCount();
}
}
plaincopyprint?
package com.b509.activity.patient.mydoctors.service;
public class Person {
private Integer id;
private String time;
private String describe;
private String illness;
private byte[] pic;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDeccribe() {
return describe;
}
public void setDeccribe(String deccribe) {
this.describe = deccribe;
}
public String getIllness() {
return illness;
}
public void setIllness(String illness) {
this.illness = illness;
}
public byte[] getPic() {
return pic;
}
public void setPic(byte[] pic) {
this.pic = pic;
}
public Person(){
}
public Person(Integer id, String time,String describe,String illness,byte[] pic) {
this.id = id;
this.time = time;
this.describe=describe;
this.illness=illness;
this.pic = pic;
}
public Person( String time,String describe,String illness) {
this.time = time;
this.describe=describe;
this.illness=illness;
}
}
[java] view
plaincopyprint?
package com.b509.activity.patient.mydoctors.service;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class OtherPersonService {
private DBOpenHelper dbOpenHelper;
public OtherPersonService(Context context) {
this.dbOpenHelper = new DBOpenHelper(context);
}
// ILLid time describe illness
public void save(Person person) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("ILLid", person.getId());
values.put("time", person.getTime());
values.put("describe", person.getDeccribe());
values.put("illness", person.getIllness());
db.insert("ILLNESS", null, values);
db.close();
}
// SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
public void update(Person person) {
// update person set name =? where personid =?
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("time", person.getTime());
values.put("describe", person.getDeccribe());
values.put("illness", person.getIllness());
System.out.println(person.getDeccribe());
System.out.println(person.getIllness());
System.out.println(person.getTime());
System.out.println(person.getId());
Integer i = person.getId();
String id_str = String.valueOf(i);
System.out.println(id_str + "把id转化为一个字符串");
db.update("ILLNESS", values, "ILLid=?", new String[] { id_str });
db.close();
}
public void delete(Integer id) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.delete("ILLNESS", "ILLid=?", new String[] { id.toString() });
db.close();
}
public List<Person> find2() {
// 如果只对数据进行读取,建议使用此方法
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("ILLNESS", new String[] { "time", "illness" },
null, null, null, null, "time");
List<Person> persons = new ArrayList<Person>();
// select personid,name from person where personid=? order by ... limit
// 3,5
if (cursor.moveToFirst()) {
String timeill = cursor.getString(cursor.getColumnIndex("time"));
String kindsill = cursor
.getString(cursor.getColumnIndex("illness"));
System.out.println(timeill);
System.out.println(kindsill);
Person person = new Person();
person.setTime(timeill);
person.setIllness(kindsill);
persons.add(person);
db.close();
return persons;
}
return null;
}
public Person find3(int id) {
// 如果只对数据进行读取,建议使用此方法
String str_id = id + "";
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("ILLNESS", new String[] { "time", "illness",
" describe" }, str_id, null, null, null, "time");
Person person = null;
if (cursor.moveToFirst()) {
String timeill = cursor.getString(cursor.getColumnIndex("time"));
String kindsill = cursor
.getString(cursor.getColumnIndex("illness"));
String disill = cursor.getString(cursor.getColumnIndex("describe"));
System.out.println(timeill);
System.out.println(kindsill);
person = new Person();
person.setTime(timeill);
person.setIllness(kindsill);
person.setDeccribe(disill);
return person;
}
db.close();
return null;
}
public List<Person> find() {
// 如果只对数据进行读取,建议使用此方法
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("ILLNESS", new String[] { "ILLid", "time",
"illness" }, null, null, null, null, "time");
List<Person> persons = new ArrayList<Person>();
// select personid,name from person where personid=? order by ... limit
// 3,5
if (cursor.moveToFirst()) {
Person person = new Person();
System.out.println("---------------");
String timeill = cursor.getString(cursor.getColumnIndex("time"));
Integer idill = cursor.getInt(cursor.getColumnIndex("ILLid"));
String kindsill = cursor
.getString(cursor.getColumnIndex("illness"));
System.out.println(timeill);
System.out.println(idill);
System.out.println(kindsill);
person.setTime(timeill);
person.setIllness(kindsill);
persons.add(person);
db.close();
return persons;
}
return null;
}
public List<Person> getScrollData(Integer offset, Integer maxResult) {
List<Person> persons = new ArrayList<Person>();
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.query("ILLNESS", new String[] { "ILLid", "time",
"illness" }, null, null, null, null, "time", offset + ","
+ maxResult);
while (cursor.moveToNext()) {
int personid = cursor.getInt(cursor.getColumnIndex("ILLid"));
String time_ill = cursor.getString(cursor.getColumnIndex("time"));
String ill = cursor.getString(cursor.getColumnIndex("illness"));
Person p = new Person();
p.setId(personid);
p.setIllness(ill);
p.setTime(time_ill);
persons.add(p);
}
cursor.close();
db.close();
return persons;
}
public long getCount() {// select count(*) from person
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
/*
* Cursor cursor = db.query("ILLNESS", new String[] { "count(*)" },
* null, null, null, null, null);
*/
Cursor cursor = db.query("ILLNESS", new String[] { "Illid" }, null,
null, null, null, null);
db.close();
return cursor.getCount();
}
}
相关文章推荐
- Android数据库(SqlLite)操作
- Android 操作sqllite数据库
- 对android内置的sqlLite数据库进行增、删、改、查操作
- Android数据库(SqlLite)操作和db文件查看
- Android基础教程——数据存储之操作数据库SQLlite(一)
- android 操作sqllite 数据库
- Android知识点八(sqllite数据库操作,以及sqlite+handler+XListView实现上拉刷新下拉加载)
- Android复习笔记(5)-数据库操作 sqllite
- android数据库事务操作详解
- Android教程之SQlit数据库操作
- Android 中短信数据库的简单操作
- sqlLite数据库操作--项目中小练
- Android数据库的批处理操作(以添加联系人为例)
- Android获取操作数据库的SQLiteDatabase实例
- android SQLite数据库基本操作示例
- 快速上手Android数据库操作
- android adb数据库操作
- Android数据库的批处理操作(以添加联系人为例)
- 快速上手Android数据库操作
- android 数据库技术 | 文件系统,SDCARD访问,CONTENT PROVIDER,SQLITE ,CURSOR操作,CURSOR适配器,SQLITEHELPER,数据库设计模式