0

I used SQLite Database Browser to create a database for my android app. I retrieved the data from this database by using the code below:

public class DataBaseHelper extends SQLiteOpenHelper {

 private static String DB_NAME = "tourdb.sqlite";
 private SQLiteDatabase db;
 private final Context context;
 private String DB_PATH;

 public DataBaseHelper(Context context) {
      super(context, DB_NAME, null, 1);
      this.context = context;
      DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
 }

 public void createDataBase() throws IOException {

      boolean dbExist = checkDataBase();
      if (dbExist) {

      } else {
       this.getReadableDatabase();
       try {
        copyDataBase();
       } catch (IOException e) {
        throw new Error("Error copying database");
       }
      }
 }

 private boolean checkDataBase() {
      File dbFile = new File(DB_PATH + DB_NAME);
      return dbFile.exists();
 }

 private void copyDataBase() throws IOException {

      InputStream myInput = context.getAssets().open(DB_NAME);
      String outFileName = DB_PATH + DB_NAME;
      OutputStream myOutput = new FileOutputStream(outFileName);
      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 Cursor getData() {
      String myPath = DB_PATH + DB_NAME;
      db = SQLiteDatabase.openDatabase(myPath, null,
        SQLiteDatabase.OPEN_READONLY);
      Cursor c = db.rawQuery("SELECT * FROM CITY", null);
       // Note: Master is the one table in External db. Here we trying to access the records of table from external db.
      return c;
 }

 @Override
 public void onCreate(SQLiteDatabase arg0) {
  // TODO Auto-generated method stub
 }

 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  // TODO Auto-generated method stub
 }

}

public class event extends Activity {

DataBaseHelper dbhelper; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.event);

    // if you use siplecursoradapter then you should have _id as one of column name and its values should be integer in your db.
      // so "_id", "columnName1", "columnName2" are column names from your db.
      String[] from = new String[] { "_id", "cityName", "cityLong", "cityLat" };
      int[] to = new int[] { R.id.TextView1, R.id.TextView2, R.id.TextView3 };

      dbhelper = new DataBaseHelper(this);
      try {
       dbhelper.createDataBase();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }

      Cursor c = dbhelper.getData();

      @SuppressWarnings("deprecation")
      SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.list, c, from, to);

       ListView list = (ListView) findViewById(R.id.ListView1);

       list.setAdapter(adapter);
}

}

After I run my code, the LogCat shown that: there is no table "CITY" and i had try to look the database in DDMS it does not shown all the table that i had create. it only shown the android_metadata. Why does this happen? Why my table does not appeared?

angie1289
  • 1
  • 3

1 Answers1

0
     @Override
    public void onCreate(SQLiteDatabase arg0) {

// TODO Auto-generated method stub

  String CREATE_CONTACTS_TABLE = "CREATE TABLE if not exists City"+ 
            "("+ "_id"+ " INTEGER PRIMARY KEY," +
            "cityName" + " TEXT,"+ 
            "cityLong"+ " TEXT," +
            "cityLat"+ " TEXT)";


    db.execSQL(CREATE_CONTACTS_TABLE);

}

Karthick pop
  • 616
  • 3
  • 16