2015-03-31 3 views
2

Эй, я хочу знать, как создать базу данных для андроида, а не тот, который нужно отредактировать пользователю, база данных, которая является полной и находится в приложении apk. Я использую excel или access, а затем добавляю базу данных в папки приложений или что мне делать? все, что я мог найти в Интернете, - это создание базы данных, в которой эти данные добавляются с помощью самого приложения. Итак, как мне создать базу данных?Как создать базу данных SQL для Android?

+0

можно дубликат [Как доставляете Android приложения с базой данных?] (HTTP: // StackOverflow .com/questions/513084/how-to-ship-an-android-application-with-a-database) –

+0

Я использую плагин SQLiteManager FireFox. –

ответ

0

Вам просто нужно добавить sqlite-файл с базой данных в ресурсы приложения, если я правильно помню, и использовать его с классом DBHelper, например these, но адаптированным к вашим приложениям.

Я лично создаю и редактирую файлы sqlite с помощью this Firefox extension.

0

Вы можете разместить существующую базу данных SQLite в папке с ресурсами вашего приложения. Если вы хотите получить доступ к этой базе данных в своем приложении во время выполнения, вы можете использовать этот код для открытия базы данных;

 SQLiteDatabase db = SQLiteDatabase.openDatabase("path-to-database", null, SQLiteDatabase.OPEN_READONLY); 

В настоящее время я не знаю, можно ли открыть файл db из активов напрямую. В противном случае вы, вероятно, можете скопировать его на внутреннюю директорию базы данных вашего приложения:

private void copyDatabase(final Context context) { 
    InputStream reader = null; 
    try { 
    reader = context.getAssets().open("asset-name"); 
    final File out = context.getDatabasePath("databasename"); 
    final OutputStream writer = new FileOutputStream(out); 
    final byte[] buffer = new byte[1024 * 100]; 
    int bytesRead = reader.read(buffer); 
    while (bytesRead > 0) { 
     writer.write(buffer, 0, bytesRead); 
     bytesRead = reader.read(buffer); 
    } 
    writer.close(); 
    } catch (IOException e) { 
    Log.e(TAG, e.getMessage(), e); 
    } finally { 
    if (reader != null) { 
     try { 
      reader.close(); 
     } catch (IOException e) { 
      Log.e(TAG, e.getMessage(), e); 
     } 
    } 
    } 
} 
0

@Danny Ответил:

Есть два варианта для создания и обновления баз данных.

Одним из них является создание базы данных извне, а затем размещение ее в папке с ресурсами проекта, а затем копирование всей базы данных. Это намного быстрее, если в базе данных много таблиц и других компонентов. Модификации запускаются путем изменения номера версии базы данных в файле res/values ​​/ strings.xml. Модернизация затем будет выполнена путем создания новой базы данных извне, замены старой базы данных в папке с ресурсами новой базой данных, сохранением старой базы данных во внутреннем хранилище под другим именем, копированием новой базы данных из папки ресурсов во внутреннее хранилище, переносом всех данных старой базы данных (которая была переименована ранее) в новую базу данных и, наконец, удаление старой базы данных. Вы можете создать базу данных изначально с помощью плагина SQLite Manager FireFox для выполнения своих SQL-запросов создания.

Другой вариант - создать базу данных внутри файла sql. Это не так быстро, но задержка, вероятно, будет незаметна для пользователей, если в базе данных будет всего несколько таблиц. Модификации запускаются путем изменения номера версии базы данных в файле res/values ​​/ strings.xml. Модернизация затем будет выполнена путем обработки обновленного sql-файла. Данные в базе данных остаются неизменными, за исключением случаев, когда удаляется его контейнер, например, отбрасывая таблицу.

В примере ниже показано, как использовать любой из этих методов.

Приведен пример файла create_database.sql. Он должен быть помещен в папку активов проекта для внутреннего метода или скопирован в «Execute SQL» SQLite Manager для создания базы данных для внешнего метода. (ПРИМЕЧАНИЕ. Обратите внимание на комментарий о таблице, требуемой для Android.)

--Android requires a table named 'android_metadata' with a 'locale' column 
CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US'); 
INSERT INTO "android_metadata" VALUES ('en_US'); 

CREATE TABLE "kitchen_table"; 
CREATE TABLE "coffee_table"; 
CREATE TABLE "pool_table"; 
CREATE TABLE "dining_room_table"; 
CREATE TABLE "card_table"; 

Вот пример update_database.sql файла. Он должен быть размещен в папке активов проекта для внутреннего метода или скопирована в «Execute SQL» в SQLite Manager, чтобы создать базу данных для внешнего метод. (ПРИМЕЧАНИЕ. Обратите внимание, что все три типа комментариев SQL будут игнорироваться парсером sql, который включен в этот пример.)

--CREATE TABLE "kitchen_table"; This is one type of comment in sql. It is ignored by parseSql. 
/* 
* CREATE TABLE "coffee_table"; This is a second type of comment in sql. It is ignored by parseSql. 
*/ 
{ 
CREATE TABLE "pool_table"; This is a third type of comment in sql. It is ignored by parseSql. 
} 
/* CREATE TABLE "dining_room_table"; This is a second type of comment in sql. It is ignored by parseSql. */ 
{ CREATE TABLE "card_table"; This is a third type of comment in sql. It is ignored by parseSql. } 

--DROP TABLE "picnic_table"; Uncomment this if picnic table was previously created and now is being replaced. 
CREATE TABLE "picnic_table" ("plates" TEXT); 
INSERT INTO "picnic_table" VALUES ('paper'); 

Вот запись, чтобы добавить в файл /res/values/strings.xml для номера версии базы данных.

<item type="string" name="databaseVersion" format="integer">1</item> 

Это деятельность, которая обращается к базе данных и затем использует ее. (Примечание: Вы можете запустить код базы данных в отдельном потоке, если он использует много ресурсов.)

package android.example; 

import android.app.Activity; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 

/** 
* @author Danny Remington - MacroSolve 
* 
*   Activity for demonstrating how to use a sqlite database. 
*/ 
public class Database extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     DatabaseHelper myDbHelper; 
     SQLiteDatabase myDb = null; 

     myDbHelper = new DatabaseHelper(this); 
     /* 
     * Database must be initialized before it can be used. This will ensure 
     * that the database exists and is the current version. 
     */ 
     myDbHelper.initializeDataBase(); 

     try { 
      // A reference to the database can be obtained after initialization. 
      myDb = myDbHelper.getWritableDatabase(); 
      /* 
      * Place code to use database here. 
      */ 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } finally { 
      try { 
       myDbHelper.close(); 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } finally { 
       myDb.close(); 
      } 
     } 

    } 
} 

