使用SQLite记录传感器数据

时间:2017-06-23 01:10:00

标签: java android sqlite android-sqlite

我是Android新手。我正在研究Android手机中的磁传感器。我能够访问磁传感器并将传感器数据记录到.csv文件中。但我想在SQLite中记录它。问题是磁传感器数据可以在主活动中的onSensorChanged方法中获得,我不知道如何在SQLite中准备插入类,它可以从主活动中获取数据。我已经粘贴了获取数据和DBHelper类的代码。

任何帮助都会有用。提前谢谢你。



// For accessing and displaying magnetic data

public class MainActivity extends AppCompatActivity implements SensorEventListener {
    Sensor magnetometer;
    SensorManager sm;
    TextView magnetismx;
    TextView magnetismy;
    TextView magnetismz;
    DBHelper dbHelper;
    public float a;
    public float b;
    public float c;
    
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
         sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL);

        magnetismx = (TextView) findViewById(R.id.magnetismx);
        magnetismy = (TextView) findViewById(R.id.magnetismy);
        magnetismz = (TextView) findViewById(R.id.magnetismz);

        magnetometer = sm.getDefaultSensor(magnetometer.TYPE_MAGNETIC_FIELD);
        if (magnetometer == null) {
            Toast.makeText(this, "Magnetometer not available", Toast.LENGTH_SHORT).show();
            finish();
        }
        
        @Override
    public void onSensorChanged(SensorEvent event) {
        Sensor sensor = event.sensor;
        a = event.values[0];
        b = event.values[1];
        c = event.values[2];

        if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {

            magnetismx.setText(Float.toString(event.values[0]));
            magnetismy.setText(Float.toString(event.values[1]));
            magnetismz.setText(Float.toString(event.values[2]));
        }
         @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

}




// my dbHelper class 

public class DBHelper extends SQLiteOpenHelper{
    private static final String DB_NAME = "Mag_Positioning.db";
    private static final int DB_VERSION = 1;
    private static final String COL_ID = "ID";
    private static final String COLXAXIS = "X-AXIS";
    private static final String TABLENAME = "MAP_COORDINATES";
    private static final String COLYAXIS = "Y-AXIS";
    private static final String COLZAXIS = "Z-AXIS";

    public DBHelper(Context context){
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLENAME + "(" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," +
                COLXAXIS + " INTEGER, " +
                COLYAXIS + " INTEGER, " +
                COLZAXIS + " INTEGER )";
        db.execSQL(createTable);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLENAME);
        onCreate(db);
    }
    public void insert(Integer x, Integer y, Integer z, SQLiteDatabase db ) {

        ContentValues contentvalues = new ContentValues();
        contentvalues.put("X-AXIS", x);
        contentvalues.put("Y-AXIS", y);
        contentvalues.put("Z-AXIS", z);
        db.insert("MAP_COORDINATES", null, contentvalues);
    }
}




2 个答案:

答案 0 :(得分:2)

您将数据库类更改为单例,并将数据类型Integer更改为 REAL ,因为您将存储浮点值,

注意:“ - ”在sqlite名称中无效,所以我改为“_”,ex在X-AXIS中转换为X_AXIS。

public class DBHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "Mag_Positioning.db";
    private static final int DB_VERSION = 1;
    private static final String COL_ID = "ID";
    private static final String COLXAXIS = "X_AXIS";
    private static final String TABLENAME = "MAP_COORDINATES";
    private static final String COLYAXIS = "Y_AXIS";
    private static final String COLZAXIS = "Z_AXIS";

    private static DBHelper mInstance;

    private DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public static DBHelper getInstance() {
        if (mInstance == null) {
            synchronized (DBHelper.class) {
                if (mInstance == null) {
                    mInstance = new DBHelper(BaseApp.getApp());
                }
            }
        }

        return mInstance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLENAME + "(" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," +
                COLXAXIS + " REAL, " +
                COLYAXIS + " REAL, " +
                COLZAXIS + " REAL )";
        db.execSQL(createTable);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLENAME);
        onCreate(db);
    }

    public void insert(float x, float y, float z) {

        ContentValues contentvalues = new ContentValues();
        contentvalues.put("X-AXIS", x);
        contentvalues.put("Y-AXIS", y);
        contentvalues.put("Z-AXIS", z);
        getWritableDatabase().insert("MAP_COORDINATES", null, contentvalues);
    }
}

如果您已经有BaseApplication类,请忽略以下代码

public class BaseApp extends Application {

    private static BaseApp mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static Application getApp() {
        return mInstance;
    }
}

检查是否在mainfest文件中添加了基类,

<application
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:name=".BaseApp"
    android:theme="@style/AppTheme">
</application>

获取传感器数据后,您可以这样调用,

public void onSensorChanged(SensorEvent event) {
    Sensor sensor = event.sensor;
    a = event.values[0];
    b = event.values[1];
    c = event.values[2];

    if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {

        DBHelper.getInstance().insert(a, b, c);
    }
}

答案 1 :(得分:1)

通常在使用数据库助手类时,您只希望拥有该对象的一个​​实例。 这可以这样实现:

<强> DBHelper.java

public class DBHelper extends SQLiteOpenHelper{
....

    private static DBHelper instance;

    public static DBHelper getInstance (Context context) {
        if (instance == null)
            instace = new DBHelper (context);
        return instance;
     }

    private DBHelper (Context context) // Notice the private constructor 
        ....
    }

    ....
}

现在可以使用您的活动中的DBHelper.getInstance (this)访问帮助程序

注意:这称为单例模式