1.  

SQLite数据库存储(下)

1.增添数据

对于添加数据的话我们只需要在主活动当中import新的包以及在主活动当中写上适当的代码就可以了,不需要在我们之前创建新的类当中书写新的代码。现在的主活动代码如下:

  1. package com.example.lenovo.studyittwo;
  2.  
  3. import android.content.IntentFilter;
  4. import android.content.SharedPreferences;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.content.*;
  10. import android.content.Context;
  11. import android.database.sqlite.SQLiteDatabase;
  12. import android.database.sqlite.SQLiteOpenHelper;
  13. import android.widget.Toast;
  14.  
  15. public class MainActivity extends AppCompatActivity {
  16. private MyDatabaseHelper dbHelper;
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. // 构建MyDatabaseHelper对象,指定数据库名为"BookStore.db、版本号为1,版本号改为2之后则会直接
  23. dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, );
  24. Button btn_create_database = (Button) findViewById(R.id.creat);
  25. btn_create_database.setOnClickListener(new View.OnClickListener() {
  26. @Override
  27. public void onClick(View view) {
  28. // 创建或打开一个现有的数据库(已存在则打开,否则创建一个新的)
  29. dbHelper.getWritableDatabase();
  30. }
  31. });
  32. Button addData= (Button)findViewById(R.id.add);
  33. addData.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View view) {
  36. SQLiteDatabase db=dbHelper.getWritableDatabase();
  37. ContentValues values=new ContentValues();
  38. values.put("name","the fuck code");
  39. values.put("autuor","fuckers");
  40. db.insert("Book",null,values);
  41. values.clear();
  42. values.put("name","the fuck code");
  43. values.put("autuor","fuckers");
  44. db.insert("Category",null,values);
  45. values.clear();
  46.  
  47. }
  48. });
  49.  
  50. }}

这样我们就分别向book表以及category表当中增添了数据了。当然我们也可以在这段代码当中看到我们新建了一个按钮,用于演示我们数据是否已经插入成功,下面是我们新的主活动界面的代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:orientation="vertical"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity">
  9.  
  10. <Button
  11. android:id="@+id/creat"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:text="Create database"/>
  15. <Button
  16. android:id="@+id/add"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:text="Add data"/>
  20.  
  21. </LinearLayout>

很自然地运用了一个线性的垂直布局,只是增加了一个button而已。

2.更改数据

为了方便研究更改数据,我们在布局下加入更改数据的按钮,代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:orientation="vertical"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity">
  9.  
  10. <Button
  11. android:id="@+id/creat"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:text="Create database"/>
  15. <Button
  16. android:id="@+id/add"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:text="Add data"/>
  20. <Button
  21. android:id="@+id/updata"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:text="Updata data"/>
  25.  
  26. </LinearLayout>

这里上主活动的代码,我们只是在第三个按钮处将代码做了适当的添加,这样就可以进行数据的更改了:

  1. package com.example.lenovo.studyittwo;
  2.  
  3. import android.content.IntentFilter;
  4. import android.content.SharedPreferences;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.content.*;
  10. import android.content.Context;
  11. import android.database.sqlite.SQLiteDatabase;
  12. import android.database.sqlite.SQLiteOpenHelper;
  13. import android.widget.Toast;
  14.  
  15. public class MainActivity extends AppCompatActivity {
  16. private MyDatabaseHelper dbHelper;
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. // 构建MyDatabaseHelper对象,指定数据库名为"BookStore.db、版本号为1,版本号改为2之后则会直接
  23. dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, );
  24. Button btn_create_database = (Button) findViewById(R.id.creat);
  25. btn_create_database.setOnClickListener(new View.OnClickListener() {
  26. @Override
  27. public void onClick(View view) {
  28. // 创建或打开一个现有的数据库(已存在则打开,否则创建一个新的)
  29. dbHelper.getWritableDatabase();
  30. }
  31. });
  32. Button addData= (Button)findViewById(R.id.add);
  33. addData.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View view) {
  36. SQLiteDatabase db=dbHelper.getWritableDatabase();
  37. ContentValues values=new ContentValues();
  38. values.put("name","the fuck code");
  39. values.put("autuor","fuckers");
  40. db.insert("Book",null,values);
  41. values.clear();
  42. values.put("name","the fuck code");
  43. values.put("autuor","fuckers");
  44. db.insert("Category",null,values);
  45. values.clear();
  46.  
  47. }
  48. });
  49. Button updataData= (Button)findViewById(R.id.updata);
  50. updataData.setOnClickListener(new View.OnClickListener() {
  51. @Override
  52. public void onClick(View view) {
  53. SQLiteDatabase db=dbHelper.getWritableDatabase();
  54. ContentValues values=new ContentValues();
  55. values.put("name","我是傻逼\n");
  56. db.update("Book",values,"name=?",new String[]{"the fuck code"});
  57.  
  58. }
  59. });
  60.  
  61. }}

3.删除数据

