Android小筆記數據庫

//創建表

create table    person(_id  integer  primary key  autoincrement,name varchar(32),age  integer)

//插入數據

insert into person values(1,"bojie",18)

insert into person values(2,"pange",18)

//刪除數據

delete from person where name="pange"

//查詢數據

select  name  from person where _id=2

//修改數據

update person set name="pange+tanji"   where name="pange"


    sqlite

        

        public class MySqliteHelper extends SQLiteOpenHelper {

        /**

         * version  1

         * factory 數據查找的遊標    默認null>>> cursor 

         * name  數據庫名

         * @param context

         */

        public MySqliteHelper(Context context) {

        super(context, "my.db", null, 1);

        // TODO Auto-generated constructor stub

        }

        @Override

        public void onCreate(SQLiteDatabase db) {

        db.execSQL("create table person (id  integer primary key  autoincrement,name varchar(32),sex varchar(30),age varchar(30),phone varchar(30), qq varchar(30))");

        

        }

     Dao層


public class PersonService {


private MySqliteHelper helper;


public PersonService(Context context) {

helper = new MySqliteHelper(context);

}

/**

 * 插入數據

 * @param p

 */

public void insert(Persons p) {

SQLiteDatabase db = helper.getWritableDatabase();

if (db.isOpen()) {

db.execSQL(

"insert  into person values (null,?,?,?,?,?)",

new Object[] { p.getName(), p.getSex(), p.getAge(),

p.getQq(), p.getPhone() }); 

}

db.close();

}

/**

* 刪除數據

* @param name

*/

public void del(int id) {

SQLiteDatabase db = helper.getWritableDatabase();

if (db.isOpen()) {

db.execSQL("delete from person where id=?", new Object[] { id });

}

db.close();

}

/**

* 修改數據

* @param oldName

* @param newName

*/

public void update(String oldName, String newName) {


SQLiteDatabase db = helper.getWritableDatabase();


if (db.isOpen()) {

db.execSQL("update person set  name=? where name=?", new String[] {

newName, oldName });

}

db.close();

}

/**

* 查詢所有數據

* @return

*/

public List<Persons> getAll() {

List<Persons> lists = new ArrayList<Persons>();

SQLiteDatabase db = helper.getReadableDatabase();


if (db.isOpen()) {

Cursor cursor = db.rawQuery(

"select * from person", null);

if (cursor != null && cursor.getCount() > 0) {

while (cursor.moveToNext()) {

Persons person = new Persons();

person.setId(cursor.getInt(0));

person.setName(cursor.getString(1));

person.setSex(cursor.getString(2));

person.setAge(cursor.getString(3));

person.setQq(cursor.getString(4));

person.setPhone(cursor.getString(5));

lists.add(person);

}

}

cursor.close();

return lists;


}

db.close();

return null;


}

/**

* 查詢對應的數據

* @param id

* @return

*/

public Persons getPersonById(int id) {


SQLiteDatabase db = helper.getReadableDatabase();

Persons person = new Persons();

if (db.isOpen()) {

Cursor cursor = db.rawQuery(

"select name,sex,age,phone,qq from person where id=? ",

new String[] { String.valueOf(id) });

if (cursor != null && cursor.getCount() > 0) {

if (cursor.moveToLast()) {


String name = cursor.getString(0);

person.setName(name);

String sex = cursor.getString(1);

person.setSex(sex);

String age = cursor.getString(2);

person.setAge(age);

String phone = cursor.getString(3);

person.setPhone(phone);

String qq = cursor.getString(4);

person.setQq(qq);

}


}

cursor.close();

return person;


}

db.close();

return null;


}

//此處爲Persons類:

public class Persons {

private int id;

private String name;

private String sex;

private String age;

private String qq;

private String phone;

public Persons(){

super();

}

public Persons(String name, String sex, String age, String qq,String phone) {

this.name=name;

this.sex=sex;

this.age=age;

this.qq=qq;

this.phone=phone;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSex() {

return sex;

}

public void setSex(String sex) {

this.sex = sex;

}

public String getAge() {

return age;

}

public void setAge(String age) {

this.age = age;

}

public String getQq() {

return qq;

}

public void setQq(String qq) {

this.qq = qq;

}

public String getPhone() {

return phone;

}

public void setPhone(String phone) {

this.phone = phone;

}

@Override

public String toString() {

return "Persons [name=" + name + ", sex=" + sex + ", age=" + age

+ ", qq=" + qq + ", phone=" + phone + "]";

}

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章