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

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

概述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 and

MyDatabaseHelper

<span ></span><pre name="code" >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 >在类中定义了三个数据库更新的方法,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) ;		// 删除数据			}}) ;    }}
总结

以上是内存溢出为你收集整理的Day13持久化存储——SQLite数据库存储(增删改)全部内容,希望文章能够帮你解决Day13持久化存储——SQLite数据库存储(增删改)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1171488.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存