SQLite数据库存储(下)

1.增添数据

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

package com.example.lenovo.studyittwo;

import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.*;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 构建MyDatabaseHelper对象,指定数据库名为"BookStore.db、版本号为1,版本号改为2之后则会直接
dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, );
Button btn_create_database = (Button) findViewById(R.id.creat);
btn_create_database.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 创建或打开一个现有的数据库(已存在则打开,否则创建一个新的)
dbHelper.getWritableDatabase();
}
});
Button addData= (Button)findViewById(R.id.add);
addData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name","the fuck code");
values.put("autuor","fuckers");
db.insert("Book",null,values);
values.clear();
values.put("name","the fuck code");
values.put("autuor","fuckers");
db.insert("Category",null,values);
values.clear(); }
}); }}

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <Button
android:id="@+id/creat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create database"/>
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add data"/> </LinearLayout>

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

2.更改数据

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <Button
android:id="@+id/creat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create database"/>
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add data"/>
<Button
android:id="@+id/updata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Updata data"/> </LinearLayout>

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

package com.example.lenovo.studyittwo;

import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.*;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 构建MyDatabaseHelper对象,指定数据库名为"BookStore.db、版本号为1,版本号改为2之后则会直接
dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, );
Button btn_create_database = (Button) findViewById(R.id.creat);
btn_create_database.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 创建或打开一个现有的数据库(已存在则打开,否则创建一个新的)
dbHelper.getWritableDatabase();
}
});
Button addData= (Button)findViewById(R.id.add);
addData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name","the fuck code");
values.put("autuor","fuckers");
db.insert("Book",null,values);
values.clear();
values.put("name","the fuck code");
values.put("autuor","fuckers");
db.insert("Category",null,values);
values.clear(); }
});
Button updataData= (Button)findViewById(R.id.updata);
updataData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name","我是傻逼\n");
db.update("Book",values,"name=?",new String[]{"the fuck code"}); }
}); }}

3.删除数据

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <Button
android:id="@+id/creat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create database"/>
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add data"/>
<Button
android:id="@+id/updata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Updata data"/>
<Button
android:id="@+id/deletedata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete data"/> </LinearLayout>

然后写入主活动的代码:

package com.example.lenovo.studyittwo;

import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.*;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 构建MyDatabaseHelper对象,指定数据库名为"BookStore.db、版本号为1,版本号改为2之后则会直接
dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, );
Button btn_create_database = (Button) findViewById(R.id.creat);
btn_create_database.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 创建或打开一个现有的数据库(已存在则打开,否则创建一个新的)
dbHelper.getWritableDatabase();
}
});
Button addData= (Button)findViewById(R.id.add);
addData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name","the fuck code");
values.put("autuor","fuckers");
db.insert("Book",null,values);
values.clear();
values.put("name","the fuck code");
values.put("autuor","fuckers");
db.insert("Category",null,values);
values.clear(); }
});
Button updataData= (Button)findViewById(R.id.updata);
updataData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name","我是傻逼\n");
db.update("Book",values,"name=?",new String[]{"the fuck code"});//如果名字等于这个就可以进行更新了 }
});
Button deleteData= (Button)findViewById(R.id.deletedata);
deleteData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name","我是傻逼\n");
db.delete("Book","name=?",new String[]{"the fuck code"});//如果名字等于这个就可以直接删除了 }
}); }}

4.查询数据

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

主界面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <Button
android:id="@+id/creat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create database"/>
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add data"/>
<Button
android:id="@+id/updata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Updata data"/>
<Button
android:id="@+id/deletedata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete data"/> </LinearLayout>

主活动的查询代码如下:

 private void queryStudents() {

        // 相当于 select * from students 语句
Cursor cursor = mSQLiteDatabase.query(SQLiteDbHelper.TABLE_STUDENT, null,
"cls_id > ? and id >= 1", new String[]{""},
null, null, null, null); // 不断移动光标获取值
while (cursor.moveToNext()) {
// 直接通过索引获取字段值
int stuId = cursor.getInt(); // 先获取 name 的索引值,然后再通过索引获取字段值
String stuName = cursor.getString(cursor.getColumnIndex("name"));
Log.e("", "id: " + stuId + " name: " + stuName);
}
// 关闭光标
cursor.close();
}

最后我们利用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. mysql性能调优与架构设计笔记

    1.mysql基本介绍 mysql支持多线程高并发的关系型数据库; 数据库存储引擎InnoDB.MyISAM; mysql快速崛起的原因就是他是开源的; 性能一直是mysql自豪的一大特点; 2.my ...

  2. Cookie与 Session使用详解

    Cookie概念 在浏览某些 网站 时,这些网站会把 一些数据存在 客户端 , 用于使用网站 等跟踪用户,实现用户自定义 功能. 是否设置过期时间: 如果不设置 过期时间,则表示这个 Cookie生命 ...

  3. linux模块驱动之led(ioremap)

    一:led内核驱动 (1)在编写led内核驱动时,我们首先要进行内核裁剪,因为友善之臂将LED灯的驱动默认加载到内核中,所以编写模块驱动程序前就要先把原先的LED灯驱动裁剪掉: led驱动在源码里面的 ...

  4. Tiny4412之按键驱动

    一:按键驱动 按键驱动跟之前的LED,蜂鸣器的方法类似:通过底板,核心板我们可以看到按键的电路图: 通过电路图我们可以看出,当按键按下去为低电平,松开为高电平:所以我们要检测XEINT26的状态,通过 ...

  5. SSM-Spring-06:Spring的自动注入autowire的byName和byType

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- di的注入上次讲了一些,这次主要阐述域属性的自动注入 先讲byType方式 看名字就知道是根据类型进行自动注入 ...

  6. KVM内核文档阅读笔记

    KVM在内核中有丰富的文档,位置在Documentation/virtual/kvm/. 00-INDEX:整个目录的索引及介绍文档. api.txt:KVM用户空间API,所谓的API主要是通过io ...

  7. jQuery.on() 函数详解 【转载】

    注意事项 1:on()为指定元素的一个或多个事件绑定事件处理函数.(可传递参数) 2:从jQuery 1.7开始,on()函数提供了绑定事件处理程序所需的所有功能,用于统一取代以前的bind(). d ...

  8. 关于CSS定位属性 position 的使用

    CSS中一般通过浮动和定位来对标签进行位置操作.下面我们来讨论一下定位的用法和需要注意的地方. 1.首先,说一下position的几个属性值 (1)none属性值,这个是定义不进行定位,默认为不定位, ...

  9. ECMAScript简介以及es6新增语法

    ECMAScript简介 ECMAScript与JavaScript的关系 ECMAScript是JavaScript语言的国际化标准,JavaScript是ECMAScript的实现.(前者是后者的 ...

  10. 错误:编码GBK的不可映射字符

    当Java源代码中包含中文字符时,我们在用javac编译时会出现"错误:编码GBK的不可映射字符". 由于JDK是国际版的,我们在用javac编译时,编译程序首先会获得我们操作系统 ...