简体   繁体   中英

Storing Data on a Local Database

Thanks to contributors from Stackoverflow, I was able to develop my first Android app using Android Studio without any prior knowledge of Java. It's an unique-interface barcode scanning app (using ZXing) with two EditText fields and an OK button. The two fields are filled with the barcode scanner.

I want to store the data in the two EditText fields on a local database with two columns (for the two fields) and one table.

I am not asking for code, I'm just asking for guidelines and pieces of advice to push forward the development of my app.

Thank you

这是我的应用的屏幕截图

Why do you choose a database to store these 2 values? Did you think about Preference? I think it is more suitable.

First, you should read this https://developer.android.com/guide/topics/data/data-storage

Now, decide for yourselves what suits best to your needs keeping in mind all the relevant factors.

Looks like a pet project to me and you just want to store two values, I would suggest you store them using SharedPreferences

Refer to https://developer.android.com/training/data-storage/shared-preferences

I think you should use Sqlite Database and this is the way you should use it.

DBHelper.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;

public class DBHelper extends SQLiteOpenHelper {

   public static final String DATABASE_NAME = "MyDBName.db";
   public static final String CONTACTS_TABLE_NAME = "barcode";
   public static final String CONTACTS_COLUMN_ID = "id";
   public static final String CONTACTS_COLUMN_TEXT1 = "field1";
   public static final String CONTACTS_COLUMN_TEXT2 = "field2";

   private HashMap hp;

   public DBHelper(Context context) {
      super(context, DATABASE_NAME , null, 1);
   }

   @Override
   public void onCreate(SQLiteDatabase db) {
      // TODO Auto-generated method stub
      db.execSQL(
         "create table barcode" +
         "(id integer primary key, field1 text, field2 text)"
      );
   }

   @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      // TODO Auto-generated method stub
      db.execSQL("DROP TABLE IF EXISTS barcode");
      onCreate(db);
   }

   public boolean insertData (String text1, String text2) {
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues contentValues = new ContentValues();
      contentValues.put("field1", text1);
      contentValues.put("field2", text2);
      db.insert("barcode", null, contentValues);
      return true;
   }

   public Cursor getData(int id) {
      SQLiteDatabase db = this.getReadableDatabase();
      Cursor res =  db.rawQuery( "select * from barcode where id="+id+"", null );
      return res;
   }



   public boolean updateBarcode (Integer id, String text1, String text2) {
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues contentValues = new ContentValues();
      contentValues.put("field1", text1);
      contentValues.put("field2", text2);
      db.update("barcode", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
      return true;
   }

Your Activity or Fragment

  private DBHelper mydb ;
  mydb = new DBHelper(this);

   //insert
   if(mydb.insertBarcode(EditText1.getText().toString(), 
                       EditText2.getText().toString())){
              Toast.makeText(getApplicationContext(), "done",
                       Toast.LENGTH_SHORT).show();  
    } else{
           Toast.makeText(getApplicationContext(), "not done", 
                   Toast.LENGTH_SHORT).show();  
    }

   //Read
   Cursor getBarcode= mydb.getData(id_to_search);

You can also add more function like readfull data from table etc in DBHelper.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM