iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android中数据库常见操作实例分析
  • 615
分享到

Android中数据库常见操作实例分析

数据数据库Android 2022-06-06 09:06:42 615人浏览 安东尼
摘要

本文实例讲述了Android中数据库常见操作。分享给大家供大家参考,具体如下: android中数据库操作是非常常见了,我们会经常用到,操作的方法也有很多种形式,这里我就把最常

本文实例讲述了Android数据库常见操作。分享给大家供大家参考,具体如下:

android中数据库操作是非常常见了,我们会经常用到,操作的方法也有很多种形式,这里我就把最常见的两种形式记录下来了,以备以后用到方便查看。我就不写注释和解释了,因为android数据库的操作和其它数据库操作本质上都是一样的,大同小异。需要的一些基本解释都在代码中,直接上代码了。

简单的代码文件目录:

首先这个类是数据库帮助类,DBHelper.java,代码如下:


package net.loonggg.db; 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
 
public class DBHelper extends SQLiteOpenHelper { 
 public DBHelper(Context context) { 
  super(context, DBInfo.DB.DB_NAME, null, DBInfo.DB.DB_VERSioN); 
 } 
 @Override 
 public void onCreate(SQLiteDatabase db) { 
  db.execSQL(DBInfo.Table.USER_INFO_CREATE); 
 } 
 @Override 
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
  db.execSQL(DBInfo.Table.USER_INFO_DROP); 
  onCreate(db); 
 } 
}

其次是数据库信息类,DBInfo.java,代码如下:


package net.loonggg.db; 
 
public class DBInfo { 
  
 public static class DB { 
  // 数据库名称 
  public static final String DB_NAME = "test.db"; 
  // 数据库的版本号 
  public static final int DB_VERSION = 1; 
 } 
  
 public static class Table { 
  public static final String USER_INFO_TB_NAME = "user_table"; 
  public static final String USER_INFO_CREATE = "CREATE TABLE IF NOT EXISTS " 
    + USER_INFO_TB_NAME 
    + " ( _id INTEGER PRIMARY KEY,userId text,userName text)"; 
  public static final String USER_INFO_DROP = "DROP TABLE" 
    + USER_INFO_TB_NAME; 
 } 
}

再次是数据库操作类,DBService.java,代码如下:


package net.loonggg.service; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import net.loonggg.db.DBHelper; 
import net.loonggg.db.DBInfo.Table; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
 
public class DBService { 
 private DBHelper dbHelper = null; 
 public DBService(Context context) { 
  dbHelper = new DBHelper(context); 
 } 
  
 public void add(String id, String name) { 
  SQLiteDatabase db = dbHelper.getWritableDatabase(); 
  // 不好之处:无返回值,无法判断是否插入成功 
  db.execSQL("insert into user_table (userId,userName) values (?,?)", 
    new Object[] { id, name }); 
  db.close(); 
 } 
 public long addAndroid(String id, String name) { 
  SQLiteDatabase db = dbHelper.getWritableDatabase(); 
  ContentValues values = new ContentValues(); 
  values.put("userId", id); 
  values.put("userName", name); 
  // 好处:有返回值 
  long result = db.insert(Table.USER_INFO_TB_NAME, null, values);// 返回值是插入的是第几行,大于0代表添加成功 
  db.close(); 
  return result; 
 } 
  
 public boolean find(String name) { 
  SQLiteDatabase db = dbHelper.getReadableDatabase(); 
  Cursor cursor = db.rawQuery( 
    "select * from user_table where userName = ?", 
    new String[] { name }); 
  boolean result = cursor.moveToNext(); 
  db.close(); 
  return result; 
 } 
 public boolean findAndroid(String name) { 
  SQLiteDatabase db = dbHelper.getReadableDatabase(); 
  Cursor cursor = db.query(Table.USER_INFO_TB_NAME, null, "userName = ?", 
    new String[] { name }, null, null, null); 
  boolean result = cursor.moveToNext();// true代表查找到了 
  db.close(); 
  return result; 
 } 
  
 public void update(String id, String name) { 
  SQLiteDatabase db = dbHelper.getWritableDatabase(); 
  // 缺点无返回值 
  db.execSQL("update user_table set userName = ? where userId = ?", 
    new Object[] { name, id }); 
  db.close(); 
 } 
 public int updateAndroid(String id, String name) { 
  SQLiteDatabase db = dbHelper.getWritableDatabase(); 
  ContentValues values = new ContentValues(); 
  values.put("userName", name); 
  // 返回值大于0代表修改更新成功 
  int result = db.update(Table.USER_INFO_TB_NAME, values, "userId = ?", 
    new String[] { id }); 
  db.close(); 
  return result; 
 } 
  
 public void delete(String name) { 
  SQLiteDatabase db = dbHelper.getWritableDatabase(); 
  db.execSQL("delete from user_table where userName = ?", 
    new String[] { name }); 
  db.close(); 
 } 
 public int deleteAndroid(String name) { 
  SQLiteDatabase db = dbHelper.getWritableDatabase(); 
  int result = db.delete(Table.USER_INFO_TB_NAME, "userName = ?", 
    new String[] { name });// 返回值为受影响的行数,大于0代表成功 
  db.close(); 
  return result; 
 } 
  
