Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to open SQLite DB in java class
    text
    copied!<p>I am calling db in java class from activity(android).Is it possible to call like this from activity to java class.Am using this for server communication android.I got data from server but i cant able to insert into DB. </p> <p>I called like that am getting exception could any one help me, Thanks in advance.</p> <pre><code>11-01 06:52:03.596: W/System.err(673): java.lang.NullPointerException 11-01 06:52:03.603: W/System.err(673): at com.commiunication.ServerConnection.Insert(ServerConnection.java:32) 11-01 06:52:03.627: W/System.err(673): at com.commiunication.MainActivity.serverconnection(MainActivity.java:138) 11-01 06:52:03.627: W/System.err(673): at com.commiunication.MainActivity.onCreate(MainActivity.java:46) 11-01 06:52:03.633: W/System.err(673): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 11-01 06:52:03.633: W/System.err(673): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 11-01 06:52:03.633: W/System.err(673): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 11-01 06:52:03.633: W/System.err(673): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 11-01 06:52:03.633: W/System.err(673): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 11-01 06:52:03.633: W/System.err(673): at android.os.Handler.dispatchMessage(Handler.java:99) 11-01 06:52:03.633: W/System.err(673): at android.os.Looper.loop(Looper.java:123) 11-01 06:52:03.633: W/System.err(673): at android.app.ActivityThread.main(ActivityThread.java:4627) 11-01 06:52:03.633: W/System.err(673): at java.lang.reflect.Method.invokeNative(Native Method) 11-01 06:52:03.655: W/System.err(673): at java.lang.reflect.Method.invoke(Method.java:521) 11-01 06:52:03.655: W/System.err(673): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 11-01 06:52:03.655: W/System.err(673): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 11-01 06:52:03.655: W/System.err(673): at dalvik.system.NativeStart.main(Native Method) </code></pre> <p><strong>androidactivity.java</strong></p> <pre><code>ServerConnection server=new ServerConnection(); try { server.Insert(ReturnValue); sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME,MODE_PRIVATE,null); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } </code></pre> <p><strong>serverConnection.java</strong></p> <pre><code>public void Insert(String ReturnValue) throws JSONException{ JSONObject CDGSGJson=new JSONObject(ReturnValue); JSONArray idarr = CDGSGJson.getJSONArray("MeasureArr"); System.out.println(""+idarr.length()); sampleDB= openOrCreateDatabase(SAMPLE_DB_NAME, SQLiteDatabase.OPEN_READWRITE, null); System.out.println(""+idarr.length()); //Toast.makeText(this, "Group Details Downloading", Toast.LENGTH_SHORT).show(); try { sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " + table_name + " " + "( id INTEGER PRIMARY KEY AUTOINCREMENT," + " measure TEXT NOT NULL ); "); if(idarr.length()&gt;0){ for(int cc=0;cc&lt;idarr.length();cc++){ JSONObject CatObj = idarr.getJSONObject(cc); String id_get=CatObj.getString("id"); String measure_get=CatObj.getString("measure"); sampleDB.execSQL("INSERT OR REPLACE INTO " + table_name+ " Values ('"+id_get+"','"+measure_get+"');"); }} } catch (Exception e) { // TODO: handle exception e.printStackTrace(); // System.out.print(e.printStackTrace()); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload