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工具就可以查看到我们是否成功进行数据库操作啦!!

最新文章

  1. JAVA设计模式之责任链模式
  2. HDU pog loves szh II (数的处理)
  3. switch_to 理解
  4. MVC小系列(八)【改变Areas的FindView顺序】
  5. Base64的用法
  6. 面试题 46 1+ 2+3+...+n
  7. Spring 系列: Spring 框架简介(转载)
  8. Python装饰器学习(九步入门)
  9. Vue.js搭建路由报错 router.map is not a function,Cannot read property ‘component’ of undefined
  10. Python爬虫爬取网页图片
  11. 关于Java中IO流的练习
  12. thinkphp url build 生成localhost.localhost的解决方案
  13. 洛谷P3321 序列统计
  14. 代码管理工具:使用github和git工具管理自己的代码
  15. what&#39;s the python之函数及装饰器
  16. DevExpress 控件使用菜单栏之BarManager
  17. 使用Amazon AWS SNS 发送 SMS 消息 .net
  18. HashSet集合的add()方法的源码
  19. Spring AOP课程实战
  20. dedecms操作数据库

热门文章

  1. Jmeter 测试工具
  2. Reflection的getCallerClass静态方法
  3. python3字典:获取json响应值来进行断言
  4. zookeeper 配置文件conf目录下 zoo文件 配置详解
  5. remove方法
  6. JavaScript设计模式 Item 7 --策略模式Strategy
  7. 跟我学ASP.NET MVC之四:使用Razor
  8. python取txt文件的若干行到另一个文件
  9. Java Script 学习笔记 (一) 基础
  10. 【建图+最短路】Bzoj1001 狼抓兔子