Soru & Cevap

Eclipse te sqlite veritanına erişememe ...

18.11.2014 - 10:08

Arkadaşlar merhaba

Eclipsete android programlamaya çalışıyorum. Fakat data/data klosorunun içine giremiyorum herhangi bir sqlite uzantılı dosya göremedim. ne yapmalıyım? veritabanını dısardan dahil etmek istiyorum. direkt olarak data klasorune tasıdım hata verdi. yardımcı olabilirseniz cok iyi olur. 

6 Görüntülenme

1 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-1791-1380630425.jpg
dgnc
18.11.2014 - 11:32

öncelikle oluşturduğun database i Assent dosyasının içine kaydet.Daha sonra ;

public class mydatabase extends SQLiteOpenHelper{
    
    public static SQLiteDatabase mydb;
    public static Context context;
    public static Cursor cursor;
    public static String db_path="/data/data/package_ismini_yaz/databases/";
    public static String db_name="database_ismini_yaz";
    public static int db_version=1;
    public static String mypath=db_path+db_name;
    
    public mydatabase(Context c) {
          super(c, db_name,null, db_version);//context e bak!
          this.context=c;
        
           try {
                createdb();
                opendb();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
    public void createdb() throws IOException{

//database'in daha önceden olup olmadığını kontrol ediyor.
     boolean dbexist=checkdb();
     
     if (dbexist) {
         // eğer varsa devam ediyor.
     } else {

//database'in okunmasını sağlıyor.

          this.getReadableDatabase();

         try {

assent dosyasındaki database'i kopyalıyor.
             copydb();
          } catch (IOException e) {
            throw new Error("Error copying database");
         }
     }
    }
    public boolean checkdb(){
        SQLiteDatabase checkDB=null;
        
        try{
            checkDB=SQLiteDatabase.openDatabase(mypath, null,SQLiteDatabase.OPEN_READWRITE);
            
        }catch(SQLiteException e){
             // database doesn't exist yet.
        }
        
        if (checkDB != null) {
            checkDB.close();
           }
        
        return checkDB != null;
    }
    public void copydb() throws IOException{
        InputStream myInput=context.getAssets().open(db_name);
        OutputStream myOutput = new FileOutputStream(mypath);
        
        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 opendb()throws SQLException{
        mydb=SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
    }
    public void dbclose(){
        mydb.close();
    }

bu gördüğün kod bloğunu yeni oluşturduğun sınıfın içine kaydet.gerekli açıklamaları kod bloğunun içinde elşinden geldiğince yapmaya çalıştım. umarım yardımcı olur.