安卓操作sqlite3,增删改查

创建

layout

<?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"/></LinearLayout>

main

package demo.jq.com.databasetest;import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;/*** @author jim*/
public class MainActivity extends AppCompatActivity {/*** 引入数据库助手类*/private MyDatabaseHelper dbHelper;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);dbHelper = 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();}});}
}

helper

package demo.jq.com.databasetest;import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;/*** @author Jim*/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;}/*** 数据库创建的时候,才执行* 如果数据库已经存在,将不会执行这个方法* @param db*/@Overridepublic void onCreate(SQLiteDatabase db) {// 执行sql语句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);}
}

添加数据

<Buttonandroid:id="@+id/add_data"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Add data"/>
        // 初始化添加数据按钮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.put("name","The Lost Symbol");values.put("author","Dan Brown");values.put("pages",510);values.put("price",19.95);// 插入第二条数据db.insert("Book",null,values);}});        

修改数据

<Buttonandroid:id="@+id/update_data"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Update data"/>
// 更改数据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",18.95);// 修改数据db.update("Book",values,"id = ?",new String[] {"1"});}});

删除数据

  <Buttonandroid:id="@+id/delete_data"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Delete data"/>
 // 删除数据Button deleteData = (Button) findViewById(R.id.delete_data);deleteData.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v) {SQLiteDatabase db = dbHelper.getWritableDatabase();// 删除数据db.delete("Book","pages > ?",new String[] {"500"});}});

查询数据

<Buttonandroid:id="@+id/query_data"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Query data"/>
 // 查询数据Button queryData = (Button) findViewById(R.id.query_data);queryData.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(TAG,"book name is "+name);Log.d(TAG,"book author is "+author);Log.d(TAG,"book pages is "+pages);Log.d(TAG,"book price is "+price);} while (cursor.moveToNext());}cursor.close();}});

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/280080.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于.NetCore开发博客项目 StarBlog - (23) 文章列表接口分页、过滤、搜索、排序

1前言上一篇留的坑&#xff0c;火速补上。在之前的第6篇中&#xff0c;已经有初步介绍&#xff0c;本文做一些补充&#xff0c;已经搞定这部分的同学可以快速跳过&#xff0c;基于.NetCore开发博客项目 StarBlog - (6) 页面开发之博客文章列表对标准的WebApi来说&#xff0c;分…

如何在Chrome中保存您当前的所有标签,以便以后阅读

Chrome allows you to open tabs from your last browsing session when you open the browser. However, what if you want to save your current set of tabs to re-open at any time? Chrome doesn’t provide a way to do that natively, but there is an easy workaround…

ubuntu 16.04(Windows 10双系统+grub引导)无法进入tt1~tt6(NVIDIA驱动安装相关-黑屏,login loop,分辨率)...

目录 前言回顾最终解决&#xff1a;0.关闭x服务1.禁用nouveau2.加入3.更新4.查找匹配驱动5.选择推荐版本6.等待安装后重启,nvidia-smi查看是否安装成功,或者lsmod | grep nvidia&#xff0c;成功结果如下7.重启x服务8.此时还不能进入图形界面&#xff0c;因为nomodeset还在&…

(备忘)打开office2010总是在配置进度

1、同时按上键盘上面的windows键和R键&#xff0c;出现“运行” 2、输入“regedit”&#xff0c;回车进入注册表 3、点击“HKEY_CURRENT_USER”展开&#xff0c;依次“Software”--“Microsoft”--“Office”--"14.0"--"Word"展开&#xff0c;点击"Op…

java、oracle对CLOB处理

oracle CLOB字段转换位VARCHAR 1.实际上处理CLOB字段的时候&#xff0c;直接TO_CHAR&#xff0c;当长度超过4000的时候&#xff0c;会报错&#xff0c;提示列被截取&#xff1b; CLOB转varchar2&#xff1a;select to_char(CLOB字段) from table 2.直接使用SUBSTR对CLOB字段进行…

android 更改软键盘_如何在Android的Google键盘上更改声音和振动

android 更改软键盘Tactile feedback from a touch screen keyboard is crucial, in my opinion, but I don’t like sounds when I tap keys. You may not be like me—maybe sounds are your thing, but vibration is annoying. Or maybe you dislike both (you rebel!). The…

『 再看.NET7』看看required属性有什么不同

还是先看看C#中属性的这定义&#xff0c;在初始化和访问上有哪些方式&#xff0c;就能看出required属性有什么不一样的地方了。属性&#xff0c;是封装字段的&#xff0c;通过get和set访问器可以很好地验证数据的有效性。public record Order_00 {public Guid Id { get; set; }…

知识点:Mysql 索引原理完全手册(1)

