android画面数据存储方式,【Android】安卓存储

本文介绍了如何在Android应用中使用SQLite数据库进行表的创建、增删改查操作,包括使用ContentValues、SQL语句和Android API提供的方法。通过实践演示了如何插入、删除和查询数据,并展示了查询结果的处理方式。
摘要由CSDN通过智能技术生成

package com.example.jyandroid.store;

import androidx.annotation.RequiresApi;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.os.Build;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

import com.example.jyandroid.R;

import java.io.File;

import java.util.ArrayList;

public class SQLiteActivity extends AppCompatActivity {

// 按钮组件

private EditText data;

private Button create_table,add,delete,update,select;

private SQLiteDatabase sqLiteDatabase;

@RequiresApi(api = Build.VERSION_CODES.O_MR1)

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_s_q_lite);

// 获取组件

data = findViewById(R.id.data);

create_table = findViewById(R.id.create_table);

add = findViewById(R.id.add);

delete = findViewById(R.id.delete);

update = findViewById(R.id.update);

select = findViewById(R.id.select);

// 创建数据库文件路径

File path = new File("/data/data/com.example.jyandroid/databases/");

// 判断文件路径是否存在

if(!path.exists()){

path.mkdirs();

}

// 创建数据库

// openOrCreateDatabase

// 不是openDatabase!!!

sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(new File(path + "/myDB.db"),null);

// 创建表

create_table.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

/*

VARCHAR(n) 可变长度字符串, n <= 4000

CHAR(n) 固定长度字符类型

INTEGER 整型数据

REAL 浮点型数据

TEXT 文本字符串

BLOB Blob(Binary long Object)是二进制长对象的意思,Blob通常用于存储大文件,典型的Blob内容是一张图片或者一个声音文件,由于他们的特殊性,必须使用特殊的方式来存储

DATE 日期类型

TIME 时间类型

*/

String sql =

"CREATE TABLE my_table1" +

"(" +

"id INTEGER PRIMARY KEY AUTOINCREMENT," +

"production_name TEXT," +

"production_price REAL" +

")";

sqLiteDatabase.execSQL(sql);

// 提示

Toast.makeText(SQLiteActivity.this,"创建表成功!",Toast.LENGTH_SHORT).show();

}

});

// 增

add.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

/*

// 方式一:sql

String sql = "insert into my_table (id,production_name,production_price) values ('苹果',12.22)";

sqLiteDatabase.execSQL(sql);

// 提示

Toast.makeText(SQLiteActivity.this,"添加成功!",Toast.LENGTH_SHORT).show();

*/

// 方式二:android 提供的内置方法 insert(表名,空值默认值,ContentValues对象)

ContentValues datas = new ContentValues();

// 插入第一条数据

datas.put("production_name","苹果");

datas.put("production_price","12.22");

sqLiteDatabase.insert("my_table",null,datas);

// 插入第二条数据

datas.put("production_name","橙子");

datas.put("production_price","18.50");

sqLiteDatabase.insert("my_table",null,datas);

// 插入第三条数据

datas.put("production_name","橙子");

datas.put("production_price","28.50");

sqLiteDatabase.insert("my_table",null,datas);

// 插入第四条数据

datas.put("production_name","榴莲");

datas.put("production_price","8.50");

sqLiteDatabase.insert("my_table",null,datas);

// 插入第五条数据

datas.put("production_name","草莓");

datas.put("production_price","15.22");

sqLiteDatabase.insert("my_table",null,datas);

// 提示

Toast.makeText(SQLiteActivity.this,"添加成功!",Toast.LENGTH_SHORT).show();

}

});

// 删

delete.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

/*

// 方式一:sql

String sql = "delete from my_table where id = 1";

sqLiteDatabase.execSQL(sql);

*/

// 方式二:android 提供的内置方法 delete(表名,where条件,where args[])

String whereClause = "production_name=? and production_price=?" ;

sqLiteDatabase.delete("my_table",whereClause,new String[]{"榴莲","8.50"});

// 提示

Toast.makeText(SQLiteActivity.this,"删除成功!",Toast.LENGTH_SHORT).show();

}

});

// 改

update.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

/*

// 方式一:sql

String sql = "update my_table set production_name='芒果'";

sqLiteDatabase.execSQL(sql);

*/

// 方式二:Android提供的方法update(表名,ContentValues对象,whereClaus,where args[])

ContentValues datas = new ContentValues();

datas.put("production_price",15.00);

String whereClause = "production_name=? and production_price>?";

sqLiteDatabase.update("my_table",datas,whereClause,new String[]{"橙子","20.00"});

// 提示

Toast.makeText(SQLiteActivity.this,"修改成功!",Toast.LENGTH_SHORT).show();

}

});

// 查

select.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

/*

// 方式一:sql

String sql = "select * from my_table where production_name=? and production_price=?";

Cursor cursor = sqLiteDatabase.rawQuery(sql,new String[]{"苹果","12.22"});

while(cursor.moveToNext()){

Toast.makeText(SQLiteActivity.this,cursor.getColumnIndex(0),Toast.LENGTH_SHORT).show();

}

*/

/*

方法二: Android的方法query()

参数列表:

distinct:是否去重

table:表名。相当于select语句from关键字后面的部分。如果是多表联合查询,可以用逗号将两个表名分开。

columns:要查询出来的列名。相当于select语句select关键字后面的部分。

selection:查询条件子句,相当于select语句where关键字后面的部分,在条件子句允许使用占位符“?”

selectionArgs:对应于selection语句中占位符的值,值在数组中的位置与占位符在语句中的位置必须一致,否则就会有异常。

groupBy:相当于select语句group by关键字后面的部分

having:相当于select语句having关键字后面的部分

orderBy:相当于select语句order by关键字后面的部分,如:id desc, age asc;

limit:指定偏移量和获取的记录数,相当于select语句limit关键字后面的部分。

*/

String[] columns = {"id","production_name","production_price"};

String whereClause = " production_price>?";

Cursor cursor = sqLiteDatabase.query(true,"my_table",columns,whereClause,new String[]{"10.00"},

null,null,"id desc", String.valueOf(2));

ArrayList arrayList = new ArrayList();

// 遍历

while (cursor.moveToNext()){

MyTable myTable = new MyTable();

myTable.setId(cursor.getInt(0));

myTable.setProduction_name(cursor.getString(1));

myTable.setProduction_price(cursor.getDouble(2));

arrayList.add(myTable);

}

// 直接显示查询的数据

for(MyTable myTable:arrayList){

Toast.makeText(SQLiteActivity.this,myTable.toString(),Toast.LENGTH_SHORT).show();

}

}

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值