数据库
首页 > 数据库> > Android的Sqlite数据库使用

Android的Sqlite数据库使用

作者:互联网

使用数据库无非增删改查

1.定义表和字段

/**
 * 用来定义表名,表中数据字段
 */
public final class MyDBEntry implements BaseColumns {
    public static final String TABLE_NAME = "tb_name";
    public static final String COLUMN_NAME_NAME = "name";
    public static final String COLUMN_NAME_AGE = "age";
    public static final String COLUMN_NAME_MONEY = "money";

}

2.数据库创建以及使用

public class MyDBOpenHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "db_name.db";//数据库名字
    private static final int DATABASE_VERSION = 1;//版本号

    //新建表的语句
    private static final String SQL_CREATE_ENTRIES =
            "CREATE TABLE " + MyDBEntry.TABLE_NAME + " (" +
                    MyDBEntry._ID +" INTEGER PRIMARY KEY," +
                    MyDBEntry.COLUMN_NAME_NAME+" TEXT," +
                    MyDBEntry.COLUMN_NAME_AGE+" integer,"+
                    MyDBEntry.COLUMN_NAME_MONEY + " double)";

    //删除表的语句
    private static final String SQL_DELETE_ENTRIES =
            "DROP TABLE IF EXISTS " + MyDBEntry.TABLE_NAME;

    public MyDBOpenHelper(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRIES);
    }

    /**
     * 数据库表升级,一般删掉旧的,再创建新的
     * @param db
     * @param oldVersion
     * @param newVersion
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }

    /**
     * 增加数据
     */
    public static long insertData(Context context,String name,int age,double money){
        MyDBOpenHelper dbOpenHelper = new MyDBOpenHelper(context);
        SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(MyDBEntry.COLUMN_NAME_NAME, name);
        values.put(MyDBEntry.COLUMN_NAME_AGE, age);
        values.put(MyDBEntry.COLUMN_NAME_MONEY, money);
        long newRowId = database.insert(MyDBEntry.TABLE_NAME, null, values);//返回插入的行数,如果在插入数据时出错,会返回 -1
        dbOpenHelper.close();//关闭数据库
        return newRowId;
    }

    /**
     * 删除数据
     */
    public static int deleteData(Context context,String name){
        MyDBOpenHelper dbOpenHelper = new MyDBOpenHelper(context);
        SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
        String selection = MyDBEntry.COLUMN_NAME_NAME + " LIKE ?";
        String[] selectionArgs = { "diu" };
        int deletedRows = database.delete(MyDBEntry.TABLE_NAME, selection, selectionArgs);//返回值表示从数据库中删除的行数
        dbOpenHelper.close();//关闭数据库
        return deletedRows;
    }

    /**
     * 修改数据
     */
    public static int updateData(Context context,String oldName,String newName,int newAge,double newMoney){
        MyDBOpenHelper dbOpenHelper = new MyDBOpenHelper(context);
        SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(MyDBEntry.COLUMN_NAME_NAME, newName);
        values.put(MyDBEntry.COLUMN_NAME_AGE, newAge);
        values.put(MyDBEntry.COLUMN_NAME_MONEY, newMoney);
        String selection = MyDBEntry.COLUMN_NAME_NAME + " LIKE ?";
        String[] selectionArgs = { oldName };
        int count = database.update(
                MyDBEntry.TABLE_NAME,
                values,
                selection,
                selectionArgs);//返回值是数据库中受影响的行数
        dbOpenHelper.close();//关闭数据库
        return count;
    }
    /**
     * 查询数据
     */
    public static void queryData(Context context,String name){
        MyDBOpenHelper dbOpenHelper = new MyDBOpenHelper(context);
        SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
        //定义你要查询的表的行中哪些列的数据
        String[] projection = {
                BaseColumns._ID,
                MyDBEntry.COLUMN_NAME_NAME,
                MyDBEntry.COLUMN_NAME_AGE,
                MyDBEntry.COLUMN_NAME_MONEY
        };

        //定义要查询谁的数据
        String selection = MyDBEntry.COLUMN_NAME_NAME + " LIKE ?";
        String[] selectionArgs = { name };

        //定义排序
        String sortOrder = MyDBEntry.COLUMN_NAME_AGE + " DESC";

        Cursor cursor = database.query(
                MyDBEntry.TABLE_NAME,
                projection,//传入null 代表所有
                selection,//传入null 代表所有
                selectionArgs,//传入null 代表所有
                null,
                null,
                sortOrder
        );
        while(cursor.moveToNext()) {
            //得到某列数据所在的位置
            int idPosition = cursor.getColumnIndexOrThrow(MyDBEntry._ID);
            //获取某列的数据
            long itemId = cursor.getLong(idPosition);

            //得到某列数据所在的位置
            int namePosition = cursor.getColumnIndexOrThrow(MyDBEntry.COLUMN_NAME_NAME);
            //获取某列的数据
            cursor.getString(namePosition);

            //得到某列数据所在的位置
            int agePosition = cursor.getColumnIndexOrThrow(MyDBEntry.COLUMN_NAME_AGE);
            //获取某列的数据
            int age = cursor.getInt(agePosition);

            //得到某列数据所在的位置
            int moneyPosition = cursor.getColumnIndexOrThrow(MyDBEntry.COLUMN_NAME_MONEY);
            //获取某列的数据
            double money = cursor.getDouble(moneyPosition);

        }
        cursor.close();//关闭游标
        dbOpenHelper.close();//关闭数据库
    }

}

标签:Sqlite,NAME,COLUMN,数据库,MyDBEntry,int,Android,public,String
来源: https://www.cnblogs.com/maowuge/p/16573512.html