2014-03-12 57 views
0

我使用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的是:有沒有表「城市「,我曾嘗試在DDMS中查看數據庫,但沒有顯示我創建的所有表。它只顯示android_metadata。 這是爲什麼發生?爲什麼我的桌子不出現?

+0

當你在這裏創建表? –

+0

@KarthickPandiyan,抱歉,我不明白你的意思。 – angie1289

+0

確保表格名稱「CITY」在數據庫中可用。 –

回答

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

}

+0

我已經創建我的數據庫並將其放置在資產文件夾中。所以,我仍然需要重新創建它? – angie1289