java - Create sqlite database through android -


i believe following code, able create database, create table, , insert sensor values inside.

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

in oncreate method, table created. in insert method, record inserted.

however when check in ddms, db not created. found data folder there no data/data folder.

any idea why 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();     } } 

the exception getting because calling this.oncreate(super.getreadabledatabase()); directly in constructor. there no need call oncreate() directly, callback will called when database file did not exist previously.

there no need change path database file gets written to, leave be. @pathfinderelite mentioned, it's written internal storage of app, cannot accessed unless have rooted device.

you state don't know it's stored, do know it's stored, don't have access it, expected.

in order verify contents of database, simple select query, , log results.

public void printallactivities() {     sqlitedatabase database = this.getwritabledatabase();     system.out.println(database.getpath());      cursor c = database.rawquery("select * sport", null);     (c.movetofirst(); !c.isafterlast(); c.movetonext()) {         int index = c.getcolumnindex("activity");         string activity = c.getstring(index);         system.out.println("activity database: " + activity);                  }     c.close();     database.close(); } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -