 |
BOOK: Beginning Android Application Development
 | This is the forum to discuss the Wrox book Beginning Android Application Development by Wei-Meng Lee; ISBN: 978-1-1180-1711-1 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning Android Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 8th, 2011, 01:58 PM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Chapter 6 Bundling a Database pg 231
I cannot get the Bundling a Database example to work -- it doesn't crash but it also doesn't display the imported contacts nor does it import the contacts into the database. If there is no existing database a new one will be created with the table and field names, but the table is not populated.
I have tried using various source databases in the res/assets folder: one I created using SQLite Database Browser, one from the sample code downloaded from wrox.com, and one created by an earlier version of the Databases project pulled from a running instance of the emulator & renamed. Also, adding an extension to res/assets/mydb, as in res/assets/mydb.db, had no effect. None of these worked. The Databases project from the sample code gives the same result.
Replacing /data/data/net.learn2develop.Databases/databases/MyDB with the database from the book sample code works fine, displaying all 4 contacts in toasts (assuming the database creation code is commented out).
As far as I can tell there are no permission flags that need to be set to allow the database to be copied, etc.
I have run out of ideas. Has anyone gotten this to work correctly?
|
|

September 9th, 2011, 09:50 AM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
working solution -- if only I knew why
Below is the code for a working solution for copying a database from the assets folder to the /data/data/<PROJECT>/databases folder of the emulator or device. My frustration is that I don't know why this works and the original does not. If anyone can enlighten me, please do. There are a few small changes to the code found in this post:
HTML Code:
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
This version will toast the contacts in the copied database. Please note the database format must include the tables required for it to be a legal android sqlite3 file -- it must have the android_metadata and sqlite_sequence tables as well as the contacts table. The database from the code samples only has a contacts table.
MainActivity.java
Code:
package com.android.copydatabase;
import java.io.IOException;
import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DataBaseHelper myDbHelper = new DataBaseHelper(null);
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
myDbHelper.openDataBase();
Cursor c = myDbHelper.getAllContacts();
if (c.moveToFirst()) {
do {
DisplayContact(c);
} while (c.moveToNext());
}
myDbHelper.close();
}
public void DisplayContact(Cursor c) {
Toast.makeText(
this,
"id: " + c.getString(0) + "\n" + "Name: " + c.getString(1)
+ "\n" + "Email: " + c.getString(2), Toast.LENGTH_LONG)
.show();
}
}
DataBaseHelper.java
Code:
package com.android.copydatabase;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelper extends SQLiteOpenHelper{
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.android.copydatabase/databases/";
private static String DB_NAME = "myDBName";
private SQLiteDatabase myDataBase;
private final Context myContext;
private static String DB_IN = "mydb";
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_EMAIL = "email";
private static final String DATABASE_TABLE = "contacts";
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_IN);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
public Cursor getAllContacts()
{
return myDataBase.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
KEY_EMAIL}, null, null, null, null, null);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.
}
|
|

December 11th, 2011, 09:20 PM
|
|
Authorized User
|
|
Join Date: Nov 2011
Posts: 86
Thanks: 3
Thanked 5 Times in 5 Posts
|
|
Bundling problem
Hi Mtvoyd
I have the same problem, but I think I know why. The code ask if the file exist. If it does it skips the copy. Well the last thing we did was up the version from 1 to 2, this removed the data but the file still exist.
I guess if the file exist you don't want to overwrite it, but it does not help our situation. I am thinking of putting in a menu with two choices, 1 skip the copy, 2 do the copy. Or just do the copy with a message.
One could delete the the file, if I knew how to do that in the android emulation.
Ah, new idea. If the file exist, one could check and see if it is empty (no entries). If it is empty then the copy could be done with out harm.
Hope this helps
Cliff
__________________
"Software to the Stars"
Failure is not an option. It comes bundled with your Microsoft products.
|
|

January 16th, 2012, 07:13 PM
|
|
Registered User
|
|
Join Date: Jan 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Bundling a database to run on cell phone
I created a database on a device running on the emulator using a DBHelper class. The database could be viewed at /data/data/mypackage/databases/....
However, when I disconnect the emulator's device and connect to my cell phone, I can run the application - which has been copied in tact to the phone - but the database is not copied over.
Is this something to do with security?
Would it be better to create the database and save first in the assets folder, then copy to data/data/... as described in the book. If there is a copy of the database in assets, will this be bundled with the app when I connect to the phone to run the app there?
I noticed also that when I deleted the device on the emulator - as it ran into a problem so I had to re-install it - the database and the package were lost, though they were re-created once I ran the app again on the new device.
|
|

January 16th, 2012, 07:37 PM
|
|
Authorized User
|
|
Join Date: Nov 2011
Posts: 86
Thanks: 3
Thanked 5 Times in 5 Posts
|
|
Hi Michael
I think the data must be transfered separately. Try moving the data to your phone.
My last post I talked about deleting a file on the emulator. The way to do that is in the DDMS view. In the File Explorer, there are icons on the upper right that will import, export, delete, and make a new directory.
Cliff
__________________
"Software to the Stars"
Failure is not an option. It comes bundled with your Microsoft products.
|
|

January 18th, 2012, 06:12 AM
|
|
Registered User
|
|
Join Date: Jan 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Cliff,
Thanks for your reply.
I think the problem is that the phone is locked as it is not possible to view the data folder on the phone from the emulator using the DDMS view. I have read that this can be done by rooting the phone but as that would invalidate the warranty on my personal phone, I don't want to go down that route.
In practice it won't be a problem for what I want to do as I can store the database on a server and access it using a web service.
I hope to get an unlocked tablet soon and experiment with that.
Mike
|
|

January 18th, 2012, 08:06 PM
|
|
Authorized User
|
|
Join Date: Nov 2011
Posts: 86
Thanks: 3
Thanked 5 Times in 5 Posts
|
|
Hi Michael
When they say a phone is locked, I think that means that it will only talk to one provider. The Data directory should be open. What I do is connect to the computer and a new folder becomes available. I open this and navigate to the desired folder on the device and drag and drop the file I want to transfer.
Now I'm on a Windows-XP machine. you mileage may differ with some other OS.
Also my device is a tablet, but I think it should work the same.
I have not tried using the emulator to transfer data.
Cliff
__________________
"Software to the Stars"
Failure is not an option. It comes bundled with your Microsoft products.
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Chapter 6, p. 230+231, step 3+4 |
TorbenR |
BOOK: Beginning SharePoint 2010 Development |
0 |
September 1st, 2011 07:51 AM |
| chapter 6 try it out p.231 |
Mohammed87 |
BOOK: Beginning ASP.NET 4 : in C# and VB |
3 |
August 16th, 2011 12:38 PM |
| Chapter 4 Pg 117 Try It Out |
workib |
ASP.NET 3.5 Basics |
1 |
April 7th, 2008 12:49 AM |
| chapter 5, pg 154 |
spizotfl |
BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 |
3 |
May 1st, 2007 03:08 PM |
| Chapter 4 pg 131 |
cpkaiser |
BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 |
3 |
May 2nd, 2004 01:50 PM |
|
 |
|