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
- Day13持久化存储——SQLite数据库存储(增删改)
- Day13持久化存储——SQLite数据库存储
- Day13持久化存储——SQLite数据库存储(使用contentValue封装)
- 持久化存储--3、SQLite 数据库存储
- Android数据持久化——SQLite数据库存储
- Android数据存储(3)——SQLite数据库的操作_增删改查完整输出
- 数据存储-SQLiteOpenHelper(SQLite数据库增删改查)
- SQLite持久化存储
- Android数据存储方式——SQLite数据库的增删改查用法样例和注意事项.txt
- python数据存储系列教程——python操作sqlite数据库:连接、增删查改、指令执行
- 数据存储--SQLite增删改查
- SQLite数据库——增删改查
- Android持久化存储——(包含操作SQLite数据库)
- 触发器—增删改—存储过程
- 持久化存储 (SQLite)
- 存储过程之七—存储过程增删改查
- android-数据持久化之SQlite数据库存储
- 【Android】数据持久化补充:SQLite数据库存储
- vi 的基本操作
- lua: Lua5.1中的API函数
- IOS UITableView 表格视图的绝大部分方法
- 关于现在各种网站转跳解决方案!
- Android项目运行时报错NoclassDefFoundError
- Day13持久化存储——SQLite数据库存储(增删改)
- 黑马程序员_关于java字符串反转的几种方案
- UIScrollView 滚动视图
- 10-30-2 加班费,要不要?
- Visual Studio (VS)和Visual Assist X(VA)快捷键
- HDU3038 - How Many Answers Are Wrong(带权并查集)
- UITabBarController 标签栏控制器
- javascript的setTimeout()标题栏效果—闪烁文字
- lua: 使用lua_next()遍历表