知识点&#xff1a;Mysql 索引原理完全手册(1) 知识点&#xff1a;Mysql 索引原理完全手册(2) 知识点&#xff1a;Mysql 索引优化实战(3) 知识点&#xff1a;Mysql 数据库索引优化实战(4) Mysql-索引原理完全手册 一、 介绍二、 索引的原理三、 索引的数据结构四、 聚集索引与辅…

如何将Apple Mail建议用于事件和联系人

Apple products come preinstalled with an email client that can, on occasion, be quite smart. Today we want to show you another great feature: suggestions for event and contacts. Apple产品预装了一个电子邮件客户端&#xff0c;该客户端有时可能非常聪明。 今天&a…

TPshop表结构

tp_account_log -- 账户表 字段名字段类型默认值描述log_idmediumint(8) unsigned 日志iduser_idmediumint(8) unsigned 用户iduser_moneydecimal(10,2)0.00用户金额frozen_moneydecimal(10,2)0.00冻结金额pay_pointsmediumint(9) 支付积分change_timeint(10) unsigned 变动时间…

Redis 通配符批量删除key

问题&#xff1a; 线上有部分的redis key需要清理。 一、 由于Keys模糊匹配&#xff0c;请大家在实际运用的时候忽略掉。因为Keys会引发Redis锁&#xff0c;并且增加Redis的CPU占用&#xff0c;情况是很恶劣的&#xff0c; 官网说明如下&#xff1a; Warning: consider KEYS as…

如何在 .Net 7 中将 Query 绑定到数组

在 .Net 7 中&#xff0c;我们可以通过绑定数组的方式来接收来自查询字符串的参数。这样就不需要再使用逗号分隔的字符串来获取参数了。代码演示 假设我们需要从 query 上接受多个 id 并返回查询的结果。例如&#xff1a;id1&id2在 .Net 7 中&#xff0c;我们可以这样实现&…

xbox one 越狱_如何在Xbox One上播放视频和音乐文件

xbox one 越狱The Xbox One has integrated TV features and support for streaming media apps like Netflix and Hulu, but that isn’t where it ends. You can play video and music files you’ve ripped or downloaded by plugging in a USB drive or streaming them ove…

C++实验七

11——3 #include<fstream>using namespace std;int main(){ ofstream file; file.open("test1.txt",ios_base::binary); file<<"已成功添加字符&#xff01;"; file.close(); return 0; } 11-4 #include<fstream>#include<iostrea…

Visual Studio 15.4发布,新增多平台支持

微软发布了Visual Studio 2017的第四个升级版本&#xff0c;并且延续了支持.NET Standard 2.0和通用Windows平台&#xff08;UWP&#xff09;的承诺。.NET Standard 2.0支持是微软推动跨平台应用程序开发和代码重用战略的重要一环。\\15.4版本的变化与微软发布的预览版非常接近…

重新学习web后端开发-001-写在前面的话

"长风破浪会有时 直挂云帆济沧海" —— 李白<!-- more --> 1. 为什么会写这个系列 随着互联网技术飞速的非常&#xff0c;web开发一直都是互联网技术的重要部分之一。在作者十余年的工作中&#xff0c;经历了从程序员到高级工程师&#xff0c;然后开始负责项目…

WPF-20 ICommand命令绑定

这节我们介绍一下WPF中比较重要的接口ICommand&#xff0c;也是WPF中一个新的特性&#xff0c;做过WinForm朋友都知道&#xff0c;WinForm开发是基于事件驱动开发模式&#xff0c;比如一个Button有Click事件&#xff0c;当我点击该按钮时&#xff0c;在当前页面会执行具体的业务…

如何在Safari中查看网页的完整URL

Modern versions of Safari don’t show the entire URL of a page in the address bar—it just shows the web site’s domain name. If this bothers you, it’s easy to change. Safari的现代版本无法在地址栏中显示页面的整个URL&#xff0c;而仅显示网站的域名。 如果这困…

PHP | Uploading and reading of files and database 【PHP | 文件的上传和读取与数据库】

这是我自己的一个作业&#xff0c;用的是很基础的代码。 有错误的地方欢迎批评和指正&#xff01; 这里最容易出错的地方在读取数据后向数据库表中插入数据是的数据格式&#xff01; 文件上传的页面 uploading.php <html> <body align "center"> <fo…

Mqttnet内存与性能改进录

1 MQTTnet介绍MQTTnet是一个高性能的 .NET MQTT库&#xff0c;它提供MQTT客户端和MQTT服务器的功能&#xff0c;支持到最新MQTT5协议版本&#xff0c;支持.Net Framework4.5.2版本或以上。MQTTnet is a high performance .NET library for MQTT based communication. It provid…