Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>my friend you can try this one code </p> <pre><code>/** * * Purpose: Open and Read database to fetch records from different database * tables. * * @version 1.0 * * @author ketan kalariya(ketan.kalariya@indianic.com) * * **/ public class DataBaseHelper extends SQLiteOpenHelper { private Context myContext; private SQLiteDatabase myDataBase; // Comman KEYs of tables public static String MASTERFENCE_TABLE = "MasterFence"; public static String KEY_ID = "_id"; public static String AREA = "area"; public static String TAG = "tag"; public static String TITLE = "title"; public static String DESCR = "description"; public static String NO = "no"; public static String ADDFENCEPOINT_TABLE = "AddFencePoint"; public static String ADDLATLONG_TABLE = "AddLatLong"; public static String LAT = "lat"; public static String LONG = "long"; public static String TEMPFENCEPOINT_TABLE = "TempFencePoint"; public static String TEMPLATLONG_TABLE = "TempLatLong"; public DataBaseHelper(Context context) { super(context, context.getResources().getString(R.string.dbname), null, 1); this.myContext = context; } @Override public void onCreate(SQLiteDatabase arg0) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } // ---Create the database--- public void createDataBase() throws IOException { // ---Check whether database is already created or not--- boolean dbExist = checkDataBase(); if (!dbExist) { this.getReadableDatabase(); try { // ---If not created then copy the database--- copyDataBase(); this.close(); } catch (IOException e) { throw new Error("Error copying database"); } } } // --- Check whether database already created or not--- private boolean checkDataBase() { try { final String myPath = "/data/data/" + myContext.getPackageName() + "/databases/" + myContext.getString(R.string.dbname); final File f = new File(myPath); if (f.exists()) return true; else return false; } catch (SQLiteException e) { e.printStackTrace(); return false; } } // --- Copy the database to the output stream--- public void copyDataBase() throws IOException { final InputStream myInput = myContext.getAssets().open( myContext.getString(R.string.dbname)); final String outFileName = "/data/data/" + myContext.getPackageName() + "/databases/" + myContext.getString(R.string.dbname); final OutputStream myOutput = new FileOutputStream(outFileName); final byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer)) &gt; 0) { myOutput.write(buffer, 0, length); } myOutput.flush(); myOutput.close(); myInput.close(); } public void openDataBase() throws SQLException { // --- Open the database--- final String myPath = "/data/data/" + myContext.getPackageName() + "/databases/" + myContext.getString(R.string.dbname); myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); } /** * Close the database */ public synchronized void closeDatabase() { if (myDataBase != null) ; myDataBase.close(); SQLiteDatabase.releaseMemory(); super.close(); } /** This method is insert require detail in database table. **/ public long insertMasterFence(String area, String title, String descr, String tag) { ContentValues values = new ContentValues(); values.put(AREA, area); values.put(TITLE, title); values.put(DESCR, descr); values.put(TAG, tag); Log.v(" db ", area + " " + title + " " + " " + descr + " " + tag); return myDataBase.insert(MASTERFENCE_TABLE, null, values); } </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