安卓操作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.pswp.cn/news/280080.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/280080.shtml
英文地址,請注明出處:http://en.pswp.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…