现在的位置: 首页 > 综合 > 正文

Day13持久化存储——SQLite数据库存储(增删改)

2017年10月12日 ⁄ 综合 ⁄ 共 3630字 ⁄ 字号 评论关闭

MyDatabaseHelper

<span style="font-size:18px; font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java">package com.example.demotest;

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

public class MyDatabaseHelper extends SQLiteOpenHelper {

	private static final String DATABASENAME = "mldn.db" ;	// 数据库名称
	private static final int DATABASEVERSION = 1 ;	// 数据库名称
	private static final String TABLENAME = "mytab" ;	// 数据表名称
	public MyDatabaseHelper(Context context) {
		super(context, DATABASENAME, null, DATABASEVERSION);// 调用父类构造
	}
	@Override
	public void onCreate(SQLiteDatabase db) {		// 创建数据表
		String sql = "CREATE TABLE " + TABLENAME + " (" + 
			"id			INTEGER 		PRIMARY KEY ," + 
			"name		VARCHAR(50)		NOT NULL ," + 
			"birthday	DATE			NOT NULL)";// SQL语句
		db.execSQL(sql) ;				// 执行SQL语句
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		String sql = "DROP TABLE IF EXISTS " + TABLENAME ;	// SQL语句
		db.execSQL(sql);				// 执行SQL语句
		this.onCreate(db); 				// 创建表
	}
	public void insert(String name, String birthday) {
		SQLiteDatabase db = super.getWritableDatabase(); 		// 取得数据库操作对象
		String sql = "INSERT INTO " + TABLENAME + " (name,birthday) VALUES ('"
				+ name + "','" + birthday + "')"; 	// SQL语句
		db.execSQL(sql);					// 执行SQL语句
		db.close() ;					// 关闭数据库操作
	}
	public void update(int id, String name, String birthday) {
		SQLiteDatabase db = super.getWritableDatabase(); 		// 取得数据库操作对象
		String sql = "UPDATE " + TABLENAME + " SET name='" + name
				+ "',birthday='" + birthday + "' WHERE id=" + id; // SQL语句
		db.execSQL(sql); 					// 执行SQL语句
		db.close() ;					// 关闭数据库操作
	}
	public void delete(int id) {
		SQLiteDatabase db = super.getWritableDatabase(); 		// 取得数据库操作对象
		String sql = "DELETE FROM " + TABLENAME + " WHERE id=" + id;
		db.execSQL(sql) ;					// 执行SQL语句
		db.close() ;	
	}

}

<span style="font-size:18px; font-family: Arial, Helvetica, sans-serif;">在类中定义了三个数据库更新的方法,insert,update,delete</span>

布局为三个按钮去增删改

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/insertBut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="74dp"
        android:text="增加数据" />

    <Button
        android:id="@+id/updateBut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="29dp"
        android:text="修改数据" />

    <Button
        android:id="@+id/deleteBut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/button2"
        android:layout_marginTop="32dp"
        android:text="删除数据" />

</RelativeLayout>

package com.example.demotest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
	private static int count = 0 ;				// 计数统计
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);			// 父类onCreate()
        super.setContentView(R.layout.activity_main);			// 默认布局管理器
        final MyDatabaseHelper helper = new MyDatabaseHelper(this) ;	// 数据库操作对象
        helper.getWritableDatabase() ;
		Button insertBut = (Button) super.findViewById(R.id.insertBut);	// 取得按钮
		Button updateBut = (Button) super.findViewById(R.id.updateBut);	// 取得按钮
		Button deleteBut = (Button) super.findViewById(R.id.deleteBut);	// 取得按钮
		insertBut.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				helper.insert("李兴华" + count ++ , "1979-08-12") ;	// 增加数据
			}}) ;
		updateBut.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				helper.update(1, "MLDN", "1981-06-27") ;	// 更新已有数据
			}}) ;
		deleteBut.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				helper.delete(3) ;		// 删除数据
			}}) ;
    }
}

抱歉!评论已关闭.