我正在用SQL開發一個簡單的Android應用程序。 我遵循以下指南 - http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/無法打開數據庫-Android
我試圖打開數據庫時出現錯誤。 這裏是我的DataBaseHelp.Java講座
public class DataBaseHelper extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/and.testDB/databases/";
private static String DB_NAME = "MyData";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
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;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// 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() {
//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();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
這是我testDB.Java接收機類
public class testDB extends Activity {
SQLiteDatabase myDataBase;
public String[] gur = new String[4];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DataBaseHelper myDbHelper = new DataBaseHelper(this);
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
// Here is the problem-Can't open the database/
}catch(SQLException sqle){
throw sqle;
}
myDataBase = myDbHelper.getWritableDatabase();
Cursor c = myDataBase.rawQuery("SELECT * FROM beer", null);
if (c != null) {
if (c.moveToFirst()) {
for(int x=0;x< 1;x++)
{
gur[x] = (c.getString(c.getColumnIndex("name")));
c.moveToNext();
}
}
Toast.makeText(this,gur[0],Toast.LENGTH_LONG).show();
}
}
}
我使用SQLite數據庫瀏覽器創建的數據庫,創建了一個名爲「啤酒」表中,添加2行並將其放置在我的項目中的/ assests文件夾中。
請注意,我是SQL新手,我第一次使用它。我整天搜索一個答案,並找不到一個答案。
謝謝!
http://stackoverflow.com/questions/9109438/how-to-use-existing-database-with -android-app/9109728#9109728 – 2012-09-07 04:11:56