溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用ContentValues對數(shù)據(jù)庫進(jìn)行相關(guān)操作

發(fā)布時(shí)間:2021-09-04 18:35:33 來源:億速云 閱讀:214 作者:chen 欄目:數(shù)據(jù)庫

本篇內(nèi)容介紹了“如何使用ContentValues對數(shù)據(jù)庫進(jìn)行相關(guān)操作”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

如何使用ContentValues對數(shù)據(jù)庫進(jìn)行相關(guān)操作
 

如何使用ContentValues對數(shù)據(jù)庫進(jìn)行相關(guān)操作

在main.xml中:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical"

    android:gravity="center_horizontal">

    <Button

        android:id="@+id/insertBut"

        android:layout_marginTop="8dp"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="增加數(shù)據(jù)" />

    <Button

        android:id="@+id/updateBut"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="修改數(shù)據(jù)" />

    <Button

        android:id="@+id/deleteBut"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="刪除數(shù)據(jù)" />

</LinearLayout>

創(chuàng)建數(shù)據(jù)庫的建表操作類Mytab.java

package com.li.sqlite;

import android.content.ContentValues;

import android.database.sqlite.SQLiteDatabase;

public class Mytab {

  private static final String TABLENAME = "mytab"; // 表示要操作的數(shù)據(jù)表名稱

  private SQLiteDatabase db = null; // 數(shù)據(jù)庫操作

  public Mytab(SQLiteDatabase db) { 

     this.db = db;

  }

  public void insert(String name,String birthday) {  //向表中增加數(shù)據(jù)

     ContentValues cv = new ContentValues();

     cv.put("name",name);

     cv.put("birthday",birthday);

     this.db.insert(TABLENAME, null, cv);

     this.db.close() ;

  }

  public void update(int id, String name, String birthday) {  //修改表的數(shù)據(jù)

     ContentValues cv = new ContentValues();

     cv.put("name",name);

     cv.put("birthday",birthday);

     String whereCaluse = "id=?";

     String whereArgs[] = new String[]{String.valueOf(id)};

     this.db.update(TABLENAME, cv, whereCaluse, whereArgs);

     this.db.close() ;

  }

  public void delete(int id) {     //刪除表的數(shù)據(jù)

     String whereCaluse = "id=?";

     String whereArgs[] = new String[]{String.valueOf(id)};

     this.db.delete(TABLENAME, whereCaluse, whereArgs);

     this.db.close() ;

  }

}

在MyDatabaseHelper.java類中:

package com.li.sqlite;

//數(shù)據(jù)庫的輔助操作類

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class MyDatabaseHelper extends SQLiteOpenHelper {

  private static final String DATABASENAME = "liyewen.db" ;

  private static final int DATABASERVERSION = 1 ;  // 設(shè)置數(shù)據(jù)庫的版本

  private static final String TABLENAME = "mytab" ;

  public MyDatabaseHelper(Context context) {  // 用戶最關(guān)心的也肯定只是Context

     super(context, DATABASENAME, null, DATABASERVERSION);

  }

  @Override

  public void onCreate(SQLiteDatabase db) { // 創(chuàng)建數(shù)據(jù)表

     String sql = "CREATE TABLE " + TABLENAME + "("

         + "id    INTEGER       PRIMARY KEY ,"   // 在SQLite中設(shè)置為Integer、PRIMARY KEY則ID自動(dòng)增長

         + "name   VARCHAR(50)   NOT NULL ,"

         + "birthday DATE NOT    NULL" + ")";

     db.execSQL(sql) ;  // 執(zhí)行SQL

     System.out.println("****************** 創(chuàng)建:onCreate()。");

  }

  @Override

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

     String sql = "DROP TABLE IF EXISTS " + TABLENAME ;

     db.execSQL(sql) ;

     System.out.println("****************** 更新:onUpgrade()。");

     this.onCreate(db) ;

  }

}

MySQLiteDemo.java中:

package com.li.sqlite;

import android.app.Activity;

import android.database.sqlite.SQLiteOpenHelper;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MySQLiteDemo extends Activity {

  private Button inserBut = null;

  private Button updateBut = null;

  private Button deleteBut = null;

  private SQLiteOpenHelper helper = null;

  private Mytab mtab = null;

  private static int count = 0;

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.inserBut = (Button)super.findViewById(R.id.insertBut);

     this.updateBut = (Button)super.findViewById(R.id.updateBut);

     this.deleteBut = (Button)super.findViewById(R.id.deleteBut);

     this.helper = new MyDatabaseHelper(this);

     this.inserBut.setOnClickListener(new InertOnClickListenerImpl());

     this.updateBut.setOnClickListener(new UpdateOnClickListenerImpl());

     this.deleteBut.setOnClickListener(new DeleteOnClickListenerImpl());

  }

  private class InertOnClickListenerImpl implements OnClickListener{

     public void onClick(View v) {

       MySQLiteDemo.this.mtab = new Mytab(

            MySQLiteDemo.this.helper.getWritableDatabase());

       MySQLiteDemo.this.mtab.insert("liyewen" + count++, "1988-08-16");

     }

  }

  private class UpdateOnClickListenerImpl implements OnClickListener{

     public void onClick(View v) {

       MySQLiteDemo.this.mtab = new Mytab(

            MySQLiteDemo.this.helper.getWritableDatabase());

       MySQLiteDemo.this.mtab.update(72, "update", "1988/8/15");

     }

  }

  private class DeleteOnClickListenerImpl implements OnClickListener{

     public void onClick(View v) {

       MySQLiteDemo.this.mtab = new Mytab(

            MySQLiteDemo.this.helper.getWritableDatabase());

       MySQLiteDemo.this.mtab.delete(73);

     }

  }

}

“如何使用ContentValues對數(shù)據(jù)庫進(jìn)行相關(guān)操作”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI