2011-03-19 39 views
0

這裏是我的代碼,並顯示錯誤:外部數據庫好好嘗試一下工作

'no such table:AddressBookGroup:,While compiling select * from AddressBookGroup' 

我已經把數據庫文件中asstes與此表文件夾。

我已經使用openhelper類代碼從這個鏈接using-your-own-sqlite-database-in-android-applications

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteException; 
import android.database.sqlite.SQLiteOpenHelper; 

public class AddressBookGroup { 
    private final static String ID = "ID"; 
    private final static String NAME = "Name"; 

    public String Id; 
    public String Name; 

    private static final String DATABASE_PATH = "/data/data/com.devindia.dataSample/databases/"; 
    private static final String DATABASE_NAME = "AirtelApp.db"; 
    private static final int DATABASE_VERSION = 1; 
    private static final String TABLE_NAME = "AddressBookGroup"; 

    private Context context; 
    private SQLiteDatabase db; 

    public AddressBookGroup(Context context) { 
     this.context = context; 
     OpenHelper oh=new OpenHelper(context); 
     try { 
      oh.createDataBase(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    this.db= oh.openDataBase(); 
     int i=0; 



    } 

    public AddressBookGroup() { 
     // TODO Auto-generated constructor stub 
    } 

    public void insert(AddressBookGroup ret) { 
     final ContentValues values = new ContentValues(); 

     Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null); 
     values.put(ID, cursor.getCount() + 1); 
     values.put(NAME, ret.Name); 

     db.insert(TABLE_NAME, null, values); 
    } 

    public void deleteAll() { 
     this.db.delete(TABLE_NAME, null, null); 
    } 

    public AddressBookGroup[] selectAll() { 

     // Cursor cursor = this.db.query(TABLE_NAME, new String[] { NAME }, 
     // null,null, null, null, "name desc"); 
     Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null); 

     AddressBookGroup[] ret = new AddressBookGroup[cursor.getCount()]; 
     if (cursor.moveToFirst()) { 
      int i = 0; 
      do { 
       ret[i] = new AddressBookGroup(); 
       ret[i].Id = (cursor.getString(0)); 
       ret[i].Name = (cursor.getString(1)); 
        // ret[i].Name = cursor.getString(cursor.getColumnIndex(NAME)); 
       // ret[i].Name= cursor.getString(cursor.getColumnIndex(PHONE)); 
       i++; 
      } while (cursor.moveToNext()); 
     } 
     if (cursor != null && !cursor.isClosed()) { 
      cursor.close(); 
     } 
     return ret; 
    } 

    private class OpenHelper extends SQLiteOpenHelper { 
     Context context; 

     OpenHelper(Context context) { 

      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
      this.context = context; 
     } 

     public void onCreate(SQLiteDatabase db) { 

     } 

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

     } 

     /** 
     * Creates a empty database on the system and rewrites it with your own 
     * database. 
     * */ 
     public void createDataBase() throws IOException { 

      boolean dbExist = checkDataBase(); 

      if (dbExist) { 
       // do nothing - database already exist 
      } else { 

       // By calling this method and empty database will be created 
       // into the default system path 
       // of your application so we are gonna be able to overwrite that 
       // database with our database. 
       this.getReadableDatabase(); 

       try { 

        copyDataBase(); 

       } catch (IOException e) { 

        throw new Error("Error copying database"); 

       } 
      } 

     } 

     /** 
     * Check if the database already exist to avoid re-copying the file each 
     * time you open the application. 
     * 
     * @return true if it exists, false if it doesn't 
     */ 
     private boolean checkDataBase() { 

      SQLiteDatabase checkDB = null; 

      try { 
       String myPath = DATABASE_PATH + DATABASE_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 = context.getAssets().open(DATABASE_NAME); 

      // Path to the just created empty db 
      String outFileName = DATABASE_PATH + DATABASE_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[3072]; 
      int length; 
      while ((length = myInput.read(buffer)) > 0) { 
       myOutput.write(buffer, 0, length); 
      } 

      // Close the streams 
      myOutput.flush(); 
      myOutput.close(); 
      myInput.close(); 

     } 

     public SQLiteDatabase openDataBase() throws SQLException { 
      SQLiteDatabase db1; 
      // Open the database 
      String myPath = DATABASE_PATH + DATABASE_NAME; 
      db1 = SQLiteDatabase.openDatabase(myPath, null, 
        SQLiteDatabase.OPEN_READWRITE); 
      int i = 0; 
      return db1; 

     } 

    } 
} 

回答

0

先複製您的數據庫資產的文件夾。並嘗試實現以下代碼。

公共靜態無效copyDataBase(){ 嘗試 { // 打開你的本地數據庫的輸入流

   OutputStream databaseOutputStream = new 
       FileOutputStream("/data/data/com.devindia.dataSample/databases"); 
      // Path to the just created empty db 


      InputStream databaseInputStream; 

     byte[] buffer = new byte[1024]; 
     int length; 

     databaseInputStream = minnesota.databaseInputStream1; 


     while ((length = databaseInputStream.read(buffer)) > 0) { 
      databaseOutputStream.write(buffer); 
     } 
     databaseInputStream.close(); 

     databaseInputStream = minnesota.databaseInputStream2; 

     while ((length = databaseInputStream.read(buffer)) > 0) { 
      databaseOutputStream.write(buffer); 
     } 
     databaseInputStream .close();   
     databaseOutputStream.flush(); 
     databaseOutputStream.close(); 



     }catch (Exception e) { 
         // TODO: handle exception 
       //Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show(); 
       } 
} 
+0

此代碼工作對我很好,我希望也爲U將。 – 2011-03-19 08:39:44

相關問題