使用的外部数据库缺少表

时间:2014-03-12 06:58:51

标签: android database sqlite

我使用SQLite数据库浏览器为我的Android应用程序创建数据库。 我使用以下代码从该数据库中检索数据:

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);
}

}

运行我的代码后,LogCat显示:没有表" CITY"我试图在DDMS中查看数据库,它没有显示我创建的所有表。它只显示了android_metadata。 为什么会这样?为什么我的桌子没有出现?

1 个答案:

答案 0 :(得分:0)

     @Override
    public void onCreate(SQLiteDatabase arg0) {

// TODO自动生成的方法存根

  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);

}