久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

如何使用ContentValues對數據庫進行相關操作

150次閱讀
沒有評論

共計 4413 個字符,預計需要花費 12 分鐘才能閱讀完成。

本篇內容介紹了“如何使用 ContentValues 對數據庫進行相關操作”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

 

在 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= 增加數據 /

  Button

  android:id= @+id/updateBut

  android:layout_width= wrap_content

  android:layout_height= wrap_content

  android:text= 修改數據 /

  Button

  android:id= @+id/deleteBut

  android:layout_width= wrap_content

  android:layout_height= wrap_content

  android:text= 刪除數據 /

/LinearLayout

創建數據庫的建表操作類 Mytab.java

package com.li.sqlite;

import android.content.ContentValues;

import android.database.sqlite.SQLiteDatabase;

public class Mytab {

  private static final String TABLENAME = mytab // 表示要操作的數據表名稱

  private SQLiteDatabase db = null; // 數據庫操作

  public Mytab(SQLiteDatabase db) { 

  this.db = db;

  }

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

  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) {  // 修改表的數據

  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) {  // 刪除表的數據

  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;

// 數據庫的輔助操作類

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 ;  // 設置數據庫的版本

  private static final String TABLENAME = mytab

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

  super(context, DATABASENAME, null, DATABASERVERSION);

  }

  @Override

  public void onCreate(SQLiteDatabase db) {// 創建數據表

  String sql = CREATE TABLE + TABLENAME + (

  + id  INTEGER  PRIMARY KEY ,   // 在 SQLite 中設置為 Integer、PRIMARY KEY 則 ID 自動增長

  + name   VARCHAR(50)   NOT NULL ,

  + birthday DATE NOT   NULL + )

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

  System.out.println(****************** 創建: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 對數據庫進行相關操作”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注丸趣 TV 網站,丸趣 TV 小編將為大家輸出更多高質量的實用文章!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-07-28發表,共計4413字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 澎湖县| 临江市| 京山县| 苍溪县| 水富县| 巴林左旗| 明光市| 剑川县| 平江县| 宜君县| 眉山市| 宜春市| 卢湾区| 论坛| 桂东县| 克山县| 桑日县| 嘉祥县| 历史| 德清县| 富平县| 侯马市| 谷城县| 察哈| 衡水市| 阜南县| 中牟县| 雅安市| 万州区| 景谷| 太谷县| 石家庄市| 海门市| 虹口区| 屯门区| 高陵县| 南平市| 孙吴县| 昌吉市| 澄城县| 松溪县|