Sqlite数据库

1.在手机内存卡上建立文件夹并且新建一个数据库

//SqliteOpenHelper类是一个抽象类,该类常用于数据库的创建以及表的创建
   String path = Environment.getExternalStorageDirectory() + "/com.qiaoguo"+"/test.db";
                   SQLiteOpenHelper sqLiteOpenHelper=new SQLiteOpenHelper(MainActivity.this,path,null,1) {
                       @Override
                       public void onCreate(SQLiteDatabase db) {
                           Toast.makeText(MainActivity.this,"database was created",Toast.LENGTH_SHORT).show();
                       }

                       @Override
                       public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                           Toast.makeText(MainActivity.this,"database was updated",Toast.LENGTH_SHORT).show();
                       }

                   };
                   sqLiteOpenHelper.getReadableDatabase();
image.png

2.在oncreate 方法中创建表

     public void onCreate(SQLiteDatabase db) {
                           String sql="Create table people(_id integer primary key autoincrement,"
                                   +"name vachar(20),"+
                                   "age integer)";
                           db.execSQL(sql);
                           

3.数据库封装

package com.example.para_huang.imoocdemo;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by Para_Huang on 1/23/2017.
 */

public class StudentDao {
    private SQLiteDatabase db;

    public StudentDao(Context context){
        String path = Environment.getExternalStorageDirectory() + "/stu.db";
        SQLiteOpenHelper helper = new SQLiteOpenHelper(context,path,null,2) {
            @Override
            public void onCreate(SQLiteDatabase sqLiteDatabase) {
            }

            @Override
            public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
            }
        };
        db = helper.getReadableDatabase();
    }

    public void addStudent(Student stu){
        String sql = "insert into info_tb (name,age,gender) values(?,?,?)";
        db.execSQL(sql,new Object[]{stu.getName(),stu.getAge()+"",stu.getGender()});
    }

    public Cursor getStudent(String... strs){
        //1.查询所有(没有参数)
        String sql = "select * from info_tb ";
        //2.含条件查询(姓名/年龄/编号)(参数形式:第一个参数指明条件,第二个参数指明条件值)
        if(strs.length != 0){
            sql += " where " + strs[0] + "='" + strs[1] + "'";
        }
        Cursor c = db.rawQuery(sql,null);
        return c;
    }

    public ArrayList getStudentInList(String... strs){
        ArrayList list = new ArrayList<>();
        Cursor c = getStudent(strs);
        while (c.moveToNext()){
            int id = c.getInt(0);
            String name = c.getString(1);
            int age = c.getInt(2);
            String gender = c.getString(3);
            Student s = new Student(id,name,age,gender);
            list.add(s);
        }
        return list;
    }

    public void deleteStudent(String... strs){
        String sql  = "delete from info_tb where " + strs[0] + "='" + strs[1] + "'";
        db.execSQL(sql);
    }

    public void updateStudent(Student stu){
        String sql = "update info_tb set name=?,age=?,gender=? where _id=?";
        db.execSQL(sql,new Object[]{stu.getName(),stu.getAge(),stu.getGender(),stu.getId()});
    }

}

你可能感兴趣的:(Sqlite数据库)