As it stands right now, I have a TableLayout that I am populating based on certain columns and every row from a .db file. The table is populated based upon a single .db file which I will call x from now on. Every time the activity is recreated, my populate method (what populates the table) checks to see what is in x.db. Every row in the table has the same 3 columns, but if you long press a row then you can see every detail of this row (the details calculate one of the column displays and the calculation is different for each row).
From here the user has the option to calculate a grand total, save the current .db file, load a different .db file, or clear the .db file and start over. Here is how I am handling the saving and loading, which is what my question is about (I had to change some of the names of variables to be able to post)
case R.id.save:
SAVE_DIALOG = R.id.save;
AlertDialog.Builder save_alert = new AlertDialog.Builder(this,3);
edit = new EditText(this);
edit.setSingleLine();
save_alert.setTitle("Please enter a name to save as");
save_alert.setView(edit);
save_alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if(edit.getText().toString().trim().isEmpty()){
Toast toast = Toast.makeText(getApplicationContext(), "Name must not be blank", Toast.LENGTH_LONG);
toast.show();
return;
}
else {
File makeDirectory = new File("data/data/com.project.project/databases/stored_x");
if(!makeDirectory.isDirectory())
makeDirectory.mkdir();
FileChannel fromChannel = null;
FileChannel toChannel = null;
try {
File input = new File("data/data/com.project.project/databases/x.db");
//File output = new File("data/data/com.project.project/databases/stored_x/"+ edit.getText() +".db");
File output = new File("data/data/com.project.project/databases/stored_x/"+ edit.getText());
FileInputStream in = new FileInputStream(input);
FileOutputStream out = new FileOutputStream(output);
fromChannel = in.getChannel();
toChannel = out.getChannel();
fromChannel.transferTo(0, fromChannel.size(), toChannel);
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
fromChannel.close();
}
catch (IOException e) {
e.printStackTrace();
}
try {
toChannel.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
save_alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog saveAlert = save_alert.create();
saveAlert.setCanceledOnTouchOutside(true);
saveAlert.show();
break;
After the user presses the save button, an alert dialog appears asking the user for a name to save the file as. After the user input a name and presses ok, the file is copied and stored in a folder that I make in “data/data/com.project.project/databases/stored_x/” So I am just making a copy of the .db file and putting it somewhere else.
In order to load I do something similar to save, except I chose the file view a listview and replace the current x.db with this file, now calling it x.db.
Everything works fine, and will work fine for the end users, but I am just curious if this is the way it is supposed to be done.
Ok so your query to get all items of a specify type would be something like this …
To insert a data into the database would be like this …
You can get a list of of different types by running this query
Android has build in classes that make query a database alot eaier. The SQLiteDatabase makes quering databases so much easier. I prefer to use the rawQuery() method but you can use the wrapper methods if you like. Here is an example for using the rawQuery method.