2010-08-23 8 views
13

मैं एक मौजूदा SQLite डेटाबेस के साथ एक अनुप्रयोग को तैनात करने की कोशिश कर रहा हूँ।मौजूदा SQLite डेटाबेस के लिए एक पूर्ण एंड्रॉइड डेटाबेस हेल्पर क्लास क्या है?

मैंने कई नमूनों को ऑनलाइन कार्यान्वित करने और पढ़ने का प्रयास किया है, हालांकि मुझे पता चला है कि वे हमेशा कुछ कोड खो रहे हैं और या तो विज्ञापन के रूप में संकलित या काम नहीं करते हैं।

क्या किसी के पास एंड्रॉइड पर मौजूदा SQLite डेटाबेस को तैनात करने के लिए एक पूर्ण एंड्रॉइड डेटाबेस हेल्पर क्लास है?

+0

मैं अपने कोड का उपयोग करने की कोशिश की है, लेकिन यह इस हिस्से पर अटक जाती है: ActivityManager: शुरू: आशय {अधिनियम = android.intent.action.MAIN बिल्ली = [android.intent.category.LAUNCHER] सीएमपी = com.mdegges/.MicheleActivity। डेटा/डेटा/com.mdegges/डेटाबेस फ़ाइल कभी नहीं बनाई जाती है। – mdegges

+0

@mdegges मुझे यकीन नहीं है कि वहां क्या गलत हो रहा है। आपके अन्य प्रश्नों से ऐसा लगता है कि आप लॉन्चर भाग पर भी अटक गए हैं। क्या आप डीबी कोड को शामिल करने से पहले एक हैलो वर्ल्ड टेस्ट करने में सक्षम हैं? –

+0

हां, मैं देव साइट पर बहुत सारे टट्स (हैलो वर्ल्ड समेत) को पूरा करने में सक्षम हूं। यह बहुत अजीब बात है कि मैंने कोशिश की गई किसी भी मार्गदर्शिका में काम नहीं किया है .. डीबी मेरे संपत्ति फ़ोल्डर में है, मैंने डीबी_PATH को सही आउटपुट फ़ोल्डर में बदल दिया है, और मैंने डीबी नाम को मेरे डीबी में बदल दिया है (विस्तार के साथ और बिना), लेकिन कोई भाग्य नहीं! – mdegges

उत्तर

27

यही वह है जो मैं आया था, उम्मीद है कि इससे परेशानी हो रही अन्य लोगों की मदद मिलेगी।

package com.MyPackage; 

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

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; 
import android.util.Log; 

public class AnyDBAdapter { 

    private static final String TAG = "AnyDBAdapter"; 
    private DatabaseHelper mDbHelper; 
    private static SQLiteDatabase mDb; 

    //make sure this matches the 
    //package com.MyPackage; 
    //at the top of this file 
    private static String DB_PATH = "/data/data/com.MyPackage/databases/"; 

    //make sure this matches your database name in your assets folder 
    // my database file does not have an extension on it 
    // if yours does 
    // add the extention 
    private static final String DATABASE_NAME = "data"; 

    //Im using an sqlite3 database, I have no clue if this makes a difference or not 
    private static final int DATABASE_VERSION = 3; 

    private final Context adapterContext; 

    public AnyDBAdapter(Context context) { 
     this.adapterContext = context; 
    } 

    public AnyDBAdapter open() throws SQLException { 
     mDbHelper = new DatabaseHelper(adapterContext); 

     try { 
      mDbHelper.createDataBase(); 
     } catch (IOException ioe) { 
      throw new Error("Unable to create database"); 
     } 

     try { 
      mDbHelper.openDataBase(); 
     } catch (SQLException sqle) { 
      throw sqle; 
     } 
     return this; 
    } 
    //Usage from outside 
    // AnyDBAdapter dba = new AnyDBAdapter(contextObject); //in my case contextObject is a Map 
    // dba.open(); 
    // Cursor c = dba.ExampleSelect("Rawr!"); 
    // contextObject.startManagingCursor(c); 
    // String s1 = "", s2 = ""; 
    // if(c.moveToFirst()) 
    // do { 
    // s1 = c.getString(0); 
    // s2 = c.getString(1); 
    // } while (c.moveToNext()); 
    // dba.close(); 
    public Cursor ExampleSelect(string myVariable) 
    { 
     String query = "SELECT locale, ? FROM android_metadata"; 
     return mDb.rawQuery(query, new String[]{myVariable}); 
    } 

    //Usage 
    // AnyDBAdatper dba = new AnyDBAdapter(contextObjecT); 
    // dba.open(); 
    // dba.ExampleCommand("en-CA", "en-GB"); 
    // dba.close(); 
    public void ExampleCommand(String myVariable1, String myVariable2) 
    { 
     String command = "INSERT INTO android_metadata (locale) SELECT ? UNION ALL SELECT ?"; 
     mDb.execSQL(command, new String[]{ myVariable1, myVariable2}); 
    } 

    public void close() { 
     mDbHelper.close(); 
    } 

    private static class DatabaseHelper extends SQLiteOpenHelper { 

     Context helperContext; 

     DatabaseHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
      helperContext = context; 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      Log.w(TAG, "Upgrading database!!!!!"); 
      //db.execSQL(""); 
      onCreate(db); 
     } 

     public void createDataBase() throws IOException { 
      boolean dbExist = checkDataBase(); 
      if (dbExist) { 
      } else { 

       //make sure your database has this table already created in it 
       //this does not actually work here 
       /* 
       * db.execSQL("CREATE TABLE IF NOT EXISTS \"android_metadata\" (\"locale\" TEXT DEFAULT 'en_US')" 
       *); 
       * db.execSQL("INSERT INTO \"android_metadata\" VALUES ('en_US')" 
       *); 
       */ 
       this.getReadableDatabase(); 
       try { 
        copyDataBase(); 
       } catch (IOException e) { 
        throw new Error("Error copying database"); 
       } 
      } 
     } 

     public SQLiteDatabase getDatabase() { 
      String myPath = DB_PATH + DATABASE_NAME; 
      return SQLiteDatabase.openDatabase(myPath, null, 
        SQLiteDatabase.OPEN_READONLY); 
     } 

     private boolean checkDataBase() { 
      SQLiteDatabase checkDB = null; 
      try { 
       String myPath = DB_PATH + DATABASE_NAME; 
       checkDB = SQLiteDatabase.openDatabase(myPath, null, 
         SQLiteDatabase.OPEN_READONLY); 
      } catch (SQLiteException e) { 
      } 
      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 = helperContext.getAssets().open(DATABASE_NAME); 

      // Path to the just created empty db 
      String outFileName = DB_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[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() throws SQLException { 
      // Open the database 
      String myPath = DB_PATH + DATABASE_NAME; 
      mDb = SQLiteDatabase.openDatabase(myPath, null, 
        SQLiteDatabase.OPEN_READWRITE); 
     } 

     @Override 
     public synchronized void close() { 

      if (mDb != null) 
       mDb.close(); 

      super.close(); 

     } 
    } 

} 
+1

अपनी कक्षा के साथ, एसडी कार्ड पर मौजूद डेटाबेस खोलना संभव है? – Thiago

+0

यदि आपके पास एक मौजूदा डीबी है जिसे आपने अपने एसडी कार्ड में कॉपी किया है तो मुझे लगता है कि आप 'copyDataBase()' कॉल को हटाने और उचित स्थान पर DB_PATH को बदलने का प्रयास कर सकते हैं। लेकिन यह सिर्फ एक अनुमान है। मैं उपयोगकर्ता को ऐप डेटा तक सीधे पहुंचने की अनुमति नहीं दूंगा, इसलिए मैंने कभी भी उस विकल्प को नहीं माना। –

+0

mContext कहां परिभाषित किया गया है? – Carnivoris

4
DatabaseHelper dbHelper = new DatabaseHelper(getApplicationContext()); 

सुनिश्चित करें कि आप एप्लिकेशन जीवनकाल के दौरान एक बार डेटाबेसहेल्पर ऑब्जेक्ट बनाते हैं और इसका पुन: उपयोग करते हैं।

पढ़ने डेटा के लिए:

SQLiteDatabase db = dbHelper.getReadableDatabase(); 

पढ़ने/संशोधन डेटा के लिए:

SQLiteDatabase db = dbHelper.getWritableDatabase(); 

अगले उपयोग insert(), query(), update(), SQLiteDatabase वस्तु की delete() तरीके:

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

आपको SQL डेटा फ़ाइल को सीधे एक्सेस करके डेटाबेस भी नहीं बनाना चाहिए जो आप न्यूडेटाबेस विधि बनाते हैं। अपने ऑनक्रेट (...) विधि में SQLiteDatabase ऑब्जेक्ट की execSQL() विधि का उपयोग करें। वहां अपने CREATE तालिका क्वेरी निष्पादित करें।

+0

हाय राडेक-के, क्या आप अपने पहले बिंदु पर विस्तार करने में सक्षम होंगे? 'सुनिश्चित करें कि आप एप्लिकेशन लाइफटाइम के दौरान एक बार डेटाबेसहेल्पर ऑब्जेक्ट बनाते हैं और इसका पुन: उपयोग करते हैं। –

+1

यदि आप डेटाबेसहेल्पर ऑब्जेक्ट बनाते हैं तो कई बार सुनिश्चित करें कि आपने पहले डेटाबेस बंद कर दिया है - getWritableDatabase.close()। एक बहु थ्रेडेड एप्लिकेशन मॉडल में जब आप पिछले एक को बंद नहीं करते हैं तो आप डेटाबेसहेल्पर को फिर से नहीं बना सकते हैं। आपको बस कुछ अपवाद मिलेगा। सबसे अच्छा तरीका डेटाबेसहेल्पर ऑब्जेक्ट को केवल एक बार बनाने के लिए है (उदाहरण के लिए आवेदन में) और उसी ऑब्जेक्ट संदर्भ का हर समय उपयोग करें। – plugmind

+1

मैं 'getAplicationContext()' के उपयोग की अनुशंसा नहीं करता। इसके बजाय इसका उपयोग करें, क्योंकि जो भी हो रहा है 'getAplicationContext() 'एक' संदर्भ 'है। – CommonsWare

संबंधित मुद्दे