欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android?studio數(shù)據(jù)存儲(chǔ)建立SQLite數(shù)據(jù)庫實(shí)現(xiàn)增刪查改

 更新時(shí)間:2022年01月25日 09:36:03   作者:wow_awsl_qwq  
這篇文章主要介紹了vandroid?studio數(shù)據(jù)存儲(chǔ)建立SQLite數(shù)據(jù)庫實(shí)現(xiàn)增刪查改,分別使用sqlite3工具和Android代碼的方式建立SQLite數(shù)據(jù)庫,具體內(nèi)容,需要的小伙伴可以參考下面文章得詳細(xì)內(nèi)容

實(shí)驗(yàn)?zāi)康模?/strong>

分別使用sqlite3工具和Android代碼的方式建立SQLite數(shù)據(jù)庫。在完成建立數(shù)據(jù)庫的工作后,編程實(shí)現(xiàn)基本的數(shù)據(jù)庫操作功能,包括數(shù)據(jù)的添加、刪除和更新。

實(shí)驗(yàn)要求:

  • 1.創(chuàng)建一個(gè)學(xué)生管理的應(yīng)用,基本信息包含學(xué)生姓名,班級(jí),學(xué)號(hào)。采用數(shù)據(jù)庫存儲(chǔ)這些信息。
  • 2.應(yīng)用應(yīng)該至少包含信息錄入和刪除功能。
  • 3.數(shù)據(jù)顯示考慮采用ListView。

實(shí)驗(yàn)效果:

工程結(jié)構(gòu):

源代碼:

DBAdapter.java

package com.example.shiyan6_sqlite;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DBAdapter {

 private static final String DB_NAME = "student.db";
 private static final String DB_TABLE = "peopleinfo";
 private static final int DB_VERSION = 1;

 public static final String KEY_ID = "_id";
 public static final String KEY_NAME = "name";
 public static final String KEY_BANJI = "banji";
 public static final String KEY_XUEHAO = "xuehao";

 private SQLiteDatabase db;
 private final Context context;
 private DBOpenHelper dbOpenHelper;

 public DBAdapter(Context _context) {
  context = _context;
 }

 public void close() {
  if(db !=null)
  {
   db.close();
   db=null;
  }
 }

 public void open() throws SQLiteException {
  dbOpenHelper = new DBOpenHelper(context, DB_NAME, null, DB_VERSION);
  try {
   db = dbOpenHelper.getWritableDatabase();
  }
  catch (SQLiteException ex) {
   db = dbOpenHelper.getReadableDatabase();
  }
 }


 public long insert(People people) {
  ContentValues newValues = new ContentValues();
  newValues.put(KEY_NAME, people.Name);
  newValues.put(KEY_BANJI, people.Banji);
  newValues.put(KEY_XUEHAO, people.Xuehao);

  return db.insert(DB_TABLE, null, newValues);
 }


 public People[] queryAllData() {
  Cursor results =  db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_BANJI, KEY_XUEHAO},
    null, null, null, null, null);
  return ConvertToPeople(results);
 }

 public People[] queryOneData(long id) {
  Cursor results =  db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME, KEY_BANJI, KEY_XUEHAO},
    KEY_ID + "=" + id, null, null, null, null);
  return ConvertToPeople(results);
 }

 @SuppressLint("Range")
 private People[] ConvertToPeople(Cursor cursor){
  int resultCounts = cursor.getCount();
  if (resultCounts == 0 || !cursor.moveToFirst()){
   return null;
  }
  People[] peoples = new People[resultCounts];
  for (int i = 0 ; i<resultCounts; i++){
   peoples[i] = new People();
   peoples[i].ID = cursor.getInt(0);
   peoples[i].Name = cursor.getString(cursor.getColumnIndex(KEY_NAME));
   peoples[i].Banji = cursor.getString(cursor.getColumnIndex(KEY_BANJI));
   peoples[i].Xuehao = cursor.getString(cursor.getColumnIndex(KEY_XUEHAO));
   cursor.moveToNext();
  }
  return peoples;
 }

 public long deleteAllData() {
  return db.delete(DB_TABLE, null, null);
 }

 public long deleteOneData(long id) {
  return db.delete(DB_TABLE,  KEY_ID + "=" + id, null);
 }

 public long updateOneData(long id , People people){
  ContentValues updateValues = new ContentValues();
  updateValues.put(KEY_NAME, people.Name);
  updateValues.put(KEY_BANJI, people.Banji);
  updateValues.put(KEY_XUEHAO, people.Xuehao);

  return db.update(DB_TABLE, updateValues,  KEY_ID + "=" + id, null);
 }

 private static class DBOpenHelper extends SQLiteOpenHelper {

  public DBOpenHelper(Context context, String name, CursorFactory factory, int version) {
   super(context, name, factory, version);
  }

  private static final String DB_CREATE = "create table " +
    DB_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " +
    KEY_NAME+ " text not null, " + KEY_BANJI+ " text not null," + KEY_XUEHAO + " text not null);";

  @Override
  public void onCreate(SQLiteDatabase _db) {
   _db.execSQL(DB_CREATE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
   _db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
   onCreate(_db);
  }
 }
}

People.java

package com.example.shiyan6_sqlite;

public class People {
 public int ID = -1;
 public String Name;
 public String Banji;
 public String Xuehao;

 @Override
 public String toString(){
  String result = "";
  result += "ID:" + this.ID + ",";
  result += "姓名:" + this.Name + ",";
  result += "班級(jí):" + this.Banji + ", ";
  result += "學(xué)號(hào):" + this.Xuehao;
  return result;
 }
}


MainActivity.java

package com.example.shiyan6_sqlite;

import androidx.appcompat.app.AppCompatActivity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText e_xm,e_nl,e_sg,e_id;
    TextView t_1;
    Button b_add,b_allsee,b_clearsee,b_alldel,b_delid,b_seeid,b_updid;
    DBAdapter dbAdapter;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        e_xm=findViewById(R.id.e_xm);
        e_nl=findViewById(R.id.e_nl);
        e_sg=findViewById(R.id.e_sg);
        b_add=findViewById(R.id.b_add);
        b_allsee=findViewById(R.id.b_allsee);
        b_clearsee=findViewById(R.id.b_clearall);
        b_alldel=findViewById(R.id.b_delall);
        b_delid=findViewById(R.id.b_delid);
        b_seeid=findViewById(R.id.b_seeid);
        b_updid=findViewById(R.id.b_updid);
        e_id=findViewById(R.id.e_id);
        t_1=findViewById(R.id.t_1);
        dbAdapter=new DBAdapter(this);
        dbAdapter.open();


        b_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                People t=new People();
                t.Name=e_xm.getText().toString();
                t.Banji=e_nl.getText().toString();
                t.Xuehao=e_sg.getText().toString();
                long colunm=dbAdapter.insert(t);
                if (colunm == -1 ){
                    t_1.setText("添加過程錯(cuò)誤!");
                } else {
                    t_1.setText("成功添加數(shù)據(jù),ID:"+String.valueOf(colunm));
                }
            }
        });

        b_allsee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                People [] peoples =dbAdapter.queryAllData();
                if (peoples == null){
                    t_1.setText("數(shù)據(jù)庫中沒有數(shù)據(jù)");
                    return;
                }
                String t="數(shù)據(jù)庫:\n";
                for(int i=0;i<peoples.length;++i){
                    t+=peoples[i].toString()+"\n";
                }
                t_1.setText(t);
            }
        });

        b_clearsee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                t_1.setText("");
            }
        });

        b_alldel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dbAdapter.deleteAllData();
                t_1.setText("已刪除所有數(shù)據(jù)!");
            }
        });

        b_delid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id=Integer.parseInt(e_id.getText().toString());
                long result=dbAdapter.deleteOneData(id);
                String msg = "刪除ID為"+e_id.getText().toString()+"的數(shù)據(jù)" + (result>0?"成功":"失敗");
                t_1.setText(msg);
            }
        });

        b_seeid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id=Integer.parseInt(e_id.getText().toString());
                People people[]=dbAdapter.queryOneData(id);
                if(people==null){
                    t_1.setText("Id為"+id+"的記錄不存在!");
                }
                else{
                    t_1.setText("查詢成功:\n"+people[0].toString());
                }
            }
        });

        b_updid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id=Integer.parseInt(e_id.getText().toString());
                People t=new People();
                t.Name=e_xm.getText().toString();
                t.Banji=e_nl.getText().toString();
                t.Xuehao=e_sg.getText().toString();
                long n=dbAdapter.updateOneData(id,t);
                if (n<0){
                    t_1.setText("更新過程錯(cuò)誤!");
                } else {
                    t_1.setText("成功更新數(shù)據(jù),"+String.valueOf(n)+"條");
                }
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        dbAdapter.close();
    }
}

到此這篇關(guān)于vandroid studio數(shù)據(jù)存儲(chǔ)建立SQLite數(shù)據(jù)庫實(shí)現(xiàn)增刪查改的文章就介紹到這了,更多相關(guān)數(shù)據(jù)存儲(chǔ)建立SQLite數(shù)據(jù)庫實(shí)現(xiàn)增刪查改內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android自定義組合控件之自定義下拉刷新和左滑刪除實(shí)例代碼

    Android自定義組合控件之自定義下拉刷新和左滑刪除實(shí)例代碼

    最近做了個(gè)項(xiàng)目,其中有項(xiàng)目需求,用到下拉刷新和左滑刪除,網(wǎng)上沒有找到比較理想的解決辦法。下面小編給大家分享我的一個(gè)小demo有關(guān)Android自定義組合控件之自定義下拉刷新和左滑刪除實(shí)例代碼,需要的朋友參考下
    2016-04-04
  • Android桌面組件App Widget用法入門教程

    Android桌面組件App Widget用法入門教程

    這篇文章主要介紹了Android桌面組件App Widget用法,較為深入淺出的分析了Android桌面組件App Widget的功能、定義及使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • 深入淺出學(xué)習(xí)Android ListView基礎(chǔ)

    深入淺出學(xué)習(xí)Android ListView基礎(chǔ)

    這篇文章主要介紹了深入淺出的帶領(lǐng)大家學(xué)習(xí)Android ListView基礎(chǔ),ListView是安卓里常用的控件,本文介紹一下常用用法,以及優(yōu)化等方法,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Android Studio使用教程(三):常用快捷鍵

    Android Studio使用教程(三):常用快捷鍵

    這篇文章主要介紹了Android Studio使用教程(三):常用快捷鍵,本文還同時(shí)介紹了自動(dòng)導(dǎo)包設(shè)置,需要的朋友可以參考下
    2015-05-05
  • Android編程簡單實(shí)現(xiàn)撥號(hào)器功能的方法

    Android編程簡單實(shí)現(xiàn)撥號(hào)器功能的方法

    這篇文章主要介紹了Android編程簡單實(shí)現(xiàn)撥號(hào)器功能的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android撥號(hào)器功能的實(shí)現(xiàn)原理、步驟、操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-07-07
  • 新版Android studio導(dǎo)入微信支付和支付寶官方Demo問題解決大全

    新版Android studio導(dǎo)入微信支付和支付寶官方Demo問題解決大全

    這篇文章主要為大家詳細(xì)介紹了新版Android studio導(dǎo)入微信支付和支付寶官方Demo問題的解決大全,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android實(shí)現(xiàn)圖片查看功能

    Android實(shí)現(xiàn)圖片查看功能

    這篇文章主要介紹了Android如何實(shí)現(xiàn)圖片查看功能,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-04-04
  • Android GPS定位測試(附效果圖和示例)

    Android GPS定位測試(附效果圖和示例)

    本人做了GPS相關(guān)的嵌入式軟件已經(jīng)幾年了,所以說起要做個(gè)測試GPS定位模塊的程序,第一反應(yīng)就是串口讀取GPS模塊的數(shù)據(jù),然后解析GPS的NMEA格式數(shù)據(jù)
    2013-07-07
  • android 設(shè)置圓角圖片實(shí)現(xiàn)代碼

    android 設(shè)置圓角圖片實(shí)現(xiàn)代碼

    在android應(yīng)用開發(fā)中,可能是美化需要,圖片需要處理成圓角,本文將給出實(shí)現(xiàn)代碼,開發(fā)中的遇到此問題的朋友可以參考下
    2012-11-11
  • Android Retrofit原理深入探索

    Android Retrofit原理深入探索

    Retrofit 是一個(gè) RESTful 的 HTTP 網(wǎng)絡(luò)請(qǐng)求框架的封裝,網(wǎng)絡(luò)請(qǐng)求的工作本質(zhì)上是 OkHttp 完成,而 Retrofit 僅負(fù)責(zé) 網(wǎng)絡(luò)請(qǐng)求接口的封裝
    2022-11-11

最新評(píng)論