还是同样的套路,我们直接在主界面上加入第四个删除数据的按钮:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:orientation="vertical"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity">
  9.  
  10. <Button
  11. android:id="@+id/creat"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:text="Create database"/>
  15. <Button
  16. android:id="@+id/add"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:text="Add data"/>
  20. <Button
  21. android:id="@+id/updata"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:text="Updata data"/>
  25. <Button
  26. android:id="@+id/deletedata"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:text="Delete data"/>
  30.  
  31. </LinearLayout>

然后写入主活动的代码:

  1. package com.example.lenovo.studyittwo;
  2.  
  3. import android.content.IntentFilter;
  4. import android.content.SharedPreferences;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.content.*;
  10. import android.content.Context;
  11. import android.database.sqlite.SQLiteDatabase;
  12. import android.database.sqlite.SQLiteOpenHelper;
  13. import android.widget.Toast;
  14.  
  15. public class MainActivity extends AppCompatActivity {
  16. private MyDatabaseHelper dbHelper;
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. // 构建MyDatabaseHelper对象,指定数据库名为"BookStore.db、版本号为1,版本号改为2之后则会直接
  23. dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, );
  24. Button btn_create_database = (Button) findViewById(R.id.creat);
  25. btn_create_database.setOnClickListener(new View.OnClickListener() {
  26. @Override
  27. public void onClick(View view) {
  28. // 创建或打开一个现有的数据库(已存在则打开,否则创建一个新的)
  29. dbHelper.getWritableDatabase();
  30. }
  31. });
  32. Button addData= (Button)findViewById(R.id.add);
  33. addData.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View view) {
  36. SQLiteDatabase db=dbHelper.getWritableDatabase();
  37. ContentValues values=new ContentValues();
  38. values.put("name","the fuck code");
  39. values.put("autuor","fuckers");
  40. db.insert("Book",null,values);
  41. values.clear();
  42. values.put("name","the fuck code");
  43. values.put("autuor","fuckers");
  44. db.insert("Category",null,values);
  45. values.clear();
  46.  
  47. }
  48. });
  49. Button updataData= (Button)findViewById(R.id.updata);
  50. updataData.setOnClickListener(new View.OnClickListener() {
  51. @Override
  52. public void onClick(View view) {
  53. SQLiteDatabase db=dbHelper.getWritableDatabase();
  54. ContentValues values=new ContentValues();
  55. values.put("name","我是傻逼\n");
  56. db.update("Book",values,"name=?",new String[]{"the fuck code"});//如果名字等于这个就可以进行更新了
  57.  
  58. }
  59. });
  60. Button deleteData= (Button)findViewById(R.id.deletedata);
  61. deleteData.setOnClickListener(new View.OnClickListener() {
  62. @Override
  63. public void onClick(View view) {
  64. SQLiteDatabase db=dbHelper.getWritableDatabase();
  65. ContentValues values=new ContentValues();
  66. values.put("name","我是傻逼\n");
  67. db.delete("Book","name=?",new String[]{"the fuck code"});//如果名字等于这个就可以直接删除了
  68.  
  69. }
  70. });
  71.  
  72. }}

4.查询数据

在咱们的安卓开发当中的SQLiteDatabase类当中还提供了一个query()方法对于数据进行查询,这个方法非常复杂,最短的一个方法重载也需要传入7个参数。

主界面:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:orientation="vertical"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity">
  9.  
  10. <Button
  11. android:id="@+id/creat"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:text="Create database"/>
  15. <Button
  16. android:id="@+id/add"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:text="Add data"/>
  20. <Button
  21. android:id="@+id/updata"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:text="Updata data"/>
  25. <Button
  26. android:id="@+id/deletedata"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:text="Delete data"/>
  30.  
  31. </LinearLayout>

主活动的查询代码如下:

  1. private void queryStudents() {
  2.  
  3. // 相当于 select * from students 语句
  4. Cursor cursor = mSQLiteDatabase.query(SQLiteDbHelper.TABLE_STUDENT, null,
  5. "cls_id > ? and id >= 1", new String[]{""},
  6. null, null, null, null);
  7.  
  8. // 不断移动光标获取值
  9. while (cursor.moveToNext()) {
  10. // 直接通过索引获取字段值
  11. int stuId = cursor.getInt();
  12.  
  13. // 先获取 name 的索引值,然后再通过索引获取字段值
  14. String stuName = cursor.getString(cursor.getColumnIndex("name"));
  15. Log.e("", "id: " + stuId + " name: " + stuName);
  16. }
  17. // 关闭光标
  18. cursor.close();
  19. }

最后我们利用adb工具就可以查看到我们是否成功进行数据库操作啦!!

