SharedPreferences的使用与Sqlite的使用
生活随笔
收集整理的這篇文章主要介紹了
SharedPreferences的使用与Sqlite的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
ShapePreferences的使用
增加數據
刪除數據
修改數據
獲取數據
清空數據
SqLite的簡單介紹
Sqlite的使用
Sqlite數據庫的創建
增加數據
更新數據
刪除數據
查詢數據
ShapePreferences的使用
SharedPreferences的概述
? ? ? SharedPreferences是Android平臺上一個輕量級的存儲輔助類,用來保存應用的一些常用配置,它提供了String,set,int,long,float,boolean六種數據類型。SharedPreferences的數據以鍵值對的進行保存在以xml形式的文件中。在應用中通常做一些簡單數據的持久化緩存。
增加數據
//獲取SharedPreferences對象SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);//獲取Editor對象的引用SharedPreferences.Editor editor=sharedPreferences.edit();//將獲取過來的值放入文件editor.putString("name","wcl");editor.putInt("age",21);// 提交數據editor.commit();刪除數據
//獲取SharedPreferences對象SharedPreferences sharedPreferences2=getSharedPreferences("user",MODE_PRIVATE);//獲取Editor對象的引用SharedPreferences.Editor editor2=sharedPreferences2.edit();editor2.remove("age");// 提交數據editor2.commit();break;修改數據
//獲取SharedPreferences對象SharedPreferences sharedPreferences1=getSharedPreferences("user",MODE_PRIVATE);//獲取Editor對象的引用SharedPreferences.Editor editor1=sharedPreferences1.edit();//將獲取過來的值放入文件editor1.putString("name","czl");// 提交數據editor1.commit();獲取數據
//獲取SharedPreferences對象SharedPreferences sharedPreferences3=getSharedPreferences("user",MODE_PRIVATE);//獲取數據String name=sharedPreferences3.getString("name","");Toast.makeText(this,"Editor"+name,Toast.LENGTH_SHORT).show();break;清空數據
//獲取SharedPreferences對象SharedPreferences sharedPreferences4=getSharedPreferences("user",MODE_PRIVATE);//獲取Editor對象的引用SharedPreferences.Editor editor3=sharedPreferences4.edit();//清空數據editor3.clear();//提交數據editor3.commit();實例代碼:
package com.c201801080119.day_4;import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.ShareActionProvider;import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.Toast;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void click(View view) {switch (view.getId()){case R.id.button://獲取SharedPreferences對象SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);//獲取Editor對象的引用SharedPreferences.Editor editor=sharedPreferences.edit();//將獲取過來的值放入文件editor.putString("name","wcl");editor.putInt("age",21);// 提交數據editor.commit();break;case R.id.button2://獲取SharedPreferences對象SharedPreferences sharedPreferences1=getSharedPreferences("user",MODE_PRIVATE);//獲取Editor對象的引用SharedPreferences.Editor editor1=sharedPreferences1.edit();//將獲取過來的值放入文件editor1.putString("name","czl");// 提交數據editor1.commit();break;case R.id.button3://獲取SharedPreferences對象SharedPreferences sharedPreferences2=getSharedPreferences("user",MODE_PRIVATE);//獲取Editor對象的引用SharedPreferences.Editor editor2=sharedPreferences2.edit();editor2.remove("age");// 提交數據editor2.commit();break;case R.id.button4://獲取SharedPreferences對象SharedPreferences sharedPreferences3=getSharedPreferences("user",MODE_PRIVATE);//獲取數據String name=sharedPreferences3.getString("name","");Toast.makeText(this,"Editor"+name,Toast.LENGTH_SHORT).show();break;case R.id.button5://獲取SharedPreferences對象SharedPreferences sharedPreferences4=getSharedPreferences("user",MODE_PRIVATE);//獲取Editor對象的引用SharedPreferences.Editor editor3=sharedPreferences4.edit();//清空數據editor3.clear();//提交數據editor3.commit();break;}} } <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="63dp"android:layout_marginEnd="1dp"android:layout_marginRight="1dp"android:onClick="click"android:text="刪除數據"app:layout_constraintEnd_toEndOf="@+id/button2"app:layout_constraintTop_toBottomOf="@+id/button2" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="117dp"android:layout_marginLeft="117dp"android:onClick="click"android:layout_marginTop="48dp"android:text="增加數據"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="click"android:layout_marginStart="1dp"android:layout_marginLeft="1dp"android:layout_marginTop="57dp"android:text="修改數據"app:layout_constraintStart_toStartOf="@+id/button"app:layout_constraintTop_toBottomOf="@+id/button" /><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="4dp"android:onClick="click"android:layout_marginLeft="4dp"android:layout_marginTop="56dp"android:text="查詢數據"app:layout_constraintStart_toStartOf="@+id/button3"app:layout_constraintTop_toBottomOf="@+id/button3" /><Buttonandroid:id="@+id/button5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="4dp"android:layout_marginLeft="4dp"android:onClick="click"android:layout_marginTop="56dp"android:text="清楚數據"app:layout_constraintStart_toStartOf="@+id/button4"app:layout_constraintTop_toBottomOf="@+id/button4" /></androidx.constraintlayout.widget.ConstraintLayout>SqLite的簡單介紹
SQLite 一個非常流行的嵌入式數據庫,它支持 SQL 語言,并且只利用很少的內存就有很好的性能。此外它還是開源的,任何人都可以使用它。
Sqlite的使用
Sqlite數據庫的創建
通過繼承SQLiteOpenHelper類去實現,SQLiteOpenHelper有兩個抽象方法onCreate()和onUpgrade(),必須在自己的類中重寫這兩個方法,然后分別在這兩個方法中實現創建和升級數據庫的邏輯。
package com.c201801080119.day_4;import android.content.Context; import android.database.DatabaseErrorHandler; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Build;import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi;class Demo extends SQLiteOpenHelper {public Demo(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {super(context, name, factory, version);}public Demo(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version, @Nullable DatabaseErrorHandler errorHandler) {super(context, name, factory, version, errorHandler);}@RequiresApi(api = Build.VERSION_CODES.P)public Demo(@Nullable Context context, @Nullable String name, int version, @NonNull SQLiteDatabase.OpenParams openParams) {super(context, name, version, openParams);}@Overridepublic void onCreate(SQLiteDatabase db) {String sting="create table user(id integer primary key autoincrement,username varchar(20),password varchar(20),age integer)";db.execSQL(sting);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} }增加數據
調用insert方法
ContentValues contentValues=new ContentValues();contentValues.put("username","cs");contentValues.put("password","123456");contentValues.put("age",21);sqLiteDatabase.insert("user",null,contentValues);更新數據
調用update的方法
ContentValues contentValues1=new ContentValues();contentValues1.put("username","css");sqLiteDatabase.update("user",contentValues1,"username=?",new String[]{"cs"});刪除數據
調用delete對象
sqLiteDatabase.delete("user","username=?",new String[]{"czl"});查詢數據
Cursor cursor=sqLiteDatabase.query("user",new String[]{"id","username","age"},"username=?",new String[]{"wcl"},null,null,null);while(cursor.moveToNext()){String id=cursor.getString(cursor.getColumnIndex("id"));String username=cursor.getString(cursor.getColumnIndex("username"));Log.i("Tomast",id+username);}完整代碼
package com.c201801080119.day_4;import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.ShareActionProvider;import android.content.ContentValues; import android.content.SharedPreferences; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Toast;public class MainActivity extends AppCompatActivity {private Demo demo;private SQLiteDatabase sqLiteDatabase;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);demo=new Demo(MainActivity.this,"user.db",null,1);sqLiteDatabase=demo.getWritableDatabase();}public void click(View view) {switch (view.getId()){case R.id.button:ContentValues contentValues=new ContentValues();contentValues.put("username","cs");contentValues.put("password","123456");contentValues.put("age",21);sqLiteDatabase.insert("user",null,contentValues);break;case R.id.button2:ContentValues contentValues1=new ContentValues();contentValues1.put("username","css");sqLiteDatabase.update("user",contentValues1,"username=?",new String[]{"cs"});break;case R.id.button3:sqLiteDatabase.delete("user","username=?",new String[]{"czl"});break;case R.id.button4:Cursor cursor=sqLiteDatabase.query("user",new String[]{"id","username","age"},"username=?",new String[]{"wcl"},null,null,null);while(cursor.moveToNext()){String id=cursor.getString(cursor.getColumnIndex("id"));String username=cursor.getString(cursor.getColumnIndex("username"));Log.i("Tomast",id+username);}break;case R.id.button5:break;}} }總結
以上是生活随笔為你收集整理的SharedPreferences的使用与Sqlite的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vr 计算机配置,能玩VR的电脑装机 |
- 下一篇: P1719 最大加权矩形