ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码
当前位置:首页 >> 数据库 >> SQLite >> sqlite 增删改查完整例子。

sqlite 增删改查完整例子。(1/2)

来源:网络整理     时间:2016-09-22     关键词:

本篇文章主要介绍了" sqlite 增删改查完整例子。",主要涉及到方面的内容,对于SQLite感兴趣的同学可以参考一下: 先看效果图:PersonDao1.javapackage mm.shandong.com.testsqlsqllite.dao;import android.c...

先看效果图:

 sqlite 增删改查完整例子。

PersonDao1.java

package mm.shandong.com.testsqlsqllite.dao;

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


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

import mm.shandong.com.testsqlsqllite.entity.Person;
import mm.shandong.com.testsqlsqllite.util.StudySQLiteOpenHelper;

/**
 * Created by buyadong on 2016/8/7.
 */
public class PersonDao1 {
    Context context;
    StudySQLiteOpenHelper studySQLiteOpenHelper;

    public PersonDao1(Context context) {
        this.context = context;
        studySQLiteOpenHelper = new StudySQLiteOpenHelper(context, "androidStudy.db", 7);
    }

    public void addPerson(Person person) {
        SQLiteDatabase db = studySQLiteOpenHelper.getWritableDatabase();
        String sql = "insert into person(_id,name,sex,age,code) values(null,?,?,?,?)";
        db.execSQL(sql,new Object[]{person.getName(),
                person.getSex(), person.getAge(), person.getCode()});
    }

    public void deletePerson(Person person) {
        SQLiteDatabase db = studySQLiteOpenHelper.getWritableDatabase();
        String sql = "delete from person where _id=?";
        db.execSQL(sql, new Object[]{person.get_id()});
    }

    public void updatePerson(Person person) {
        SQLiteDatabase db = studySQLiteOpenHelper.getWritableDatabase();
        String sql = "update person set name=?,sex=?,age=?,code=? where _id=?";
        db.execSQL(sql, new Object[]{person.getName(), person.getSex(),
                person.getAge(), person.getCode(), person.get_id()});
    }

    public List<Person> getAllPerson() {
        List<Person> persons = new ArrayList<Person>();
        SQLiteDatabase db = studySQLiteOpenHelper.getWritableDatabase();
        String sql = "select * from person";
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String sex = cursor.getString(cursor.getColumnIndex("sex"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));
            String code = cursor.getString(cursor.getColumnIndex("code"));
            int _id = cursor.getInt(cursor.getColumnIndex("_id"));
            Person person = new Person(name, sex, age, code);
            person.set_id(_id);
            persons.add(person);
        }
        return persons;
    }
}

   Person实体类

package mm.shandong.com.testsqlsqllite.entity;

import java.io.Serializable;

/**
 * Created by 安卓无忧 on 2016/7/27.
 */
public class Person implements Serializable{
      private  String name;
      private  int age;
      private String code;
      private String sex;

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    private String first_letter;
      private int _id;

    public void set_id(int _id) {
        this._id = _id;
    }

    public int get_id() {

        return _id;
    }

    public void setFirst_letter(String first_letter) {
        this.first_letter = first_letter;
    }

    public String getFirst_letter() {

        return first_letter;
    }

    public Person(){

    }
    public Person(String name, int age, String code){
        this.name=name;
        this.age=age;
        this.code=code;
    }
    public Person(String name,String sex, int age, String code){
        this.name=name;
        this.age=age;
        this.code=code;
        this.sex=sex;
    }
    @Override
    public String toString() {
        return  name +" "+age+" "+code;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {

        return age;
    }

    public String getCode() {
        return code;
    }

    public String getName() {
        return name;
    }
}

 StudySQLiteOpenHelper

package mm.shandong.com.testsqlsqllite.util;

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

/**
 * Created by 安卓无忧 on 2016/7/10.
 */
public class StudySQLiteOpenHelper extends SQLiteOpenHelper {
    String dataBase_name;
    Context context;
    String create_TABLE_sql="create table person(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "name varchar(30),age integer,sex varchar(30),code varchar(30))" ;
    String delete_Sql="delete from person";

    public StudySQLiteOpenHelper(Context context, String name, int verson){
        super(context,name,null,verson);
        this.c
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
          sqLiteDatabase.execSQL(create_TABLE_sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
       

    }
}

  activity

相关图片

相关文章