侧边栏壁纸
博主头像
ERP_LXKUN_JAK

自然法则,物竞天择,适者生存

  • 累计撰写 7 篇文章
  • 累计创建 1 个标签
  • 累计收到 0 条评论
标签搜索

Android 原生数据库 SQLite 简单操作,最简代码,详细中文注释,适用于初学者,代码复制可用

ERP_LXKUN_JAK
2022-08-20 / 1,023 阅读 / 3,294 字
温馨提示:
本文最后更新于 2022-08-20,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

一共三个类,其中一个是log调试类,核心源码两个类
20191204165546177
【DatabaseHelper.java】

public class DatabaseHelper extends SQLiteOpenHelper {


    public static final String DATABASE_NAME = "user";
    public static final String USER_NAME = "username";
    public static final String USER_PASSWORD = "password";

    //数据库设置
    public DatabaseHelper(Context context,  String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);  //上下文,  数据库名字(Test.db), null(默认数据库), 1(数据库版本)
    }

    //创建表
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + DATABASE_NAME + "(" + USER_NAME + " varchar(20) not null," + USER_PASSWORD + " varchar(60) not null);");

    }

    //数据库升级操作
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

【MainActivity.java】

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private SQLiteDatabase mSqLiteDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        DatabaseHelper Helper = new DatabaseHelper(this,"Test.db",null,1);  //设置数据库
        mSqLiteDatabase = Helper.getWritableDatabase();//创建一个可读,可写的数据库

    }

    private void initView() {
        button1 = findViewById(R.id.button1);
        button1.setOnClickListener(this);
        button2 = findViewById(R.id.button2);
        button2.setOnClickListener(this);
        button3 = findViewById(R.id.button3);
        button3.setOnClickListener(this);
        button4 = findViewById(R.id.button4);
        button4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button1:   //添加数据
                //Add数据
                ContentValues contentValues = new ContentValues();
                contentValues.put(DatabaseHelper.USER_NAME,"LXKUN");
                contentValues.put(DatabaseHelper.USER_PASSWORD,"123456");
                long rownum = mSqLiteDatabase.insert(DatabaseHelper.DATABASE_NAME,null,contentValues);
                if(rownum != -1){
                    Toast.makeText(MainActivity.this,"插入数据成功!",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.button2:   //查找数据
                //游标【查出来的数据集合】
                Cursor cursor = mSqLiteDatabase.query(DatabaseHelper.DATABASE_NAME, null,
                        null,null,null,null,null);
                if(cursor.moveToFirst()){  //游标移动到最开始的位置
                    int mCount = cursor.getCount();//得到数据的总条数
                    L.e("-------------  " + "用户名 " + "    密码");
                    for (int i = 0; i < mCount; i++) {
                        String username = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.USER_NAME));
                        String password = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.USER_PASSWORD));
                        L.e("------------- :" + username + "     " + password);
                    }
                }
                break;
            case R.id.button3:   //删除数据
                //String whereClassString = "username=? and password=?";
                String whereClassString = "username=?";
                String[] whereString = {"LXKUN"};

                mSqLiteDatabase.delete(DatabaseHelper.DATABASE_NAME,whereClassString,whereString);
                break;
            case R.id.button4:   //更新数据  【将username 这一列 的 LXKUN 的 USER_PASSWORD 改成 abcdef 】
                ContentValues contentValues1 = new ContentValues();
                contentValues1.put(DatabaseHelper.USER_PASSWORD,"abcdef");
                String whereClassString01 = "username=?";
                String[] whereString01 = {"LXKUN"};
                mSqLiteDatabase.update(DatabaseHelper.DATABASE_NAME,contentValues1,whereClassString01,whereString01);
                break;
        }
    }
}

【L.java】

public class L {

    public static final String TAG = "Smartbutler";  // Smart butler: 智能滤镜

    public static void e(String text){
        Log.e(TAG,text);
    }
}