安卓使用sqlite

article/2025/10/17 7:37:11

搭建环境

在这里插入图片描述

// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {repositories {maven{ url 'https://maven.aliyun.com/repository/google'}maven{ url 'https://maven.aliyun.com/repository/gradle-plugin'}maven{ url 'https://maven.aliyun.com/repository/public'}maven{ url 'https://maven.aliyun.com/repository/jcenter'}}dependencies {classpath 'com.android.tools.build:gradle:4.0.0'// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files}
}allprojects {repositories {maven{ url 'https://maven.aliyun.com/repository/google'}maven{ url 'https://maven.aliyun.com/repository/gradle-plugin'}maven{ url 'https://maven.aliyun.com/repository/public'}maven{ url 'https://maven.aliyun.com/repository/jcenter'}}
}task clean(type: Delete) {delete rootProject.buildDir
}

创建界面

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/create_database"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Create database"/><Buttonandroid:id="@+id/add_data"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Add data"/><Buttonandroid:id="@+id/update_data"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Update data"/><Buttonandroid:id="@+id/delete_data"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Delete data"/><Buttonandroid:id="@+id/query_data"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Query data"/></LinearLayout>

MyDatabaseHelper

package com.example.databasetest;import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;public class MyDatabaseHelper extends SQLiteOpenHelper {//    建库语句public static final String CREATE_BOOK = "create table Book ("+ "id integer primary key autoincrement, "+ "author text, "+ "price real, "+ "pages integer, "+ "name text)";public static final String CREATE_CATEGORY = "create table Category ("+ "id integer primary key autoincrement, "+ "category_name text, "+ "category_code integer)";private Context mContext;public MyDatabaseHelper(Context context, String name,SQLiteDatabase.CursorFactory factory, int version) {super(context, name, factory, version);mContext = context;}//    下面是重写的两个方法@Overridepublic void onCreate(SQLiteDatabase db) {
//        执行语句db.execSQL(CREATE_BOOK);db.execSQL(CREATE_CATEGORY);
//        弹框提示Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//        执行语句db.execSQL("drop table if exists Book");db.execSQL("drop table if exists Category");
//        重新建库onCreate(db);}}

MainActivity

package com.example.databasetest;import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity {private MyDatabaseHelper dbHelper;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
//        版本2dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);Button createDatabase = (Button) findViewById(R.id.create_database);createDatabase.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dbHelper.getWritableDatabase();}});
//        
//        添加Button addData = (Button) findViewById(R.id.add_data);addData.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SQLiteDatabase db = dbHelper.getWritableDatabase();ContentValues values = new ContentValues();// 开始组装第一条数据values.put("name", "The Da Vinci Code");values.put("author", "Dan Brown");values.put("pages", 454);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); // 插入第二条数据}});
//        更新Button updateData = (Button) findViewById(R.id.update_data);updateData.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SQLiteDatabase db = dbHelper.getWritableDatabase();ContentValues values = new ContentValues();values.put("price", 10.99);db.update("Book", values, "name = ?", new String[] { "The Da Vinci Code" });}});
//        删除Button deleteButton = (Button) findViewById(R.id.delete_data);deleteButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SQLiteDatabase db = dbHelper.getWritableDatabase();db.delete("Book", "pages > ?", new String[] { "500" });}});
//        查询Button queryButton = (Button) findViewById(R.id.query_data);queryButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SQLiteDatabase db = dbHelper.getWritableDatabase();// 查询Book表中所有的数据Cursor cursor = db.query("Book", null, null, null, null, null, null);if (cursor.moveToFirst()) {do {// 遍历Cursor对象,取出数据并打印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 author is " + author);Log.d("MainActivity", "book pages is " + pages);Log.d("MainActivity", "book price is " + price);} while (cursor.moveToNext());}cursor.close();}});}}

运行界面

在这里插入图片描述
在这里插入图片描述
这是存放的路径。
在这里插入图片描述

源码下载

1、关注公众号,汪程序员,回复:安卓sqlite
https://mp.weixin.qq.com/s?__biz=Mzg4NTc0ODQwNA==&mid=2247483725&idx=1&sn=b9dfaf2021139f27d8c1019ba454d24c&chksm=cfa56696f8d2ef80b71d2b12c7904324553c30b7009fb2d271c610fb9b95ba008264bc141c4c&token=22627107&lang=zh_CN#rd

2、https://download.csdn.net/download/m0_61504367/85490667


http://chatgpt.dhexx.cn/article/wD36A1qU.shtml

相关文章

SQLite

一、android内的数据库的基础知识介绍 1.用了什么数据库 android中采用的数据库是SQLite这个轻量级的嵌入式开源数据库&#xff0c;它是用C语言构建的。相关简介可以从链接查看。 2.数据库基本知识观花 对于一些和我一样还没有真正系统学习数据库技术的同学来说&#xff0c;把S…

Android SQlite数据库使用详解

目录 概述SQLite使用SQLite数据库创建增加数据删除数据更新数据查询数据 完整代码 概述 SQLite 一个非常流行的嵌入式数据库&#xff0c;它支持 SQL 语言&#xff0c;并且只利用很少的内存就有很好的性能。此外它还是开源的&#xff0c;任何人都可以使用它。 查看模拟器数据库…

SQLite数据库

目录 SQLite数据库 在Android中的使用 SQLiteOpenHelper中的方法 增删改查 添加数据 insert() 查询数据 query(),rawQuery() 查询和添加案例 数据库帮助类&#xff1a; MainActivity&#xff1a; Activity_main.xml: SQLite数据库 SQLite是一个轻量级的嵌入数据库&…

SQLite 安装与使用

在 Windows 上安装 SQLite 什么是SQLite SQLite是一款非常轻量级的关系数据库系统&#xff0c;支持多数SQL92标准。SQLite在使用前不需要安装设置&#xff0c;不需要进程来启动、停止或配置&#xff0c;而其他大多数SQL数据库引擎是作为一个单独的服务器进程&#xff0c;被程序…

【Android】SQLite

1&#xff0c;概述 android端提供的轻量级数据库&#xff0c; 2&#xff0c;实例I &#xff08;1&#xff09;创建SQLiteOpenHelper public class DBHelper extends SQLiteOpenHelper {private static DBHelper sDBHelper;public static void init(Nullable Context contex…

Android之SQLite数据库的使用

SQLite是比较小而功能比较全的关系型数据库&#xff0c;下面介绍一下SQLite数据库的使用方法&#xff0c;及增删改查操作。 创建一个Android项目&#xff1b; 首先我们需要创建一个DatabaseHelper这个助手类&#xff0c;源码&#xff1a; package cn.android.sword.sqlite.db…

android使用SQLite

文章目录 1.SQLite的特点2.SQLite的使用2.1数据库的创建2.2新增&#xff08;insert)2.3修改&#xff08;update)2.4删除(delete)2.5查询(query) 3.使用SQL语句进行数据库操作4.SQLite事务 1.SQLite的特点 SQLite是一个轻量级数据库&#xff0c;它设计目标是嵌入式的&#xff0…

Android 操作SQLite基本用法

在Android 开发中SQLite起着很重要的作用&#xff0c;网上SQLite的教程有很多很多&#xff0c;不过那些教程大多数都讲得不是很全面。本人总结了一些SQLite的常用的方法&#xff0c;借着论坛的大赛&#xff0c;跟大家分享分享的。 一.SQLite的介绍 1.SQLite简介 SQLite是一…

Android中SQLite应用详解

上次我向大家介绍了SQLite的基本信息和使用过程,相信朋友们对SQLite已经有所了解了,那今天呢,我就和大家分享一下在Android中如何使用SQLite。 现在的主流移动设备像Android、iPhone等都使用SQLite作为复杂数据的存储引擎,在我们为移动设备开发应用程序时,也许就要使用到…

Android SQLite基本用法(极简)

前言 SQLite是一个进程内的库&#xff0c;实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。它是一个零配置的数据库&#xff0c;这意味着与其他数据库不一样&#xff0c;您不需要在系统中配置。就像其他数据库&#xff0c;SQLite 引擎不是一个独立的进程&am…

SQLite数据库可视化工具【SQLiteStudio】的下载及安装

上一篇文章介绍了Windows的SQLite数据库安装&#xff0c;但是安装完成后&#xff0c;需要用SQLite命令去管理SQLite数据库&#xff0c;对于初学者不太友好。本文为大家介绍SQLite数据库的可视化工具【SQLiteStudio】&#xff0c;用户可以在SQLiteStudio中&#xff0c;实现对数据…

SQLite下载、安装与连接

1.下载SQLite SQLite官网&#xff1a;https://www.sqlite.org/download.html 根据自己电脑操作系统配置&#xff0c;选择32位或64位下载&#xff08;一般情况下是64位&#xff09;。总共两个压缩包。 或者直接去&#xff1a;https://download.csdn.net/download/WenHuiJun_/8…

SQLite 使用(针对Android)

文章目录 1. SQLite 介绍2. SQLite可视化工具 安装3. SQLite的增删改查3.1 SqliteOpenHelper 创建 库或表3.2 SqliteOpenHelper 实现 增删改查 1. SQLite 介绍 SQLite的注意事项&#xff1a; 标准的主键写法&#xff1a;_id。(不标准写法&#xff1a;id)&#xff0c;此外&…

AXI 总线入门(一)通道握手-AXI-Lite

AXI 总线(一)通道握手-AXI-Lite 文章目录 AXI 总线(一)通道握手-AXI-Lite关于本系列教程什么是AXI协议AXI读写通道AXI读过程AXI写过程单一通道的握手VALID 信号先到READY信号先到READY和VALID同时到达原则性问题 AXI-Lite总线实现解析生成一个AXI(-Lite)外设整体端口信号解析A(…

【AXI】解读AXI协议乱序机制

芯片设计验证社区芯片爱好者聚集地硬件相关讨论社区数字verifier星球四社区联合力荐&#xff01;近500篇数字IC精品文章收录&#xff01;【数字IC精品文章收录】学习路线基础知识总线脚本语言芯片求职EDA工具低功耗设计VerilogSTA设计验证FPGA架构AMBA书籍 解读AXI协议乱序机制…

AXI协议(数据传输结构)

AXI协议关于Data read and write structure的部分指出&#xff0c;在数据传输过程中&#xff0c;主要涉及三个问题&#xff1a; 窄位宽数据传输&#xff08;Narrow transfers&#xff09;地址非对齐传输&#xff08;Unaligned Transfer&#xff09;混合大小端传输&#xff08;…

AXI总线代码详细整理(一)

目录 说明1. AXI 的时钟与复位1.1 时钟1.2 复位 2. 五个通道2.1 Write Address Channel2.2 Write Data Channel2.3 Write Response (B) Channel2.4 Read Address Channel2.5 Read Data (and Response) Channel 3 突发传输机制3.1 突发传输长度和宽度3.2 突发传输类型 说明 文字…

AXI总线详解完结篇

经过几个月的整理和发文,AXI总线详解系列正式迎来最终篇,内容基本涵盖了上一篇文章的全部内容。 所有文章的链接如下: 1、 AXI总线详解 2、AXI总线详解-总线和接口以及协议 3、AXI协议中的通道结构 4、AXI总线详解-AXI4读

AXI接口简介

此部分&#xff0c;有参考他人帖子的内容&#xff0c;加上自己的理解&#xff0c;感恩原作者 1、 AXI&#xff08;Advanced eXtensible Interface&#xff09;协议主要描述了主设备&#xff08;Master&#xff09;和从设备&#xff08;Slave&#xff09;之间的数据传输方式&…

AXI接口协议详解-AXI总线、接口、协议

转自&#xff1a;https://cloud.tencent.com/developer/article/1695010 AXI接口协议详解-AXI总线、接口、协议 AXI 总线 上面介绍了AMBA总线中的两种&#xff0c;下面看下我们的主角—AXI&#xff0c;在ZYNQ中有支持三种AXI总线&#xff0c;拥有三种AXI接口&#xff0c;当然用…