本篇內(nèi)容介紹了“如何使用ContentValues對(duì)數(shù)據(jù)庫(kù)進(jìn)行相關(guān)操作”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
成都創(chuàng)新互聯(lián)公司專(zhuān)注于揭東網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供揭東營(yíng)銷(xiāo)型網(wǎng)站建設(shè),揭東網(wǎng)站制作、揭東網(wǎng)頁(yè)設(shè)計(jì)、揭東網(wǎng)站官網(wǎng)定制、微信小程序服務(wù),打造揭東網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供揭東網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。
在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ù)庫(kù)的建表操作類(lèi)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ù)表名稱(chēng)
private SQLiteDatabase db = null; // 數(shù)據(jù)庫(kù)操作
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類(lèi)中:
package com.li.sqlite;
//數(shù)據(jù)庫(kù)的輔助操作類(lèi)
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ù)庫(kù)的版本
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)增長(zhǎ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對(duì)數(shù)據(jù)庫(kù)進(jìn)行相關(guān)操作”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
網(wǎng)站題目:如何使用ContentValues對(duì)數(shù)據(jù)庫(kù)進(jìn)行相關(guān)操作
本文來(lái)源:http://www.rwnh.cn/article0/igidoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷(xiāo)、App設(shè)計(jì)、建站公司、動(dòng)態(tài)網(wǎng)站、微信小程序、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)