Advertisement
Guest User

Untitled

a guest
Jul 6th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. package com.android.me;
  2.  
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import android.content.Context;
  8. import android.database.Cursor;
  9. import android.database.sqlite.SQLiteAbortException;
  10. import android.database.sqlite.SQLiteDatabase;
  11. import android.database.sqlite.SQLiteException;
  12. import android.database.sqlite.SQLiteOpenHelper;
  13. import android.util.Log;
  14.  
  15. public class Database extends SQLiteOpenHelper {
  16.  
  17.     public Context cont;
  18.     public String DB_NAME;
  19.     public Cursor c=null;
  20.     public boolean add=false;
  21.     public SQLiteDatabase database;
  22.    
  23.     public Database(Context context, String DBNAME, boolean add) {
  24.         super(context, null, null, 1);
  25.         this.cont=context;
  26.         this.add = add;
  27.         this.DB_NAME=DBNAME;
  28.    
  29.     }
  30.  
  31.     @Override
  32.     public void onCreate(SQLiteDatabase db) {
  33.         // TODO Auto-generated method stub
  34.        
  35.     }
  36.  
  37.     @Override
  38.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  39.         // TODO Auto-generated method stub
  40.        
  41.     }
  42.  
  43.     public void copyDb() throws IOException
  44.     {
  45.         InputStream in = cont.getAssets().open(DB_NAME);
  46.         OutputStream out = new FileOutputStream("/data/data/com.android.me/files/"+DB_NAME);
  47.         try
  48.         {
  49.         byte[] buffer = new byte[1024];
  50.         int dolzina = 0;
  51.         while((dolzina=in.read(buffer))>0)
  52.         {
  53.             out.write(buffer, 0, dolzina);
  54.         }
  55.         in.close();
  56.         out.flush();
  57.         out.close();
  58.         }
  59.             catch(IOException e)
  60.             {
  61.                 e.printStackTrace();
  62.         }
  63.     }
  64.     public SQLiteDatabase sendDatabase()
  65.     {
  66.         if(DB_NAME.equalsIgnoreCase("favorites"))
  67.         {
  68.             return SQLiteDatabase.openDatabase("/data/data/com.android.me/files/"+DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);
  69.         }
  70.         else if(add)
  71.             {
  72.             Log.i("get WRITABLE", "VLEZ");
  73.                 return this.getWritableDatabase();
  74.             }
  75.        
  76.             else{
  77.         return SQLiteDatabase.openDatabase("/data/data/com.android.me/files/"+DB_NAME, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
  78.         }
  79.     }
  80.     public void closedb()
  81.     {
  82.         if(database !=null)
  83.         {
  84.             database.close();
  85.         }
  86.     }
  87.         public void openDatabase()throws SQLiteException{
  88.    
  89.         try
  90.         {
  91.             if(add)
  92.             {
  93.                 Log.i("GET WRITABLE", "VLEZ");
  94.                 database = SQLiteDatabase.openDatabase("/data/data/com.android.me/files/"+DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);
  95.             }else{
  96.         database = SQLiteDatabase.openDatabase("/data/data/com.android.me/files/"+DB_NAME, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
  97.             }
  98.         }
  99.         catch(SQLiteException se)
  100.         {
  101.            
  102.         }
  103.        
  104.     }
  105.     public boolean checkDb() throws SQLiteAbortException
  106.     {
  107.         boolean databaseContains=false;
  108.         try
  109.         {      
  110.         database = SQLiteDatabase.openDatabase("/data/data/com.android.me/files/"+DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);
  111.         }
  112.         catch(SQLiteException se)
  113.         {   }
  114.          if (database == null) {
  115.              databaseContains=false;
  116.          }
  117.          else
  118.          {
  119.              database.close();
  120.          }
  121.         return databaseContains;
  122.     }
  123.  
  124.     public void createDatabase() throws IOException
  125.  
  126.     {
  127.         if(checkDb())
  128.         {}
  129.         else{
  130.         //  this.getReadableDatabase();
  131.        
  132.             try{
  133.                 copyDb();}
  134.             catch(IOException e){              
  135.             }
  136.         }
  137.        
  138.     }
  139.    
  140.    
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement