Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add, delete, check and modify android studio data storage and establish SQLite database

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to add, delete, query and modify android studio data storage and establishing SQLite database. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Experimental purpose:

Build the SQLite database by using sqlite3 tools and Android code respectively. After completing the work of establishing the database, programming realizes the basic database operation functions, including data addition, deletion and update.

Experimental requirements:

1. Create a student management application with basic information including student name, class, and student number. A database is used to store this information.

two。 Applications should at least include information entry and deletion functions.

3. ListView is considered for data display.

Experimental results:

Engineering structure:

Source code:

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); 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); 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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report