Android studio的数据库的创建以及增删改查

Android studio的数据库的创建以及增删改查

MainActivity

package com.example.dday09;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private SQLiteDatabase readableDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //相当于建立连接
        MySqlhelp mySqlhelp = new MySqlhelp(this,"user.db",null,1);
        //打开 相当于数据库创建完成
        readableDatabase = mySqlhelp.getReadableDatabase();
    }

//    public void btn1(View view) {
//
        MySqlhelp mySqlhelp = new MySqlhelp(this,"user.db",null,1);
//
//
//
//    }
    //创建表

    public void createTable(View view) {
        String sql = "create table student(id integer primary key autoincrement,name varchar(20))";
        readableDatabase.execSQL(sql);
    }

    public void insert(View view) {
        //插入数据
//        String sql = "insert into student values(null,'xiaoming')";
//        readableDatabase.execSQL(sql);
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","嘻嘻");
        readableDatabase.insert("student",null,contentValues);
    }

    public void query(View view) {
        //查询数据
//        String sql = "select * from student where id = ?";
//        Cursor cursor = readableDatabase.rawQuery(sql, new String[]{"3"});
        Cursor cursor = readableDatabase.query("student", null, null, null, null, null, null);
        if (cursor!=null){
            while (cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex("name"));
                Toast.makeText(this, ""+name, Toast.LENGTH_SHORT).show();
            }
            cursor.close();
        }
    }


    public void delete(View view) {
//        String sql = "delete from student where id = ?";
//        readableDatabase.execSQL(sql,new Object[]{2});
        readableDatabase.delete("student","id = ?",new String[]{"3"});
    }


    public void update(View view) {
//        String sql = "update student set name = '李白' where id = ?";
//        readableDatabase.execSQL(sql,new Object[]{3});
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","大白");
        readableDatabase.update("student",contentValues,"id = ?",new String[]{"3"});
    }
}

MySqlhelp

package com.example.dday09;

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

import androidx.annotation.Nullable;

public class MySqlhelp extends SQLiteOpenHelper {
    public MySqlhelp(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

activity_main.xml(单纯是一堆按钮)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:text="创建数据库"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Button>

    <Button

        android:onClick="createTable"
        android:text="创建表"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Button>

    <Button

        android:onClick="insert"
        android:text="insert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Button>

    <Button

        android:onClick="query"
        android:text="query"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Button>

    <Button

        android:onClick="delete"
        android:text="delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Button>

    <Button

        android:onClick="update"
        android:text="update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Button>

</LinearLayout>
  • 10
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值