 public List<HashMap<String, String>> findAll() { 
  List<HashMap<String, String>> list = null; 
  SQLiteDatabase db = dbHelper.getReadableDatabase(); 
  Cursor cursor = db.rawQuery("select * from user_table", null); 
  if (cursor.getCount() > 0) { 
   list = new ArrayList<HashMap<String, String>>(); 
   while (cursor.moveToNext()) { 
    String id = cursor.getString(cursor.getColumnIndex("userId")); 
    String name = cursor.getString(cursor 
      .getColumnIndex("userName")); 
    HashMap<String, String> map = new HashMap<String, String>(); 
    map.put("id", id); 
    map.put("name", name); 
    list.add(map); 
   } 
  } 
  cursor.close(); 
  db.close(); 
  return list; 
 } 
 public List<HashMap<String, String>> findAllAndroid() { 
  List<HashMap<String, String>> list = null; 
  SQLiteDatabase db = dbHelper.getReadableDatabase(); 
  Cursor cursor = db.query(Table.USER_INFO_TB_NAME, new String[] { 
    "userId", "userName" }, null, null, null, null, null); 
  if (cursor.getCount() > 0) { 
   list = new ArrayList<HashMap<String, String>>(); 
   while (cursor.moveToNext()) { 
    String id = cursor.getString(cursor.getColumnIndex("userId")); 
    String name = cursor.getString(cursor 
      .getColumnIndex("userName")); 
    HashMap<String, String> map = new HashMap<String, String>(); 
    map.put("id", id); 
    map.put("name", name); 
    list.add(map); 
   } 
  } 
  cursor.close(); 
  db.close(); 
  return list; 
 } 
}

最后是MainActivity,简单的调用了一下,这些操作,代码如下:


package net.loonggg.test; 
import net.loonggg.service.DBService; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
public class MainActivity extends Activity { 
 private Button queryOne; 
 private Button insert; 
 private Button update; 
 private Button delete; 
 private Button findAll; 
 private DBService service; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  queryOne = (Button) findViewById(R.id.queryOne); 
  insert = (Button) findViewById(R.id.insert); 
  update = (Button) findViewById(R.id.update); 
  delete = (Button) findViewById(R.id.delete); 
  findAll = (Button) findViewById(R.id.findAll); 
  queryOne.setOnClickListener(new ButtonListener()); 
  insert.setOnClickListener(new ButtonListener()); 
  update.setOnClickListener(new ButtonListener()); 
  delete.setOnClickListener(new ButtonListener()); 
  findAll.setOnClickListener(new ButtonListener()); 
  service = new DBService(this); 
 } 
 class ButtonListener implements View.OnClickListener { 
  @Override 
  public void onClick(View v) { 
   switch (v.getId()) { 
   case R.id.queryOne: 
    // service.find("loonggg"); 
    service.findAndroid("loonggg"); 
    break; 
   case R.id.insert: 
    // service.add("1", "loonggg"); 
    service.addAndroid("2", "heihei"); 
    break; 
   case R.id.update: 
    // service.update("1", "timmy"); 
    service.updateAndroid("1", "haha"); 
    break; 
   case R.id.delete: 
    // service.delete("timmy"); 
    service.deleteAndroid("heihei"); 
    break; 
   case R.id.findAll: 
    // service.findAll(); 
    service.findAllAndroid(); 
    break; 
   default: 
    break; 
   } 
  } 
 } 
}

还有MainActivity对应的布局文件,activity_main.xml:


<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" > 
 <Button 
  android:id="@+id/queryOne" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="查询一条记录" /> 
 <Button 
  android:id="@+id/insert" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="添加" /> 
 <Button 
  android:id="@+id/update" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="修改" /> 
 <Button 
  android:id="@+id/delete" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="删除" /> 
 <Button 
  android:id="@+id/findAll" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="查询全部" /> 
</LinearLayout>

到这里就介绍完了,这些代码并不高深,之所以记录下来,是留着以后用到的时候方便查看,当然这个代码对于初学者,还是非常有帮助的。

希望本文所述对大家Android程序设计有所帮助。

您可能感兴趣的文章:Android SQLite数据库增删改查操作的使用详解Android使用SQLite数据库的简单实例Android学习笔记-保存数据到SQL数据库中(Saving Data in SQL Databases)Android中操作SQLite数据库快速入门教程Android SQLite数据库增删改查操作的案例分析Android 数据库打包随APK发布的实例代码android通过jxl读excel存入sqlite3数据库Android操作SQLite数据库(增、删、改、查、分页等)及ListView显示数据的方法详解Android通过WEBservice操作sqlserver数据库实例代码Android开发中的数据库事务用法分析Android编程连接mongoDB及增删改查等基本操作示例


--结束END--

本文标题: Android中数据库常见操作实例分析

本文链接: https://www.lsjlt.com/news/26456.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作