1. 程式人生 > >安卓使用sql語句實現SQLite資料庫的增刪改查

安卓使用sql語句實現SQLite資料庫的增刪改查

本篇博文是在上一篇新建了資料庫的基礎上編寫的,上一篇博文連結:https://blog.csdn.net/liyunfu233/article/details/84193368

首先在佈局檔案中新增四個按鍵分別是增刪改查四種方法,在主視窗類中實現四種方法:
第一個點選按鈕增加一條記錄
在這裡插入圖片描述
第二個點選按鈕刪除一條記錄
在這裡插入圖片描述

第三個點選按鈕更新一條記錄

在這裡插入圖片描述

第四個點選按鈕查詢記錄
在這裡插入圖片描述

本篇博文需要將上一篇博文建立的資料庫欄位增加一個phone欄位。
在這裡插入圖片描述
cursor為行的集合,即如果查詢到資料,cursor就不為空,moveTonext是移動到下一行。

所有程式碼如下:
佈局程式碼:

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

    <Button
            android:onClick="click1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="add"/>
    <Button
            android:onClick="click2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="delete"/>
    <Button
            android:onClick="click3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="update"/>
    <Button
            android:onClick="click4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="find"/>

</LinearLayout>

myopenhelper類程式碼:

package com.example.a15114.createsqlitedemo;

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

import java.sql.ResultSet;

public class MyOpenHelper extends SQLiteOpenHelper {
    /**
     *
     *  context 上下文
     *   name  資料庫的名字
     *  factory   目的建立cursor物件
     *  version    資料庫的版本  從1開始
     */
    public MyOpenHelper(Context context) {
        super(context, "itheima.db", null, 4);
    }
    /**
     * 當資料庫第一次建立的時候呼叫
     * 那麼這個方法特別適合做表結構的初始化 建立表就是寫sql語句
     *
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        //id 一般以_id
        db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20),phone varchar(20))");
    }
    /**
     *
     * 當資料庫版本升級的時候呼叫
     *
     * 這個方法適合做 表結構的更新
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        System.out.println("升級");
        db.execSQL("alter table info add phone varchar(20)");
    }
}

主視窗類程式碼:

package com.example.a15114.createsqlitedemo;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    MyOpenHelper myOpenHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myOpenHelper=new MyOpenHelper(getApplicationContext());
        //開啟或者建立資料庫  如果是第一次就是建立
        //SQLiteDatabase sqLiteDatabase=myOpenHelper.getWritableDatabase();

        //開啟或建立資料庫 如果是第一次就是建立  如果磁碟滿了只返回一個可讀的
        SQLiteDatabase sqLiteDatabase=myOpenHelper.getReadableDatabase();


    }

    //點選按鈕增加一條記錄
    public void click1(View view) {
        //[1]獲取資料庫物件
        SQLiteDatabase db=myOpenHelper.getReadableDatabase();
        //[2]執行增加一條的sql語句
        db.execSQL("insert into info(name,phone) values(?,?)",new Object[]{"張三","138888"});
        //[3]資料庫用完需要關閉
        db.close();
    }


    //刪除
    public void click2(View view) {

        SQLiteDatabase db=myOpenHelper.getReadableDatabase();

        db.execSQL("delete from info where name=?",new Object[]{"張三"});

        db.close();
    }

    //更新
    public void click3(View view) {

        SQLiteDatabase db=myOpenHelper.getReadableDatabase();

        db.execSQL("update info set phone=? where name=?",new Object[]{"139999"});

        db.close();

    }

    //查詢
    public void click4(View view) {

        SQLiteDatabase db=myOpenHelper.getReadableDatabase();

        Cursor cursor=db.rawQuery("select * from info",null);
        if (cursor!=null&&cursor.getCount()>0){
            while (cursor.moveToNext()){
                //columindex代表列的索引
                String name=cursor.getString(1);
                String phone=cursor.getString(2);
                System.out.println("name:"+name+"---------"+phone);
            }
        }
    }
}