android——SQLite数据库存储(操作)
public class MyDatabaseHelper extends SQLiteOpenHelper {
//把定义SQL建表语句成字符串常量
//图书的详细信息
//ID、作者、价格、页数、书名
public static final String CREATE_BOOK = "create table Book("
+"id integer primary key autoincrement,"
+"author text,"
+"price real,"
+"pages integer,"
+"name text)";
private Context mContext;
//构造方法
public MyDatabaseHelper(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version){
super(context,name,factory,version);
mContext = context;
}
//建表
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
Toast.makeText(mContext,"数据库创建成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
}
}
目前数据库中有一个Book表,如果想要添加一个Category表,就需要对数据库进行升级,这时需要用到MyDatabaseHelper中的onUpgrade()方法。
首先和Book表的建立一样需要先写好建表语句:
create table Category (
id integer primary key autoincrement
category_name text
category_code integer)
修改后的代码如下:
public class MyDatabaseHelper extends SQLiteOpenHelper {
//把定义SQL建表语句成字符串常量
//图书的详细信息
//ID、作者、价格、页数、书名
public static final String CREATE_BOOK = "create table Book("
+"id integer primary key autoincrement,"
+"author text,"
+"price real,"
+"pages integer,"
+"name text)";
//图书的分类
//图书的id、分类名、分类代码
public static final String CREATE_CATEGORY = "create table Category("
+ "id integer primary key autoincrement,"
+ "category_name text,"
+ "category_code inter)";
private Context mContext;
//构造方法
public MyDatabaseHelper(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version){
super(context,name,factory,version);
mContext = context;
}
//建表
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
db.execSQL(CREATE_CATEGORY);
Toast.makeText(mContext,"数据库创建成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists Category");
onCreate(db);
}
}
在onUpgrade()方法中先使用drop语句如果已经存在Book表和Category表,就把两张表都删掉,因为数据库已经存在了,onCreate()方法怎么样都不会再执行的。
然后在MainActivity修改代码:
public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建帮助类的实例
dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);
//注册按钮
Button creatDatabase = (Button) findViewById(R.id.creat_database);
//按钮响应
creatDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dbHelper.getWritableDatabase();
}
});
}
}
dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);把最后一个参数从之前的1改为2,再按下创建数据库就可完成升级。
接下来完成数据库的添加、更新、删除、查询操作。
先修改布局文件添加4个按钮:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/creat_database"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="建立数据库"/> <Button
android:id="@+id/add_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加数据"/> <Button
android:id="@+id/updata_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="更新数据"/> <Button
android:id="@+id/delete_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除数据"/> <Button
android:id="@+id/query_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询数据"/>
</LinearLayout>
在MainActivity中这样完成:
public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建帮助类的实例
dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);
//注册按钮
Button creatDatabase = (Button) findViewById(R.id.creat_database);
Button adddata = (Button) findViewById(R.id.add_data);
Button updataData = (Button) findViewById(R.id.updata_data);
Button deleteData = (Button) findViewById(R.id.delete_data);
Button queryData = (Button) findViewById(R.id.query_data);
//按钮响应
creatDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dbHelper.getWritableDatabase();
}
});
//添加数据
adddata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
//第一条数据
values.put("name","The Da Vinci Code");
values.put("author","Dan Brown");
values.put("pages",45);
values.put("price",16.96);
db.insert("Book",null,values);
values.clear();
//第二条数据
values.put("name","The Lost symbol");
values.put("author","Dan Brown");
values.put("pages",510);
values.put("price",19.95);
db.insert("Book",null,values);
Toast.makeText(MainActivity.this,"添加数据成功",Toast.LENGTH_SHORT).show();
}
});
//更新数据
updataData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("price",10.899);
db.update("Book", values, "name = ?",new String[] {"The Da Vinci Code"});
}
});
//删除数据
deleteData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("Book","pages > ?",new String[] {"500"});
}
});
//查询数据
queryData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("Book", null, null ,null, null, null, null );
if(cursor.moveToFirst()){
String name = cursor.getString(cursor.getColumnIndex("name"));
String author = cursor.getString(cursor.getColumnIndex("author"));
int pages = cursor.getInt(cursor.getColumnIndex("pages"));
double price = cursor.getDouble(cursor.getColumnIndex("price"));
Log.d("MainActivity","book name is " + name);
Log.d("MainActivity","book auther is " + author);
Log.d("MainActivity","book pagesis " + pages);
Log.d("MainActivity","book price is " + price);
}
}
});
}
}
添加数据:先使用dbHelper.getWritableDatabase()方法创建一个SQLiteDatabase的实例db,用于操作数据库,然后创键一个ContentValues的实例values,用来存放要添加的数据。然后是要values的put()方法将数据存入values中,再使用db的insert()方法将数据添加进数据库。然后使用clear()方法清空values再添加下一个数据。最后提醒添加成功。
更新数据:一样使用dbHelper.getWritableDatabase()创建一个一个SQLiteDatabase的实例db,在创建一个values,将要更新的数据存放在values中,然后使用update()方法更新数据。第三个和第四个参数用于判断修改的是哪一行的数据。
删除数据:使用delete()方法,第一个参数是想要操作的表名,第二个第三个指定操作的行。
查询数据:将数据存放在cursor对象中,query()方法最短也要有7个参数,例如:(返回值)方法名:query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy),其中table指定想要查询的表名,columns指定查询的列名,selection指定where的约束条件,selectionArgs为where中的占位符提供具体的值,groupBy指定group by的列,having对group by后的结果进行约束,orderBy查询结果的排序方式。
db.query("Book", null, null ,null, null, null, null );这样的用法表示将遍历整个Book表。
然后使用cursor的moveToFirst方法将指针移到第一行,再一次向下移动实现遍历,再使用cursor.getColumnIndex()方法得到相应列的索引,通过getString、getInt、getDouble获得相应类型的数据,最后输出查询的结果。
android——SQLite数据库存储(操作)的更多相关文章
- android——SQLite数据库存储(创建)
Android 专门提供了SQLiteOpenHelper帮助类,借助这个类就可以非常简单的对数据库进行创建和升级. 首先SQLiteOpenHelper是一个抽象类,在使用的时候需要创建一个自己的帮 ...
- Android SQLite数据库增删改查操作
一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库——SQLite,SQLite3支持NULL.INTEGER.REAL(浮点数字). TEXT(字符 ...
- Android SQLite 数据库 增删改查操作
Android SQLite 数据库 增删改查操作 转载▼ 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NU ...
- <Android基础> (六) 数据存储 Part 3 SQLite数据库存储
6.4 SQLite数据库存储 SQLite是一种轻量级的关系型数据库,运算速度快,占用资源少. 6.4.1 创建数据库 Android为了管理数据库,专门提供了SQLiteOpenHelper帮助类 ...
- Android学习之基础知识九 — 数据存储(持久化技术)之SQLite数据库存储
前面一讲介绍了数据持久化技术的前两种:文件存储.SharedPreferences存储.下面介绍第三种技术:SQLite数据库存储 一.SQLite数据库存储 SQLite数据库是一款轻量级的关系型数 ...
- Android中数据存储(三)——SQLite数据库存储数据
当一个应用程序在Android中安装后,我们在使用应用的过程中会产生很多的数据,应用都有自己的数据,那么我们应该如何存储数据呢? 数据存储方式 Android 的数据存储有5种方式: 1. Share ...
- Android Sqlite 数据库版本更新
Android Sqlite 数据库版本更新 http://87426628.blog.163.com/blog/static/6069361820131069485844/ 1.自己写一个类继承 ...
- 使用Sqlite数据库存储数据
1.Sql基本命令 1.1.创建表 表是有行和列组成的,列称为字段,行称为记录. 使用CREATE命令来创建表: 1 CREATE TABLE tab_student (studentId INTEG ...
- Android——SQLite/数据库 相关知识总结贴
android SQLite简介 http://www.apkbus.com/android-1780-1-1.html Android SQLite基础 http://www.apkbus.com/ ...
随机推荐
- visual studio code emmet 插件不提示?解决方案
使用visual studio code编辑.vue文件时,emmet插件无法使用, 可以通过以下方试解决: code →首选项 →设置 ,在右侧窗口“用户配置”.“工作区设置”添加以下代码: // ...
- Jpa 笔记
ORM 思想 对象关系映射, 建立实体类和表的关系映射关系, 实体类和表中字段的映射关系,我们操作实体类底层是操作数据表, 进而自动的拼接出SQL语句 Jpa规范 Jpa(Java Persisten ...
- golang开发:类库篇(三)命令行工具cli的使用
为什么要使用命令行 觉得这个问题不应该列出来,又觉得如果初次进行WEB开发的话,可能会觉得所有的东西都可以使用API去做,会觉得命令行没有必要. 其实,一个生产的项目命令行是绕不过去的.比如运营需要导 ...
- 自我救赎 → 利用 IDEA 和 Spring Boot 搭建 SSM
前言 开心一刻 儿子读高中放学回来了,一向不管他学习的我突然来了兴趣,想看看他的学习他的状况,抄起他的数学习题看了起来,当看到 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x ...
- tomcat问题解决
tomcat问题解决 运行tomcat环境下,idea中出现 error running 项目名address localhost1099 is already in use 的时候,如何解决? 1, ...
- Spring Cloud Alibaba | Nacos集群部署
目录 Spring Cloud Alibaba | Nacos集群部署 1. Nacos支持三种部署模式 2. 集群模式下部署Nacos 2.1 架构图 2.2 下载源码或者安装包 2.3 配置集群配 ...
- Jenkins Ci系列目录
Jenkins入门篇 1.Jenkins入门之界面概览 2.Jenkins入门之新建任务 3.Jenkins入门之导航操作 4.Jenkins入门之任务基本操作 5.Jenkins入门之执行Power ...
- u盘制作启动盘步骤以及安装win10步骤
1.下载制作工具:微PE工具箱V2.0 http://www.wepe.com.cn/download.html 2.默认制作启动盘 3.下载win10镜像 ed2k://|file|cn_windo ...
- IOCP Input/Output Completion Port IO完成端口
I/O completion ports provide an efficient threading model for processing multiple asynchronous I/O r ...
- webpack基础知识
一.基础 1 安装 npm i -g webpack webpack-cli // 推荐安装至本地 npm i -D webpack webpack-cli 2 webpck基础使用 2.1 webp ...