Вот вспомогательный класс базы данных, где база данных создается или обновляется, если это необходимо. (Примечание: Android требует, чтобы вы создали класс, который расширяет SQLiteOpenHelper для работы с базой данных Sqlite.)

package android.example; 

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

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

/** 
* @author Danny Remington - MacroSolve 
* 
*   Helper class for sqlite database. 
*/ 
public class DatabaseHelper extends SQLiteOpenHelper { 

    /* 
    * The Android's default system path of the application database in internal 
    * storage. The package of the application is part of the path of the 
    * directory. 
    */ 
    private static String DB_DIR = "/data/data/android.example/databases/"; 
    private static String DB_NAME = "database.sqlite"; 
    private static String DB_PATH = DB_DIR + DB_NAME; 
    private static String OLD_DB_PATH = DB_DIR + "old_" + DB_NAME; 

    private final Context myContext; 

    private boolean createDatabase = false; 
    private boolean upgradeDatabase = false; 

    /** 
    * Constructor Takes and keeps a reference of the passed context in order to 
    * access to the application assets and resources. 
    * 
    * @param context 
    */ 
    public DatabaseHelper(Context context) { 
     super(context, DB_NAME, null, context.getResources().getInteger(
       R.string.databaseVersion)); 
     myContext = context; 
     // Get the path of the database that is based on the context. 
     DB_PATH = myContext.getDatabasePath(DB_NAME).getAbsolutePath(); 
    } 

    /** 
    * Upgrade the database in internal storage if it exists but is not current. 
    * Create a new empty database in internal storage if it does not exist. 
    */ 
    public void initializeDataBase() { 
     /* 
     * Creates or updates the database in internal storage if it is needed 
     * before opening the database. In all cases opening the database copies 
     * the database in internal storage to the cache. 
     */ 
     getWritableDatabase(); 

     if (createDatabase) { 
      /* 
      * If the database is created by the copy method, then the creation 
      * code needs to go here. This method consists of copying the new 
      * database from assets into internal storage and then caching it. 
      */ 
      try { 
       /* 
       * Write over the empty data that was created in internal 
       * storage with the one in assets and then cache it. 
       */ 
       copyDataBase(); 
      } catch (IOException e) { 
       throw new Error("Error copying database"); 
      } 
     } else if (upgradeDatabase) { 
      /* 
      * If the database is upgraded by the copy and reload method, then 
      * the upgrade code needs to go here. This method consists of 
      * renaming the old database in internal storage, create an empty 
      * new database in internal storage, copying the database from 
      * assets to the new database in internal storage, caching the new 
      * database from internal storage, loading the data from the old 
      * database into the new database in the cache and then deleting the 
      * old database from internal storage. 
      */ 
      try { 
       FileHelper.copyFile(DB_PATH, OLD_DB_PATH); 
       copyDataBase(); 
       SQLiteDatabase old_db = SQLiteDatabase.openDatabase(OLD_DB_PATH, null, SQLiteDatabase.OPEN_READWRITE); 
       SQLiteDatabase new_db = SQLiteDatabase.openDatabase(DB_PATH,null, SQLiteDatabase.OPEN_READWRITE); 
       /* 
       * Add code to load data into the new database from the old 
       * database and then delete the old database from internal 
       * storage after all data has been transferred. 
       */ 
      } catch (IOException e) { 
       throw new Error("Error copying database"); 
      } 
     } 

    } 