安卓开发笔记(十三):SQLite数据库储存(下)数据的增添,更改,删除,查询的更多相关文章

  1. 在安卓开发中使用SQLite数据库操作实例

    前段时间写了个安卓平台下SQLite数据库操作的实例 ,一直没得时间总结 ,今天把它弄出来了. 在Android 运行时环境包含了完整的 SQLite. 首先介绍一下SQLite这个数据库: SQLi ...

  2. 在Android 开发中使用 SQLite 数据库笔记

    SQLite 介绍   SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PH ...

  3. UWP开发随笔——使用SQLite数据库

    摘要 大多数的app都需要数据存储,在数据存储这方面,强大的windows把app数据分为两种:settings和files,并提供了十分简洁的api,让开发者能够轻松使用.但是在有些场景下,app的 ...

  4. 安卓开发笔记——深入Activity

    在上一篇文章<安卓开发笔记——重识Activity >中,我们了解了Activity生命周期的执行顺序和一些基本的数据保存操作,但如果只知道这些是对于我们的开发需求来说是远远不够的,今天我 ...

  5. 安卓开发笔记——自定义广告轮播Banner(实现无限循环)

    关于广告轮播,大家肯定不会陌生,它在现手机市场各大APP出现的频率极高,它的优点在于"不占屏",可以仅用小小的固定空位来展示几个甚至几十个广告条,而且动态效果很好,具有很好的用户& ...

  6. 安卓开发笔记——丰富多彩的TextView

    随手笔记,记录一些东西~ 记得之前写过一篇文章<安卓开发笔记——个性化TextView(新浪微博)>:http://www.cnblogs.com/lichenwei/p/4411607. ...

  7. 安卓开发笔记——关于开源项目SlidingMenu的使用介绍(仿QQ5.0侧滑菜单)

    记得去年年末的时候写过这个侧滑效果,当时是利用自定义HorizontalScrollView来实现的,效果如下: 有兴趣的朋友可以看看这篇文件<安卓开发笔记——自定义HorizontalScro ...

  8. android开发之使用SQLite数据库存储

    http://blog.csdn.net/jason0539/article/details/16360835 SQLite 介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且 ...

  9. 安卓开发笔记——打造万能适配器(Adapter)

    为什么要打造万能适配器? 在安卓开发中,用到ListView和GridView的地方实在是太多了,系统默认给我们提供的适配器(ArrayAdapter,SimpleAdapter)经常不能满足我们的需 ...

  10. 安卓开发笔记——关于Handler的一些总结(上)

    接上篇文章<安卓开发笔记——关于AsyncTask的使用>,今天来讲下在安卓开发里"重中之重"的另一个异步操作类Handler. 今天打算先讲下关于Handler的一些 ...

随机推荐

  1. LogicalDOC office预览中文乱码的问题

    近期在试用LogicalDOC,一个文档管理系统. 上传的office文件预览中文乱码 问题原因是LibreOffice缺少对应的中文字体导致,只需要把对应的中文字体拷贝到/opt/libreoffi ...

  2. Unity3D学习(八):《Unity Shader入门精要》——透明效果

    前言 在实时渲染中要实现透明效果,通常会在渲染模型时控制它的透明通道. Unity中通常使用两种方法来实现透明 :(1)透明度测试(AlphaTest)(2)透明度混合(AlphaBlend).前者往 ...

  3. linux dialog详解(图形化shell)

    liunx 下的dialog 工具是一个可以和shell脚本配合使用的文本界面下的创建对话框的工具.每个对话框提供的输出有两种形式:   1.  将所有输出到stderr 输出,不显示到屏幕.   2 ...

  4. window安装mysql5.7解压版(解决乱码问题)

    ♣安装mysql两种方法 ♣下载mysql5.7解压版 ♣配置步骤 ♣解决服务无法启动 ♣解决乱码 1.安装mysql两种方法 MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.如果 ...

  5. capwap学习笔记——初识capwap(二)(转)

    2.5.1 AC发现机制 WTP使用AC发现机制来得知哪些AC是可用的,决定最佳的AC来建立CAPWAP连接. WTP的发现过程是可选的.如果在WTP上静态配置了AC,那么WTP并不需要完成AC的发现 ...

  6. IntelliJ IDEA中 todo的使用

    在代码的注释部分加入TODO 大小写忽略,如下图所示 查看项目中有哪些待办项,所下图所示

  7. 基于opencv3.0下的运动车辆识别

    在opencv的初等应用上,对运动物体的识别主要有帧差或背景差两种方式. 帧差法主要的原理是当前帧与前一帧作差取绝对值: 背景差主要的原理是当前帧与背景帧作差取绝对值: 在识别运动车辆上主要需要以下9 ...

  8. 在Ubuntu16.04上配置.Net Core 2 环境

    一.安装.Net Core SDK 按照官方文档,执行以下命令安装SDK curl https://packages.microsoft.com/keys/microsoft.asc | gpg -- ...

  9. SQL注入简单介绍

    一.SQL注入概念   1.sql注入是一种将sql代码添加到输入参数中   2.传递到sql服务器解析并执行的一种攻击手法   举例:某个网站的用户名为name=‘admin’.执行时为select ...

  10. datetime日期和时间

    datetime是Python处理日期和时间的标准库. from datetime import datetime # 获取当前时间 now = datetime.now() print(now) # ...