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

来源:互联网 发布:总决赛五项数据第一 编辑:程序博客网 时间:2024/05/18 01:36

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);// 调用父类构造}@Overridepublic void onCreate(SQLiteDatabase db) {// 创建数据表String sql = "CREATE TABLE " + TABLENAME + " (" + "idINTEGER PRIMARY KEY ," + "nameVARCHAR(50)NOT NULL ," + "birthdayDATENOT NULL)";// SQL语句db.execSQL(sql) ;// 执行SQL语句}@Overridepublic 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(){@Overridepublic void onClick(View arg0) {helper.insert("李兴华" + count ++ , "1979-08-12") ;// 增加数据}}) ;updateBut.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {helper.update(1, "MLDN", "1981-06-27") ;// 更新已有数据}}) ;deleteBut.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View arg0) {helper.delete(3) ;// 删除数据}}) ;    }}



0 0