简体   繁体   English

在本地数据库上存储数据

[英]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. 感谢Stackoverflow的贡献者,我无需使用Java就可以使用Android Studio开发我的第一个Android应用程序。 It's an unique-interface barcode scanning app (using ZXing) with two EditText fields and an OK button. 这是一个具有两个EditText字段和一个OK按钮的独特界面条形码扫描应用程序(使用ZXing)。 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. 我想将数据存储在本地数据库的两个EditText字段中,该字段具有两列(用于两个字段)和一个表。

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 首先,您应该阅读此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 对我来说,这看起来像是一个宠物项目,您只想存储两个值,建议您使用SharedPreferences存储它们

Refer to https://developer.android.com/training/data-storage/shared-preferences 请参阅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. 我认为您应该使用Sqlite数据库,这就是应该使用它的方式。

DBHelper.java 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 您还可以在DBHelper.java添加更多功能,例如从表中DBHelper.java完整数据等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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