Skip to content

Instantly share code, notes, and snippets.

@ashutoshpw
Created May 6, 2018 13:40
Show Gist options
  • Save ashutoshpw/e4ed81a18d87dc50f184d7f9a8394d4d to your computer and use it in GitHub Desktop.
Save ashutoshpw/e4ed81a18d87dc50f184d7f9a8394d4d to your computer and use it in GitHub Desktop.
Android DBHelper Example
public class DBHelper extends SQLiteOpenHelper{
// Database Information
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "sensor_data";
// Table and Column Names
public static final String TABLE_ACC = "acc";
private static final String CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";
private static final String KEY_ID = "id";
private static final String KEY_CREATED_AT = "created_at";
// Specific Table - column nmaes
private static final String KEY_X = "x";
private static final String KEY_Y = "y";
private static final String KEY_Z = "z";
private static final String CREATE_TABLE_ACC = "CREATE TABLE "
+ TABLE_ACC + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_X + " TEXT, "
+ KEY_Y + " TEXT, "
+ KEY_Z + " TEXT, "
+ KEY_CREATED_AT + " DATETIME DEFAULT " + CURRENT_TIMESTAMP + " )";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_ACC);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACC);
onCreate(db);
}
public void insertAcc(String x, String y, String z){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_X, x);
values.put(KEY_Y, y);
values.put(KEY_Z, z);
//db.insert(TABLE_ACC, null, values);
long rowInserted = db.insert(TABLE_ACC, null, values);
if(rowInserted != -1)
Log.d("DATABASE_OPERATION","ACC_INSERTED_" + rowInserted);
else
Log.d("DATABASE_OPERATION","ACC_INSERTED_FAILED" + rowInserted);
}
public void exportDB(String table_name, int col_count, Context c) {
Log.d("CSV_OPERATION_STARTED","STARTED");
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
Log.d("CSV_OPERATION_PATH",String.valueOf(Environment.getExternalStorageDirectory()));
if (!exportDir.exists())
{
exportDir.mkdirs();
}
File file = new File(exportDir, "csvname-" + table_name + ".csv");
try
{
file.createNewFile();
CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
SQLiteDatabase db = this.getReadableDatabase();
Cursor curCSV = db.rawQuery("SELECT * FROM " + table_name,null);
csvWrite.writeNext(curCSV.getColumnNames());
Log.d("CSV_OPERATION_WRITE","STARTED");
while(curCSV.moveToNext())
{
//Which column you want to exprort
//String arrStr[] ={curCSV.getString(0),curCSV.getString(1), curCSV.getString(2)};
List<String> list = new ArrayList<String>();
String arrStr[];
for(int i=0;i<col_count+2;i++){
list.add(curCSV.getString(i));
}
arrStr = list.toArray(new String[0]);
csvWrite.writeNext(arrStr);
}
csvWrite.close();
curCSV.close();
Intent newIntent = new Intent(Intent.ACTION_VIEW);
String mimeType ="text/csv";
newIntent.setDataAndType(FileProvider.getUriForFile(c, c.getPackageName()+".net.api_central.apps.sensortracker",file),mimeType);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
c.startActivity(newIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(c, "No handler for this type of file.", Toast.LENGTH_LONG).show();
}
}
catch(Exception sqlEx)
{
Log.e("CSV_OPERATION_FAILED", sqlEx.getMessage(), sqlEx);
}
}
}
public class MainActivity extends AppCompatActivity {
DBHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DBHelper(getApplicationContext());
//Inserting Data into DB
db.insertAcc("1","2","3");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment