首页 > 其他分享 >2月22日总结-补

2月22日总结-补

时间:2023-02-23 08:11:47浏览次数:28  
标签:总结 name 22 cursor values user mWDB null

代码量:100行
现在是23日早上8点,来补充上昨日的总结。昨日除了上课之外学习了一些android的数据库部分内容,其中一个重要的是SQLiteOpenHelper,其与jdbc的操作非常类似.
示例代码如下:

package com.example.chapter06.database;

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

import com.example.chapter06.entity.User;

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

public class UserDBHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "user.db";
    private static final String TABLE_NAME = "user_info";
    private static final int DB_VERSION = 2;
    private static UserDBHelper mHelper = null;
    private SQLiteDatabase mRDB = null;
    private SQLiteDatabase mWDB = null;

    public UserDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public static UserDBHelper getInstance(Context context) {
        if (mHelper == null) {
            mHelper = new UserDBHelper(context);
        }
        return mHelper;
    }

    public SQLiteDatabase openReadLink() {
        if (mRDB == null || !mRDB.isOpen()) {
            mRDB = mHelper.getReadableDatabase();
        }
        return mRDB;
    }

    public SQLiteDatabase openWriteLink() {
        if (mWDB == null || !mWDB.isOpen()) {
            mWDB = mHelper.getWritableDatabase();
        }
        return mWDB;
    }

    public void closeLink() {
        if (mRDB != null && mRDB.isOpen()) {
            mRDB.close();
            mRDB = null;
        }
        if (mWDB != null && mWDB.isOpen()) {
            mWDB.close();
            mWDB = null;
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table if not exists " + TABLE_NAME + "(" +
                "_id INTEGER primary key AUTOINCREMENT NOT NULL," +
                "name VARCHAR NOT NULL," +
                "age INTEGER NOT NULL," +
                "height LONG NOT NULL," +
                "weight FLOAT NOT NULL," +
                "married INTEGER NOT NULL);";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        String sql = "ALTER TABLE "+ TABLE_NAME+" ADD COLUMN phone VARCHAR;";

        db.execSQL(sql);

        sql = "ALTER TABLE "+ TABLE_NAME+" ADD COLUMN password VARCHAR;";

        db.execSQL(sql);

    }

    public long insert(User user) {
        ContentValues values = new ContentValues();
        values.put("name", user.name);
        values.put("age", user.age);
        values.put("height", user.height);
        values.put("weight", user.weight);
        values.put("married", user.married);
//        return mWDB.insert(TABLE_NAME, null, values);

        try{
            mWDB.beginTransaction();

            mWDB.insert(TABLE_NAME, null, values);

//            int i = 10/0;

            mWDB.insert(TABLE_NAME, null, values);

            mWDB.setTransactionSuccessful();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            mWDB.endTransaction();
        }

        return 1;
    }

    public long deleteByName(String name) {
        return mWDB.delete(TABLE_NAME, "name=?", new String[]{name});
    }

    public long upd(User user) {
        ContentValues values = new ContentValues();
        values.put("name", user.name);
        values.put("age", user.age);
        values.put("height", user.height);
        values.put("weight", user.weight);
        values.put("married", user.married);
        return mWDB.update(TABLE_NAME, values, "name=?", new String[]{user.name});
    }

    public List<User> queryAll() {
        List<User> list = new ArrayList<>();

        Cursor cursor = mRDB.query(TABLE_NAME, null, null, null, null, null, null);

        while (cursor.moveToNext()){
            User user = new User();
            user.id = cursor.getInt(0);
            user.name = cursor.getString(1);
            user.age = cursor.getInt(2);
            user.height = cursor.getLong(3);
            user.weight = cursor.getFloat(4);

            user.married = cursor.getInt(5) == 0 ? false : true;

            list.add(user);
        }


        return list;
    }
    public List<User> queryByName(String name ) {
        List<User> list = new ArrayList<>();

        Cursor cursor = mRDB.query(TABLE_NAME, null, "name=?", new String[]{name}, null, null, null);

        while (cursor.moveToNext()){
            User user = new User();
            user.id = cursor.getInt(0);
            user.name = cursor.getString(1);
            user.age = cursor.getInt(2);
            user.height = cursor.getLong(3);
            user.weight = cursor.getFloat(4);

            user.married = cursor.getInt(5) == 0 ? false : true;

            list.add(user);
        }


        return list;
    }


}

标签:总结,name,22,cursor,values,user,mWDB,null
From: https://www.cnblogs.com/lmyy/p/17146618.html

相关文章