0

I believe with the following code, I am able to create a database, create a table, and insert the sensor values inside.

This line create a db -> super(context, "sensor.sqlite", null, 1); in data/data/example.com.sensor/databases/sensor.sqlite

In the oncreate method, a table is created. In the insert method, a record will be inserted.

However when I check in ddms, the db is not even created. I found a data folder but there is no data/data folder.

Any idea why this happens?

package example.com.sensor;

import java.io.File;
import java.util.HashMap;
import java.util.Date;
import android.util.Log;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class SqlController extends SQLiteOpenHelper {
    private static final String LOGCAT = null;

    public SqlController(Context context) {
        super(context, "sensor.sqlite", null, 1);

        Log.d(LOGCAT, "Created");
        this.onCreate(super.getReadableDatabase());
    }

    @Override
    public void onCreate(SQLiteDatabase database) {


        String x = "CREATE TABLE [sport] ([id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,";
        x+=" [accx] REAL,";
        x+=" [accy] REAL,";
        x+=" [accz] REAL,";
        x+=" [geox] REAL,";
        x+=" [geoy] REAL,";
        x+=" [geoz] REAL,";
        x+=" [activity] TEXT,";
        x+=" [datetime] TIMESTAMP)";

        database.execSQL(x);
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
        onCreate(database);
    }

    public void insert(HashMap<String, String> queryValues) {
        SQLiteDatabase database = this.getWritableDatabase();
        System.out.println(database.getPath());

        ContentValues values = new ContentValues();
        values.put("accx", queryValues.get("accx"));
        values.put("accy", queryValues.get("accy"));
        values.put("accz", queryValues.get("accz"));
        values.put("geox", queryValues.get("geox"));
        values.put("geoy", queryValues.get("geoy"));
        values.put("geoz", queryValues.get("geoz"));
        values.put("activity", queryValues.get("activity"));
        values.put("datetime", new Date().toString());
        database.insert("sport", null, values);
        database.close();
    }
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Charles Brown
  • 917
  • 2
  • 10
  • 20
  • 2
    Have you debugged through the code at all? Are your methods actually called? Are you sure you're meant to call `onCreate` rather than it being called automatically for you? – Jon Skeet Apr 25 '15 at 17:36
  • 1
    Its me again :). This is the same issue as your last post. Your phone is not rooted and `adb` (and thus `ddms`) does not have permission to read the /data/data folders. The folders do exist, you just don't have permission to open them. – pathfinderelite Apr 25 '15 at 17:38
  • Can I save to sd card instead? tqs. – Charles Brown Apr 25 '15 at 17:40
  • Yes, you can do that. For KitKat and above, be sure to use `READ_EXTERNAL_STORAGE` permission in your manifest. – pathfinderelite Apr 25 '15 at 17:43
  • However, I think you said is incorrect, I still able to create database, create table and insert record. What I do not know is where I store the db. – Charles Brown Apr 25 '15 at 17:50
  • Hi, Jon Socket -> Yes there is an exception happen while create table. The exception is table was exist. I have add a try-catch on it. Now my problem is I do not know where the sensor.sqlite is stored. – Charles Brown Apr 25 '15 at 17:51

1 Answers1

1

The exception that you are getting is because you are calling this.onCreate(super.getReadableDatabase()); directly in the constructor. There is no need to call onCreate() directly, it is a callback that will be called when the database file did not exist previously.

There is no need to change the path that your database file gets written to, just leave it be. As @pathfinderelite mentioned, it's written to internal storage of your app, which cannot be accessed unless you have a rooted device.

You state that you don't know where it's stored, but you do know where it's stored, you just don't have access to it, which is expected.

In order to verify the contents of your database, just to a simple select query, and log the results.

public void printAllActivities() {
    SQLiteDatabase database = this.getWritableDatabase();
    System.out.println(database.getPath());

    Cursor c = database.rawQuery("select * from sport", null);
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        int index = c.getColumnIndex("activity");
        String activity = c.getString(index);
        System.out.println("activity from database: " + activity);            

    }
    c.close();
    database.close();
}
Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137