    /** 
    * Copies your database from your local assets-folder to the just created 
    * empty database in the system folder, from where it can be accessed and 
    * handled. This is done by transfering bytestream. 
    * */ 
    private void copyDataBase() throws IOException { 
     /* 
     * Close SQLiteOpenHelper so it will commit the created empty database 
     * to internal storage. 
     */ 
     close(); 

     /* 
     * Open the database in the assets folder as the input stream. 
     */ 
     InputStream 

    myInput = myContext.getAssets().open(DB_NAME); 

      /* 
      * Open the empty db in interal storage as the output stream. 
      */ 
      OutputStream myOutput = new FileOutputStream(DB_PATH); 

      /* 
      * Copy over the empty db in internal storage with the database in the 
      * assets folder. 
      */ 
      FileHelper.copyFile(myInput, myOutput); 

      /* 
      * Access the copied database so SQLiteHelper will cache it and mark it 
      * as created. 
      */ 
      getWritableDatabase().close(); 
     } 

     /* 
     * This is where the creation of tables and the initial population of the 
     * tables should happen, if a database is being created from scratch instead 
     * of being copied from the application package assets. Copying a database 
     * from the application package assets to internal storage inside this 
     * method will result in a corrupted database. 
     * <P> 
     * NOTE: This method is normally only called when a database has not already 
     * been created. When the database has been copied, then this method is 
     * called the first time a reference to the database is retrieved after the 
     * database is copied since the database last cached by SQLiteOpenHelper is 
     * different than the database in internal storage. 
     */ 
     @Override 
     public void onCreate(SQLiteDatabase db) { 
      /* 
      * Signal that a new database needs to be copied. The copy process must 
      * be performed after the database in the cache has been closed causing 
      * it to be committed to internal storage. Otherwise the database in 
      * internal storage will not have the same creation timestamp as the one 
      * in the cache causing the database in internal storage to be marked as 
      * corrupted. 
      */ 
      createDatabase = true; 

      /* 
      * This will create by reading a sql file and executing the commands in 
      * it. 
      */ 
       // try { 
       // InputStream is = myContext.getResources().getAssets().open(
       // "create_database.sql"); 
       // 
       // String[] statements = FileHelper.parseSqlFile(is); 
       // 
       // for (String statement : statements) { 
       // db.execSQL(statement); 
       // } 
       // } catch (Exception ex) { 
       // ex.printStackTrace(); 
       // } 
     } 

     /** 
     * Called only if version number was changed and the database has already 
     * been created. Copying a database from the application package assets to 
     * the internal data system inside this method will result in a corrupted 
     * database in the internal data system. 
     */ 
     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      /* 
      * Signal that the database needs to be upgraded for the copy method of 
      * creation. The copy process must be performed after the database has 
      * been opened or the database will be corrupted. 
      */ 
      upgradeDatabase = true; 

      /* 
      * Code to update the database via execution of sql statements goes 
      * here. 
      */ 

      /* 
      * This will upgrade by reading a sql file and executing the commands in 
      * it. 
      */ 
      // try { 
      // InputStream is = myContext.getResources().getAssets().open(
      // "upgrade_database.sql"); 
      // 
      // String[] statements = FileHelper.parseSqlFile(is); 
      // 
      // for (String statement : statements) { 
      // db.execSQL(statement); 
      // } 
      // } catch (Exception ex) { 
      // ex.printStackTrace(); 
      // } 
     } 

     /** 
     * Called everytime the database is opened by getReadableDatabase or 
     * getWritableDatabase. This is called after onCreate or onUpgrade is 
     * called. 
     */ 
     @Override 
     public void onOpen(SQLiteDatabase db) { 
      super.onOpen(db); 
     } 

     /* 
     * Add your public helper methods to access and get content from the 
     * database. You could return cursors by doing 
     * "return myDataBase.query(....)" so it'd be easy to you to create adapters 
     * for your views. 
     */ 

    } 
Смежные вопросы