0

I created one app for database which contains a student table and 3 fields in it, but I don't know how to add the file in the app to the sqlite database.

database helper:

  public class DatabaseHelper extends SQLiteOpenHelper {

  public static final String DATA_BASE="Mydatabase.db";
public static final String TABLE_NAME="Student";
public static final int DATABASE_VERSION =1;

public DatabaseHelper(Context context) {
 super(context, DATA_BASE, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE " + TABLE_NAME + 
 "(Name TEXT, AGE NUMERIC, ADDRESS TEXT)"); }

@Override
public void onUpgrade(SQLiteDatabase db,
        int oldVersion, int newVersion) {

}
}

main activity:

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATA_BASE="Mydatabase.db";
public static final String TABLE_NAME="Student";
public static final int DATABASE_VERSION =1;

public DatabaseHelper(Context context) {
 super(context, DATA_BASE, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE " + TABLE_NAME + 
 "(Name TEXT, AGE NUMERIC, ADDRESS TEXT)"); }

@Override
public void onUpgrade(SQLiteDatabase db,
        int oldVersion, int newVersion) {

}
}

display activity:

  public class DisplayActivity extends Activity {

private ArrayList<String> arrayList = 
                    new ArrayList<String>();

private SQLiteDatabase Mydatabase;
ListView listView;
EditText enter;
@Override
public void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.display);
    listView=(ListView)findViewById(R.id.listView);

    //OpenAndQueryDatabase

    try {
    DatabaseHelper db = new DatabaseHelper(this);
        //Open the database
        Mydatabase = db.getWritableDatabase();
        Cursor c = Mydatabase.rawQuery(
                "SELECT * FROM Student", null);

        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                    String Name = c.getString(
                            c.getColumnIndex("Name"));
                    int age = c.getInt(
                            c.getColumnIndex("AGE"));
                    String address = c.getString(
                            c.getColumnIndex("ADDRESS"));
                arrayList.add("Name    : " + Name +"\n"+ 
                               "Age        : " +age +"\n"+
                               "Address : " + address+"\n");
                }while (c.moveToNext());
            } 
        }           
        } catch (SQLiteException se ) { } 
        finally {
        if (Mydatabase != null) 
            Mydatabase.close();
    }


    //displayResultList

  listView.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, 
                                    arrayList));
}
}
Sam
  • 86,580
  • 20
  • 181
  • 179
aravind varma
  • 90
  • 5
  • 13
  • I'm not quite clear on what you want... Your code shows you are creating an empty database... Yet you say you want to copy a file... Did you create the DB outside of your app and now want to load it and use it in your app? – Barak Jun 20 '12 at 06:46
  • no...i created my app..and i want to view it in sqlitedatabase.i inserted the data in emmulator when the app is run..and i get all the data entered in another page in list view.....now i want to see the data entered in SQLite database – aravind varma Jun 20 '12 at 06:56

3 Answers3

0

You can find your answer here. Just create a file inside a place in SD Card.

data/ folder is protected by the OS, just rooted devices can see it.

What you need is inside the answer of the link: open the file inside your SDCARD with new File("/sdcard/myFolder/myFile.sqlite").

File databaseLocation = new File("/sdcard/myFolder/myFile.sqlite" ); 
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(databaseLocation , null);
Community
  • 1
  • 1
  • :but i am using eclipse for developing app....i dnt find the data/data//database/ path...so i am stuck here how to view the date inserted in the emmulator in th sqlite database – aravind varma Jun 20 '12 at 06:45
0

You can pull the database off the emulator to view by doing the following.

1) Go to the DDMS screen in Eclipse.
2) Open the tab called "File Explorer"
3) Navigate to the directory containing your database (usually data\data\your.package.name\databases)
4) Highlight/select your database file
5) In the upper right corner click on the icon with the arrow pointing to the right "Pull a file from device"
6) Follow the prompts to save the file to your computer.
7) Open the file with your choice of SQLite database browsers.

EDIT

Note this only works with data on the emulator... it will not pull a database from a real device as the data directory on a real device is protected/inaccessible outside outdside of your app.

Barak
  • 16,318
  • 9
  • 52
  • 84
0

In eclipse go to windows -> Showview ->android ->File Explorer There u can see your database here /data/data/yourpackage/database Pull your database from there using button in right corner. https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/ use this Firefox add to view your data.

machu
  • 111
  • 2
  • 11