0

これは私の SQLOpenHelper のコードです。

public class ContactsDB extends SQLiteOpenHelper{

    /** Database name */
    private static String DBNAME = "sqlspinnersmsdemo";

    /** Version number of the database */
    private static int VERSION = 1;

    /** Field 1 of the table contacts, which is the primary key */
    public static final String KEY_ROW_ID = "_id";

    /** Field 2 of the table contacts, stores the contact name */
    public static final String KEY_NAME = "name";

    /** Field 3 of the table contacts, stores the phone number of the contact */
    public static final String KEY_PHONE = "phone";

    /** A constant, stores the the table name */
    private static final String DATABASE_TABLE = "contacts";

    /** An instance variable for SQLiteDatabase */
    private SQLiteDatabase mDB;  


    /** Constructor */
    public ContactsDB(Context context) {
        super(context, DBNAME, null, VERSION);  
        this.mDB = getWritableDatabase();
    }


    /** This is a callback method, invoked when the method getReadableDatabase() / getWritableDatabase() is called 
      * provided the database does not exists 
    * */
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql =    "create table contacts (_id integer primary key autoincrement , "
                        + " name text not null , phone text not null ) " ;      
        db.execSQL(sql);
    }

    /** Inserts a new contact to the table contacts */
    public long insert(ContentValues contentValues){
        long rowID = mDB.insert(DATABASE_TABLE, null, contentValues);
        return rowID;

    }

    /** Updates a contact */
    public int update(ContentValues contentValues,String contactID){
        int cnt = mDB.update(DATABASE_TABLE, contentValues, "_id=" + contactID, null);
        return cnt;
    }

    /** Deletes a contact from the table */
    public int del(String contactID){
        int cnt = mDB.delete(DATABASE_TABLE, "_id="+contactID, null);       
        return cnt;
    }

    /** Returns all the contacts in the table */
    public Cursor getAllContacts(){
        return mDB.query(DATABASE_TABLE, new String[] { KEY_ROW_ID,  KEY_NAME , KEY_PHONE } , null, null, null, null, KEY_NAME + " asc ");
    }

    /** Returns a contact by passing its id */
    public Cursor getContactByID(String contactID){
        return mDB.query(DATABASE_TABLE, new String[] { KEY_ROW_ID,  KEY_NAME , KEY_PHONE } , "_ID="+contactID, null, null, null, KEY_NAME + " asc ");
    }


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

    }

}

そして、データベースがアセットから読み取られ、初めて作成されないように変更したいのですが、どうすればこれを処理できますか? 私の提案するコードはこれですが、どこに置くべきかわかりません!

public ContactDB(Context context, String databaseName) {
        super(context, databaseName, null, 1);
        this.context = context;

        String packageName = context.getPackageName();
        DB_PATH = String.format("//data//data//%s//databases//", packageName);
        DB_NAME = databaseName;
        openDataBase();
    }


public SQLiteDatabase openDataBase() throws SQLException {
        String path = DB_PATH + DB_NAME;
        if (database == null) {
            createDataBase();
            database = SQLiteDatabase.openDatabase(path, null,
                SQLiteDatabase.OPEN_READWRITE);
        }
        return database;
    }

あなたが私を助けてくれるなら、私はうれしいです!

4

1 に答える 1

1

これは、必要なクラスの作業コードです。

public class ContactsDB extends SQLiteOpenHelper {  
public static String DB_PATH = "/data/data/com.example.test/databases/";  
public static String DB_NAME = "database";  
public static SQLiteDatabase mDataBase;  
private final Context mContext;  
public DataBaseManager(Context context)throws IOException{  
super(context,DB_NAME,null,DATABASE_VERSION);  
this.mContext=context;

boolean dbexist=checkDataBase();
if(dbexist){
openDataBase();
}else{
System.out.println("Database does not exist");
createDataBase();
}
public void createDataBase() throws IOException {

boolean dbExist = checkDataBase();
SQLiteDatabase db_read= null;

if(dbExist){
// do nothing - database already exist
}else{
db_read=this.getReadableDatabase();
db_read.close();

try {
copyDataBase();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
}  
public boolean checkDataBase() {


boolean checkdb=false;

try{
String myPath = DB_PATH + DB_NAME;
File dbfile=new File(myPath);
checkdb=dbfile.exists();

}catch (SQLiteException e){
throw new Error("Database does not exist");
}

return checkdb;
}  
public void copyDataBase() throws IOException {  
InputStream myInput = mContext.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);
}

myOutput.flush();
myOutput.close();
myInput.close();
}  

public void openDataBase() throws SQLException {

String myPath = DB_PATH + DB_NAME;
mDataBase = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
}  

これを機能させるには、ContactsDB クラスのインスタンスを作成します。;)

于 2013-01-02T11:06:44.573 に答える